aboutsummaryrefslogblamecommitdiffstats
path: root/packages/connect/src/http_client.ts
blob: 2cabaae76f0b8cb0abbcf6e83d0aa616b1107499 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                                                  

                            
                                            
 
                                                             



                 

                       

                      

                      
                
                   
                          
                 
                                                                                   
 
                                      




                                                      




                                                                                                
                                    

                                             
                                                                                    



                                         
                                                                                                  


                                            

                                                                                                                            

                                                                    



                                                                                                                        
         

                                                                            
          
                                                                                                            

                                                                                        


                                   

                                                                                                                                  

                                                                 



                                                                                                             
         

                                                                            
          
                                                                                                       

                                                                                







                                                                                    
                                                                                                  

                                                                              


                                         

                                                                                                                                         

                                                                        









                                                                                                         
          
                                                                                                          

                                                                                              






                                                                                           

                                                                                        

                             
                                                                                                      

                                                                                    









                                                                                          
                                                                              
     




                                            






                                                                    
                                                             




                                               
                                          

                    
                                           
                           



                                                                                                                      
         
                    

     
import { assert } from '@0xproject/assert';
import { schemas } from '@0xproject/json-schemas';
import 'isomorphic-fetch';
import * as _ from 'lodash';
import * as queryString from 'query-string';

import { schemas as clientSchemas } from './schemas/schemas';
import {
    Client,
    FeesRequest,
    FeesResponse,
    HttpRequestOptions,
    HttpRequestType,
    OrderbookRequest,
    OrderbookResponse,
    OrdersRequestOpts,
    PagedRequestOpts,
    SignedOrder,
    TokenPairsItem,
    TokenPairsRequestOpts,
} from './types';
import { relayerResponseJsonParsers } from './utils/relayer_response_json_parsers';

const TRAILING_SLASHES_REGEX = /\/+$/;
const DEFAULT_PAGED_REQUEST_OPTS: PagedRequestOpts = {
    page: 0,
    per_page: 100,
};

/**
 * This class includes all the functionality related to interacting with a set of HTTP endpoints
 * that implement the standard relayer API v0
 */
export class HttpClient implements Client {
    private _apiEndpointUrl: string;
    /**
     * Instantiates a new HttpClient instance
     * @param   url    The relayer API base HTTP url you would like to interact with
     * @return  An instance of HttpClient
     */
    constructor(url: string) {
        assert.isHttpUrl('url', url);
        this._apiEndpointUrl = url.replace(TRAILING_SLASHES_REGEX, ''); // remove trailing slashes
    }
    /**
     * Retrieve token pair info from the API
     * @param   requestOpts     An optional (TokenPairsRequestOpts & PagedRequestOpts) instance describing token information
     *                          to retrieve with page information, defaults to { page: 0, per_page: 100 }
     * @return  The resulting TokenPairsItems that match the request
     */
    public async getTokenPairsAsync(requestOpts?: TokenPairsRequestOpts & PagedRequestOpts): Promise<TokenPairsItem[]> {
        if (!_.isUndefined(requestOpts)) {
            assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.tokenPairsRequestOptsSchema);
            assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema);
        }
        const httpRequestOpts = {
            params: _.defaults({}, requestOpts, DEFAULT_PAGED_REQUEST_OPTS),
        };
        const responseJson = await this._requestAsync('/token_pairs', HttpRequestType.Get, httpRequestOpts);
        const tokenPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson);
        return tokenPairs;
    }
    /**
     * Retrieve orders from the API
     * @param   requestOpts     An optional (OrdersRequestOpts & PagedRequestOpts) instance describing specific orders to retrieve
     *                          with page information, defaults to { page: 0, per_page: 100 }
     * @return  The resulting SignedOrders that match the request
     */
    public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise<SignedOrder[]> {
        if (!_.isUndefined(requestOpts)) {
            assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.ordersRequestOptsSchema);
            assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema);
        }
        const httpRequestOpts = {
            params: _.defaults({}, requestOpts, DEFAULT_PAGED_REQUEST_OPTS),
        };
        const responseJson = await this._requestAsync(`/orders`, HttpRequestType.Get, httpRequestOpts);
        const orders = relayerResponseJsonParsers.parseOrdersJson(responseJson);
        return orders;
    }
    /**
     * Retrieve a specific order from the API
     * @param   orderHash     An orderHash generated from the desired order
     * @return  The SignedOrder that matches the supplied orderHash
     */
    public async getOrderAsync(orderHash: string): Promise<SignedOrder> {
        assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
        const responseJson = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get);
        const order = relayerResponseJsonParsers.parseOrderJson(responseJson);
        return order;
    }
    /**
     * Retrieve an orderbook from the API
     * @param   request         An OrderbookRequest instance describing the specific orderbook to retrieve
     * @param   requestOpts     An optional PagedRequestOpts instance describing page information, defaults to { page: 0, per_page: 100 }
     * @return  The resulting OrderbookResponse that matches the 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, httpRequestOpts);
        const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(responseJson);
        return orderbook;
    }
    /**
     * Retrieve fee information from the API
     * @param   request     A FeesRequest instance describing the specific fees to retrieve
     * @return  The resulting FeesResponse that matches the request
     */
    public async getFeesAsync(request: FeesRequest): Promise<FeesResponse> {
        assert.doesConformToSchema('request', request, clientSchemas.feesRequestSchema);
        const httpRequestOpts = {
            payload: request,
        };
        const responseJson = await this._requestAsync('/fees', HttpRequestType.Post, httpRequestOpts);
        const fees = relayerResponseJsonParsers.parseFeesResponseJson(responseJson);
        return fees;
    }
    /**
     * Submit a signed order to the API
     * @param   signedOrder     A SignedOrder instance to submit
     */
    public async submitOrderAsync(signedOrder: SignedOrder): Promise<void> {
        assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
        const requestOpts = {
            payload: signedOrder,
        };
        await this._requestAsync('/order', HttpRequestType.Post, requestOpts);
    }
    private async _requestAsync(
        path: string,
        requestType: HttpRequestType,
        requestOptions?: HttpRequestOptions,
    ): 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 url = `${this._apiEndpointUrl}${path}${query}`;
        const headers = new Headers({
            'content-type': 'application/json',
        });
        const response = await fetch(url, {
            method: requestType,
            body: JSON.stringify(payload),
            headers,
        });
        const json = await response.json();
        if (!response.ok) {
            const errorString = `${response.status} - ${response.statusText}\n${requestType} ${url}\n${JSON.stringify(
                json,
            )}`;
            throw Error(errorString);
        }
        return json;
    }
}