forked from youpin-city/youpin-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
youpin-api.js
67 lines (61 loc) · 1.7 KB
/
youpin-api.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const request = require('superagent');
const feathers = require('feathers/client');
const rest = require('feathers-rest/client');
const hooks = require('feathers-hooks');
const authentication = require('feathers-authentication/client');
class Api {
// initialise api and do authentication
constructor(uri, username, password) {
this.uri = uri;
this.token;
this.username = username;
this.password = password;
const app = feathers()
.configure(hooks())
.configure(rest(uri).superagent(request))
.configure(authentication());
return new Promise(resolve => {
app.authenticate({
type: 'local',
'email': username,
'password': password
}).then(result => {
this.token = app.get('token');
resolve(this);
}).catch(error => {
console.log(error);
});
});
}
postPin(json, callback) {
request
.post(this.uri + '/pins')
.set('Authorization', 'Bearer ' + this.token)
.send(json)
.end(function (error, response) {
if (!error && response.ok) {
callback(response.body);
} else {
console.error('Unable to post a new pin.');
console.error(response);
console.error(error);
}
});
}
uploadPhotoFromURL(imgLink, callback) {
request
.post(this.uri + '/photos/upload_from_url')
.send({ url: imgLink })
.end(function (error, response) {
if (!error && response.ok) {
callback(response.body);
} else {
console.error('Unable to upload a new photo.');
console.error(response);
console.error(error);
}
});
}
};
module.exports = Api;