Home > Software engineering >  How to get the data out of `requests.Response` object in Python?
How to get the data out of `requests.Response` object in Python?

Time:01-04

  • I am downloading data from the cryptocurrency exchange enter image description here

    CodePudding user response:

    The source code for this library shows two returned attributes: data and headers

    CodePudding user response:

    The naming used in the dydx3 library is a bit confusing, dydx3.helpers.requests is not the requests module.

    The dydx3.helpers.requests.Response object only has 2 attributes:

    • headers which contains the HTTP headers of the response from the API
    • data which contains a dict either empty or containing the json data returned by the API.

    So all you have to do in your code to get the data is:

    from dydx3.helpers.request_helpers import generate_query_path
    from dydx3.helpers.requests import request
    
    class Public(object):
    
        def __init__(
            self,
            host,
        ):
            self.host = host
    
        # ============ Request Helpers ============
    
        def _get(self, request_path, params={}):
            return request(
                generate_query_path(self.host   request_path, params),
                'get',
            )
    
        def _put(self, endpoint, data):
            return request(
                self.host   '/v3/'   endpoint,
                'put',
                {},
                data,
            )
    
        # ============ Requests ============
    
        def get_historical_funding(self, market, effective_before_or_at=None):
            '''
            Get historical funding for a market
            :param market: required
            :type market: str in list [
                "BTC-USD",
                "ETH-USD",
                "LINK-USD",
                ...
            ]
            :param effective_before_or_at: optional
            :type effective_before_or_at: str
            :returns: Array of historical funding for a specific market
            :raises: DydxAPIError
            '''
            uri = '/'.join(['/v3/historical-funding', market])
            return self._get(
                uri,
                {'effectiveBeforeOrAt': effective_before_or_at},
            ).data  # CHANGE MADE HERE
    
  • Related