diff options
author | Fabio Berger <me@fabioberger.com> | 2018-02-16 11:09:53 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-02-16 11:09:53 +0800 |
commit | e2b51c5dc46b30b21e0561689de1f9a3d0127554 (patch) | |
tree | c8f93b5700961a46b16e49e52d159c989c11102d /packages/connect/src/http_client.ts | |
parent | b610b7c1923dcc56883c5167393122ceaed26708 (diff) | |
parent | b75fdd6b66a30196d53331827733b863bed770f0 (diff) | |
download | dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.tar dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.tar.gz dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.tar.bz2 dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.tar.lz dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.tar.xz dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.tar.zst dexon-sol-tools-e2b51c5dc46b30b21e0561689de1f9a3d0127554.zip |
Merge branch 'development' of github.com:0xProject/0x.js into development
* 'development' of github.com:0xProject/0x.js: (24 commits)
Rename variables
Update CHANGELOG
Add npm config for contracts list
Run prettier
Fix checks, add contract list to compile script in package.json
Add contracts to compiler options
Add missing public types from connect docs
Change imports order
Change default page params in connect to page 1 and perPage 100
Add docs staging to 0x.js package
Fix a typo
Add an assertion
Add PR numbers
Fix entry points
Add tests for dev-utils package
Move subproviders from dev-utils to subproviders
Add missing CHANGELOG entry
Add support for intersection types in docs
Add stagedocs script to connect package
web3 typings fix - web3.net.peerCount returns number
...
Diffstat (limited to 'packages/connect/src/http_client.ts')
-rw-r--r-- | packages/connect/src/http_client.ts | 94 |
1 files changed, 63 insertions, 31 deletions
diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 3df77b0f0..9e5f0f448 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -13,14 +13,26 @@ import { HttpRequestType, OrderbookRequest, OrderbookResponse, - OrdersRequest, + OrdersRequestOpts, + PagedRequestOpts, SignedOrder, TokenPairsItem, - TokenPairsRequest, + TokenPairsRequestOpts, } from './types'; import { relayerResponseJsonParsers } from './utils/relayer_response_json_parsers'; const TRAILING_SLASHES_REGEX = /\/+$/; +const DEFAULT_PAGED_REQUEST_OPTS: PagedRequestOpts = { + page: 1, + perPage: 100, +}; +/** + * This mapping defines how an option property name gets converted into an HTTP request query field + */ +const OPTS_TO_QUERY_FIELD_MAP = { + perPage: 'per_page', +}; + /** * This class includes all the functionality related to interacting with a set of HTTP endpoints * that implement the standard relayer API v0 @@ -28,6 +40,22 @@ const TRAILING_SLASHES_REGEX = /\/+$/; export class HttpClient implements Client { private _apiEndpointUrl: string; /** + * Format parameters to be appended to http requests into query string form + */ + private static _buildQueryStringFromHttpParams(params?: object): string { + // if params are undefined or empty, return an empty string + if (_.isUndefined(params) || _.isEmpty(params)) { + return ''; + } + // format params into a form the api expects + const formattedParams = _.mapKeys(params, (value: any, key: string) => { + return _.get(OPTS_TO_QUERY_FIELD_MAP, key, key); + }); + // stringify the formatted object + const stringifiedParams = queryString.stringify(formattedParams); + return `?${stringifiedParams}`; + } + /** * Instantiates a new HttpClient instance * @param url The relayer API base HTTP url you would like to interact with * @return An instance of HttpClient @@ -38,34 +66,35 @@ export class HttpClient implements Client { } /** * Retrieve token pair info from the API - * @param request A TokenPairsRequest instance describing specific token information - * to retrieve + * @param requestOpts Options specifying token information to retrieve and page information, defaults to { page: 1, perPage: 100 } * @return The resulting TokenPairsItems that match the request */ - public async getTokenPairsAsync(request?: TokenPairsRequest): Promise<TokenPairsItem[]> { - if (!_.isUndefined(request)) { - assert.doesConformToSchema('request', request, clientSchemas.relayerTokenPairsRequestSchema); + public async getTokenPairsAsync(requestOpts?: TokenPairsRequestOpts & PagedRequestOpts): Promise<TokenPairsItem[]> { + if (!_.isUndefined(requestOpts)) { + assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.tokenPairsRequestOptsSchema); + assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema); } - const requestOpts = { - params: request, + const httpRequestOpts = { + params: _.defaults({}, requestOpts, DEFAULT_PAGED_REQUEST_OPTS), }; - const responseJson = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); + const responseJson = await this._requestAsync('/token_pairs', HttpRequestType.Get, httpRequestOpts); const tokenPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson); return tokenPairs; } /** * Retrieve orders from the API - * @param request An OrdersRequest instance describing specific orders to retrieve + * @param requestOpts Options specifying orders to retrieve and page information, defaults to { page: 1, perPage: 100 } * @return The resulting SignedOrders that match the request */ - public async getOrdersAsync(request?: OrdersRequest): Promise<SignedOrder[]> { - if (!_.isUndefined(request)) { - assert.doesConformToSchema('request', request, clientSchemas.relayerOrdersRequestSchema); + public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise<SignedOrder[]> { + if (!_.isUndefined(requestOpts)) { + assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.ordersRequestOptsSchema); + assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema); } - const requestOpts = { - params: request, + const httpRequestOpts = { + params: _.defaults({}, requestOpts, DEFAULT_PAGED_REQUEST_OPTS), }; - const responseJson = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts); + const responseJson = await this._requestAsync(`/orders`, HttpRequestType.Get, httpRequestOpts); const orders = relayerResponseJsonParsers.parseOrdersJson(responseJson); return orders; } @@ -82,15 +111,22 @@ export class HttpClient implements Client { } /** * Retrieve an orderbook from the API - * @param request An OrderbookRequest instance describing the specific orderbook to retrieve + * @param request An OrderbookRequest instance describing the specific orderbook to retrieve + * @param requestOpts Options specifying page information, defaults to { page: 1, perPage: 100 } * @return The resulting OrderbookResponse that matches the request */ - public async getOrderbookAsync(request: OrderbookRequest): Promise<OrderbookResponse> { - assert.doesConformToSchema('request', request, clientSchemas.relayerOrderBookRequestSchema); - const requestOpts = { - params: request, + public async getOrderbookAsync( + request: OrderbookRequest, + requestOpts?: PagedRequestOpts, + ): Promise<OrderbookResponse> { + assert.doesConformToSchema('request', request, clientSchemas.orderBookRequestSchema); + if (!_.isUndefined(requestOpts)) { + assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema); + } + const httpRequestOpts = { + params: _.defaults({}, request, requestOpts, DEFAULT_PAGED_REQUEST_OPTS), }; - const responseJson = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts); + const responseJson = await this._requestAsync('/orderbook', HttpRequestType.Get, httpRequestOpts); const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(responseJson); return orderbook; } @@ -100,11 +136,11 @@ export class HttpClient implements Client { * @return The resulting FeesResponse that matches the request */ public async getFeesAsync(request: FeesRequest): Promise<FeesResponse> { - assert.doesConformToSchema('request', request, schemas.relayerApiFeesPayloadSchema); - const requestOpts = { + assert.doesConformToSchema('request', request, clientSchemas.feesRequestSchema); + const httpRequestOpts = { payload: request, }; - const responseJson = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts); + const responseJson = await this._requestAsync('/fees', HttpRequestType.Post, httpRequestOpts); const fees = relayerResponseJsonParsers.parseFeesResponseJson(responseJson); return fees; } @@ -126,11 +162,7 @@ export class HttpClient implements Client { ): Promise<any> { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); - let query = ''; - if (!_.isUndefined(params) && !_.isEmpty(params)) { - const stringifiedParams = queryString.stringify(params); - query = `?${stringifiedParams}`; - } + const query = HttpClient._buildQueryStringFromHttpParams(params); const url = `${this._apiEndpointUrl}${path}${query}`; const headers = new Headers({ 'content-type': 'application/json', |