Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Commit 48b73af

Browse files
committed
adding ability to query the purse api for a users open orders
1 parent 939427a commit 48b73af

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,21 @@ example:
6161
duffle --auth-token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyasdllkflkasdflaslljfjlJ1c2VyX2lkIjo2NzYsInVzZXJuYW1lIjoiZHlsYW5iYXRodXJzdCIsImV4cCI6MTU1MzE5MzUwMiwiZW1haWwiOiJkeWxhbmJhdGh1cnN0QGdtYWlsLmNvbSIsInNkIjoiZ2Z4M081T0kiLCJyIjo.iNkd4cmFoRWYiLCJkcyI6ImZVM orders-info examples/files/orders.csv
6262
```
6363
Your console output will show the number of successful and unsuccessful order info requests, as well as the order info for each order.
64+
65+
66+
## Open Orders
67+
To get a bulk set of open orders on Purse, run:
68+
69+
```
70+
duffle --auth-token YOUR_PURSE_TOKEN open-orders
71+
```
72+
example:
73+
```
74+
duffle --auth-token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyasdllkflkasdflaslljfjlJ1c2VyX2lkIjo2NzYsInVzZXJuYW1lIjoiZHlsYW5iYXRodXJzdCIsImV4cCI6MTU1MzE5MzUwMiwiZW1haWwiOiJkeWxhbmJhdGh1cnN0QGdtYWlsLmNvbSIsInNkIjoiZ2Z4M081T0kiLCJyIjo.iNkd4cmFoRWYiLCJkcyI6ImZVM orders-info examples/files/orders.csv
75+
```
76+
with options:
77+
```
78+
duffle --auth-token YOUR_PURSE_TOKEN --limit 100 --offset 0 open-orders
79+
```
80+
run `duffle open-orders -h` for more options
81+
Your console output will show the number of successful open orders, as well as the order info for each order.

bin/duffle

+27
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,31 @@ program
125125
console.log(`${JSON.stringify(bulkInfo.failedOrders)}`);
126126
});
127127

128+
program
129+
.command('open-orders')
130+
.description('Gets a bulk list of open orders')
131+
.option('-l, --limit [limit]', 'The number of orders to request', 100)
132+
.option('-o, --offset [offset]', 'The number of orders to offset by', 0)
133+
.option('-i, --inactive [inactive]', 'The active or inactive state', false)
134+
.option('-s, --sort [sort]', 'The field to sort the list by', 'created')
135+
.option('-S, --search [search]', 'Search applied to order query', false)
136+
.action(async (options) => {
137+
let Api;
138+
try {
139+
Api = new apiRequest(options.parent.authToken);
140+
} catch (e) {
141+
console.log('*********************');
142+
console.log(e.message);
143+
console.log('*********************');
144+
return;
145+
}
146+
const { limit, offset, inactive, sort, search } = options;
147+
Api.openOrderQS = { limit, offset, inactive, sort, search };
148+
const openOrders = await Api.getBulkOpenOrders();
149+
console.log(`${openOrders.successfulOrders.length} successful orders:`);
150+
console.log(`${JSON.stringify(openOrders.successfulOrders)}`);
151+
console.log(`${openOrders.failedOrders.length} failed orders:`);
152+
console.log(`${JSON.stringify(openOrders.failedOrders)}`);
153+
});
154+
128155
program.parse(process.argv);

lib/api.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class apiRequest {
1616
this.modifyOrder = this.modifyOrder.bind(this);
1717
this.cancelOrder = this.cancelOrder.bind(this);
1818
this.getOrderInfo = this.getOrderInfo.bind(this);
19+
this.getOpenOrders = this.getOpenOrders.bind(this);
1920
}
20-
async bulkChange(bulkMethod, orders) {
21+
async bulkChange(bulkMethod, orders = []) {
2122
const successfulOrders = [];
2223
const failedOrders = [];
2324

@@ -45,6 +46,9 @@ class apiRequest {
4546
async getBulkInfo(orders) {
4647
return this.bulkChange(this.getOrderInfo, orders);
4748
}
49+
async getBulkOpenOrders(params) {
50+
return this.bulkChange(this.getOpenOrders, [{}]);
51+
}
4852

4953
async createOrder(order) {
5054
const validOrder = this.buildCreateOrder(order);
@@ -142,10 +146,8 @@ class apiRequest {
142146

143147
let cancelRequest;
144148
try {
145-
console.log(opts)
146149
cancelRequest = await request(opts);
147150
} catch (e) {
148-
console.log(e)
149151
order.success = false;
150152
order.errorMessage = e.message;
151153
return order;
@@ -181,10 +183,8 @@ class apiRequest {
181183

182184
let infoRequest;
183185
try {
184-
console.log(opts)
185186
infoRequest = await request(opts);
186187
} catch (e) {
187-
console.log(e)
188188
order.success = false;
189189
order.errorMessage = e.message;
190190
return order;
@@ -201,6 +201,37 @@ class apiRequest {
201201
}
202202
}
203203
}
204+
async getOpenOrders(order) {
205+
const opts = {
206+
method: 'GET',
207+
uri: `${this.apiUrl}/orders/me/active`,
208+
qs: this.openOrderQS,
209+
resolveWithFullResponse: true,
210+
headers: {
211+
Authorization: `JWT ${this.jwt}`,
212+
Origin: 'duffle://'
213+
},
214+
};
215+
216+
let infoRequest;
217+
try {
218+
infoRequest = await request(opts);
219+
} catch (e) {
220+
order.success = false;
221+
order.errorMessage = e.message;
222+
return order;
223+
}
224+
225+
if (infoRequest.statusCode !== 200) {
226+
order.success = false;
227+
order.errorMessage = infoRequest.body;
228+
return order;
229+
} else {
230+
order.success = true;
231+
order.body = JSON.parse(infoRequest.body);
232+
return order;
233+
}
234+
}
204235

205236
buildCreateOrder(order) {
206237
const { asin, quantity, discount, full_name, street1,

0 commit comments

Comments
 (0)