-
Notifications
You must be signed in to change notification settings - Fork 2
/
youpin_api_lib.js
96 lines (91 loc) · 2.36 KB
/
youpin_api_lib.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 and do authentication
constructor(uri, doAuth) {
this.uri = uri;
this.token;
const app = feathers()
.configure(hooks())
.configure(rest(uri).superagent(request))
.configure(authentication());
return new Promise(resolve => {
if(doAuth) {
app.authenticate({
type: 'local',
'email': 'contact@youpin.city',
'password': ''
}).then(result => {
this.token = app.get('token');
resolve(this);
}).catch(error => {
console.log(error);
});
} else {
resolve(this);
}
});
}
getUser(callback) {
console.log('Get user...');
request
.get(this.uri+ '/users')
.set('Authorization', 'Bearer ' + this.token)
.end(function(err, resp) {
if (err || !resp.ok) {
console.log('Unable to get user');
return callback(err);
}
callback(resp.body);
});
}
postUser(json, callback) {
console.log('Post user...');
request
.post(this.uri + '/users')
.send(json)
.end(function(err, resp) {
if (err || !resp.ok) {
console.log('Unable to post user');
//console.log(resp);
console.log(err);
return;
}
return callback(resp.body);
});
}
postPin(json, callback) {
console.log('Post pin...');
request
.post(this.uri + '/pins')
.set('Authorization', 'Bearer ' + this.token)
.send(json)
.end(function(err, resp) {
if (err || !resp.ok) {
console.log('Unable to post pin');
//console.log(resp);
console.log(err);
return;
}
return callback(resp.body);
});
}
createApp3rd(json, callback) {
console.log('Create 3rd party app...');
request
.post(this.uri + '/app3rds')
.send(json)
.end(function(err, resp) {
if (err || !resp.ok) {
console.log('Unable to create 3rd app');
console.log(err);
return;
}
return callback(resp.body);
});
}
};
module.exports = Api;