aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/http_client.ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2017-12-20 06:22:38 +0800
committerBrandon Millman <brandon.millman@gmail.com>2017-12-25 10:01:28 +0800
commit8fe81c9d090ce50496f3150602f19433e7aedd1e (patch)
tree7e569cf16cd3a9ad9b09b5e64c5101d0c477f8fe /packages/connect/src/http_client.ts
parent9f3acf8e2888b6105062e47664ecd5adaaf3c889 (diff)
downloaddexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar
dexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.gz
dexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.bz2
dexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.lz
dexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.xz
dexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.zst
dexon-sol-tools-8fe81c9d090ce50496f3150602f19433e7aedd1e.zip
Refactor JSON parsing in HttpClient
Diffstat (limited to 'packages/connect/src/http_client.ts')
-rw-r--r--packages/connect/src/http_client.ts78
1 files changed, 45 insertions, 33 deletions
diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts
index da052ba3c..f738c7e07 100644
--- a/packages/connect/src/http_client.ts
+++ b/packages/connect/src/http_client.ts
@@ -18,7 +18,7 @@ import {
TokenPairsItem,
TokenPairsRequest,
} from './types';
-import {typeConverters} from './utils/type_converters';
+import {relayerResponseJsonParsers} from './utils/relayer_response_json_parsers';
/**
* This class includes all the functionality related to interacting with a set of HTTP endpoints
@@ -48,18 +48,13 @@ export class HttpClient implements Client {
const requestOpts = {
params: request,
};
- const tokenPairs = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts);
- assert.doesConformToSchema(
- 'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema);
- _.each(tokenPairs, (tokenPair: object) => {
- typeConverters.convertStringsFieldsToBigNumbers(tokenPair, [
- 'tokenA.minAmount',
- 'tokenA.maxAmount',
- 'tokenB.minAmount',
- 'tokenB.maxAmount',
- ]);
- });
- return tokenPairs;
+ const result = await this._requestAsync(
+ '/token_pairs',
+ HttpRequestType.Get,
+ relayerResponseJsonParsers.parseTokenPairsJson,
+ requestOpts,
+ );
+ return result;
}
/**
* Retrieve orders from the API
@@ -73,10 +68,13 @@ export class HttpClient implements Client {
const requestOpts = {
params: request,
};
- const orders = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts);
- assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
- _.each(orders, (order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order));
- return orders;
+ const result = await this._requestAsync(
+ `/orders`,
+ HttpRequestType.Get,
+ relayerResponseJsonParsers.parseOrdersJson,
+ requestOpts,
+ );
+ return result;
}
/**
* Retrieve a specific order from the API
@@ -85,10 +83,12 @@ export class HttpClient implements Client {
*/
public async getOrderAsync(orderHash: string): Promise<SignedOrder> {
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
- const order = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get);
- assert.doesConformToSchema('order', order, schemas.signedOrderSchema);
- typeConverters.convertOrderStringFieldsToBigNumber(order);
- return order;
+ const result = await this._requestAsync(
+ `/order/${orderHash}`,
+ HttpRequestType.Get,
+ relayerResponseJsonParsers.parseOrderJson,
+ );
+ return result;
}
/**
* Retrieve an orderbook from the API
@@ -100,10 +100,13 @@ export class HttpClient implements Client {
const requestOpts = {
params: request,
};
- const orderBook = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts);
- assert.doesConformToSchema('orderBook', orderBook, schemas.relayerApiOrderBookResponseSchema);
- typeConverters.convertOrderbookStringFieldsToBigNumber(orderBook);
- return orderBook;
+ const result = await this._requestAsync(
+ '/orderbook',
+ HttpRequestType.Get,
+ relayerResponseJsonParsers.parseOrderbookResponseJson,
+ requestOpts,
+ );
+ return result;
}
/**
* Retrieve fee information from the API
@@ -115,10 +118,13 @@ export class HttpClient implements Client {
const requestOpts = {
payload: request,
};
- const fees = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts);
- assert.doesConformToSchema('fees', fees, schemas.relayerApiFeesResponseSchema);
- typeConverters.convertStringsFieldsToBigNumbers(fees, ['makerFee', 'takerFee']);
- return fees;
+ const result = await this._requestAsync(
+ '/fees',
+ HttpRequestType.Post,
+ relayerResponseJsonParsers.parseFeesResponseJson,
+ requestOpts,
+ );
+ return result;
}
/**
* Submit a signed order to the API
@@ -129,10 +135,16 @@ export class HttpClient implements Client {
const requestOpts = {
payload: signedOrder,
};
- await this._requestAsync('/order', HttpRequestType.Post, requestOpts);
+ await this._requestAsync(
+ '/order',
+ HttpRequestType.Post,
+ _.noop,
+ requestOpts,
+ );
}
- private async _requestAsync(path: string, requestType: HttpRequestType,
- requestOptions?: HttpRequestOptions): Promise<any> {
+ private async _requestAsync<T>(path: string, requestType: HttpRequestType,
+ jsonParser: (json: any) => T,
+ requestOptions?: HttpRequestOptions): Promise<T> {
const params = _.get(requestOptions, 'params');
const payload = _.get(requestOptions, 'payload');
let query = '';
@@ -154,6 +166,6 @@ export class HttpClient implements Client {
throw Error(response.statusText);
}
const json = await response.json();
- return json;
+ return jsonParser(json);
}
}