-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uber.js
191 lines (161 loc) · 4.3 KB
/
Uber.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const axios = require('axios');
const qs = require('qs');
const createHmac = require('create-hmac');
const MAX_RETRIES = 2;
module.exports = class Uber {
constructor({
version = 'v1',
authUrl = 'https://login.uber.com/oauth/v2/token',
clientId,
clientSecret,
customerId,
webhookApiSecret, // From webhook settings
scope = 'eats.deliveries',
enableAutoTest = false,
debug,
}) {
this.authUrl = authUrl;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.customerId = customerId;
this.webhookApiSecret = webhookApiSecret;
this.version = version;
this.scope = scope;
this.enableAutoTest = enableAutoTest;
this.debug = debug;
}
log(data) {
if (this.debug) {
console.log(data); // eslint-disable-line no-console
}
}
async getApiHeaders(force = false) {
const { accessTokenExpiredAt } = this;
if (force
|| !accessTokenExpiredAt
|| (accessTokenExpiredAt && accessTokenExpiredAt < Date.now())
) {
await this.getAccessToken();
}
const { accessToken } = this;
return {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${accessToken}`,
};
}
getApiUrl(path) {
const {
version,
customerId,
} = this;
return `https://api.uber.com/${version}/customers/${customerId}${path}`;
}
async getAccessToken() {
const {
clientId,
authUrl,
scope,
clientSecret,
} = this;
const options = {
method: 'POST',
url: authUrl,
data: qs.stringify({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope,
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
const {
access_token: accessToken,
expires_in: accessTokenExpiredAt,
} = await this.request(options);
this.accessToken = accessToken;
this.accessTokenExpiredAt = Date.now() + accessTokenExpiredAt * 1000;
}
async request(payload, inRetries = 0) {
try {
this.log(payload);
const { data } = await axios(payload);
return data;
} catch (e) {
// this.log(e);
if (e.response && e.response.status === 403 && inRetries < MAX_RETRIES) {
this.log('Retry for 403');
Object.assign(payload, {
headers: await this.getApiHeaders(true),
});
return this.request(payload, inRetries + 1);
}
if (e.response && e.response.data && e.response.data.message) {
throw new Error(e.response.data.message);
}
throw new Error(e.toJSON().message);
}
}
// headers['X-Postmates-Signature']
verifyWebhook(payload, xPostmatesSignature) {
const { webhookApiSecret } = this;
const uint8 = createHmac('sha256', Buffer.from(webhookApiSecret)).update(payload).digest();
const hexString = Buffer.from(uint8).toString('hex');
return hexString === xPostmatesSignature;
}
// DaaS API
async createQuote(order) {
const options = {
method: 'POST',
url: this.getApiUrl('/delivery_quotes'),
data: order,
headers: await this.getApiHeaders(),
};
return this.request(options);
}
async createDelivery(order) {
if (this.enableAutoTest) {
Object.assign(order, {
test_specifications: {
robo_courier_specification: {
mode: 'auto',
},
},
});
}
const options = {
method: 'POST',
url: this.getApiUrl('/deliveries'),
data: order,
headers: await this.getApiHeaders(),
};
return this.request(options);
}
async cancelDelivery(deliveryId) {
const options = {
method: 'POST',
url: this.getApiUrl(`/deliveries/${deliveryId}/cancel`),
data: {},
headers: await this.getApiHeaders(),
};
return this.request(options);
}
async listDeliveries() {
const options = {
method: 'GET',
url: this.getApiUrl('/deliveries'),
headers: await this.getApiHeaders(),
};
return this.request(options);
}
async getDelivery(deliveryId) {
const options = {
method: 'GET',
url: this.getApiUrl(`/deliveries/${deliveryId}`),
headers: await this.getApiHeaders(),
};
return this.request(options);
}
};