-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-client.js
44 lines (38 loc) · 1016 Bytes
/
api-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const request = require('superagent')
const config = require('./config')
const parties = require('./parties')
const parseCookie = (header) => {
return header.map((v) => v.slice(0, v.indexOf(' '))).join(' ').slice(0, -1)
}
const results = new Array(parties.length)
let cookie
const createParty = (party) => {
return new Promise((resolve, reject) => {
request
.post(config.API + '/party/company')
.set('Cookie', cookie)
.send(party)
.end((err, res) => {
if (err) {
resolve(res.body)
} else {
resolve(res.body)
}
})
})
}
const main = async () => {
let res
res = await request
.post(config.API + '/auth/login')
.send({username: config.USERNAME, password: config.PASSWORD})
cookie = parseCookie(res.headers['set-cookie'])
for (let i = 0; i < parties.length; i++) {
results[i] = await createParty(parties[i])
}
}
main().then(() => {
console.log(JSON.stringify(results))
}, (err) => {
console.error(err)
})