From 8fe81c9d090ce50496f3150602f19433e7aedd1e Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 19 Dec 2017 17:22:38 -0500 Subject: Refactor JSON parsing in HttpClient --- packages/connect/src/http_client.ts | 78 +++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 33 deletions(-) (limited to 'packages/connect/src/http_client.ts') 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 { 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 { + private async _requestAsync(path: string, requestType: HttpRequestType, + jsonParser: (json: any) => T, + requestOptions?: HttpRequestOptions): Promise { 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); } } -- cgit v1.2.3 From 262c34abac6a42edf7b6f3c463c9473740281296 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 9 Jan 2018 14:18:45 -0800 Subject: Update style --- packages/connect/src/http_client.ts | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'packages/connect/src/http_client.ts') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 15c613923..d70e5ace4 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -48,13 +48,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( '/token_pairs', HttpRequestType.Get, - relayerResponseJsonParsers.parseTokenPairsJson, requestOpts, ); - return result; + const tokenPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson); + return tokenPairs; } /** * Retrieve orders from the API @@ -68,13 +68,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( `/orders`, HttpRequestType.Get, - relayerResponseJsonParsers.parseOrdersJson, requestOpts, ); - return result; + const orders = relayerResponseJsonParsers.parseOrdersJson(responseJson); + return orders; } /** * Retrieve a specific order from the API @@ -83,12 +83,12 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const result = await this._requestAsync( + const responseJson = await this._requestAsync( `/order/${orderHash}`, HttpRequestType.Get, - relayerResponseJsonParsers.parseOrderJson, ); - return result; + const order = relayerResponseJsonParsers.parseOrderJson(responseJson); + return order; } /** * Retrieve an orderbook from the API @@ -100,13 +100,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( '/orderbook', HttpRequestType.Get, - relayerResponseJsonParsers.parseOrderbookResponseJson, requestOpts, ); - return result; + const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(responseJson); + return orderbook; } /** * Retrieve fee information from the API @@ -118,13 +118,13 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( '/fees', HttpRequestType.Post, - relayerResponseJsonParsers.parseFeesResponseJson, requestOpts, ); - return result; + const fees = relayerResponseJsonParsers.parseFeesResponseJson(responseJson); + return fees; } /** * Submit a signed order to the API @@ -138,13 +138,10 @@ export class HttpClient implements Client { await this._requestAsync( '/order', HttpRequestType.Post, - _.noop, requestOpts, ); } - private async _requestAsync(path: string, requestType: HttpRequestType, - jsonParser: (json: any) => T, - requestOptions?: HttpRequestOptions): Promise { + private async _requestAsync(path: string, requestType: HttpRequestType, requestOptions?: HttpRequestOptions): Promise { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; @@ -166,6 +163,6 @@ export class HttpClient implements Client { throw Error(response.statusText); } const json = await response.json(); - return jsonParser(json); + return json; } } -- cgit v1.2.3 From eb760aa33f0e158b3d26058aa1e9906a1f400a14 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 9 Jan 2018 14:19:33 -0800 Subject: Prettier --- packages/connect/src/http_client.ts | 41 ++++++++++--------------------------- 1 file changed, 11 insertions(+), 30 deletions(-) (limited to 'packages/connect/src/http_client.ts') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index d70e5ace4..5604a9607 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -48,11 +48,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const responseJson = await this._requestAsync( - '/token_pairs', - HttpRequestType.Get, - requestOpts, - ); + const responseJson = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); const tokenPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson); return tokenPairs; } @@ -68,11 +64,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const responseJson = await this._requestAsync( - `/orders`, - HttpRequestType.Get, - requestOpts, - ); + const responseJson = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts); const orders = relayerResponseJsonParsers.parseOrdersJson(responseJson); return orders; } @@ -83,10 +75,7 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const responseJson = await this._requestAsync( - `/order/${orderHash}`, - HttpRequestType.Get, - ); + const responseJson = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get); const order = relayerResponseJsonParsers.parseOrderJson(responseJson); return order; } @@ -100,11 +89,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const responseJson = await this._requestAsync( - '/orderbook', - HttpRequestType.Get, - requestOpts, - ); + const responseJson = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts); const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(responseJson); return orderbook; } @@ -118,11 +103,7 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const responseJson = await this._requestAsync( - '/fees', - HttpRequestType.Post, - requestOpts, - ); + const responseJson = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts); const fees = relayerResponseJsonParsers.parseFeesResponseJson(responseJson); return fees; } @@ -135,13 +116,13 @@ export class HttpClient implements Client { const requestOpts = { payload: signedOrder, }; - await this._requestAsync( - '/order', - HttpRequestType.Post, - requestOpts, - ); + await this._requestAsync('/order', HttpRequestType.Post, requestOpts); } - private async _requestAsync(path: string, requestType: HttpRequestType, requestOptions?: HttpRequestOptions): Promise { + private async _requestAsync( + path: string, + requestType: HttpRequestType, + requestOptions?: HttpRequestOptions, + ): Promise { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; -- cgit v1.2.3