[英]How to make a Python requests.Response object byte-like?
[英]How to get the data out of `requests.Response` object in Python?
<dydx3.helpers.requests.Response at 0x1880b5a02b0>
。requests.Response
object 的内容,我使用了.text
方法。 但是 output 是一条错误消息,说AttributeError: 'Response' object has no attribute 'text'
.json()
也不起作用,因为 output 说AttributeError: 'Response' object has no attribute 'json'
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},
)
client = Public(host="https://api.dydx.exchange")
data = client.get_historical_funding("BTC-USD")
data
>>> <dydx3.helpers.requests.Response at 0x1880b5a02b0>
data.text
>>> AttributeError: 'Response' object has no attribute 'text'
host="https://api.dydx.exchange"
初始化客户端,因为根据API 文档,主机是 ZDB974238714CA8DE634A7CE1D083A14Z 端点的基础 URL。 该库的源代码显示了两个返回的属性: data
和headers
dydx3
库中使用的命名有点混乱, dydx3.helpers.requests
不是requests
模块。
dydx3.helpers.requests.Response
object 只有两个属性:
headers
的标头dict
或包含 API 返回的 json data
的数据。因此,您在代码中获取数据所需要做的就是:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.