本文整理匯總了Python中httpx.Response方法的典型用法代碼示例。如果您正苦於以下問題:Python httpx.Response方法的具體用法?Python httpx.Response怎麽用?Python httpx.Response使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類httpx
的用法示例。
在下文中一共展示了httpx.Response方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def __init__(
self,
base_url: str,
default_headers: Optional[dict] = None,
default_params: Optional[dict] = None,
history_len: int = 30,
):
self.base_url = base_url
self.default_headers = default_headers or {}
self.default_params = default_params or {}
self.history: Deque[Response] = deque(maxlen=history_len)
self.http_client = Client(
base_url=self.base_url, headers=default_headers, params=self.default_params
)
示例2: get
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def get(self, url: str, params: dict, headers: Optional[dict] = None):
custom_headers = headers or {}
all_params = {**self._generate_params(), **params}
logger.debug(f"Sending request to {url}", params=all_params, custom_headers=custom_headers)
response = self.http_client.get(url=url, params=all_params, headers=custom_headers)
self.history.append(response)
body = response.text or "is empty!"
logger.debug(f"Response return status_code: {response.status_code}, body: {body}")
for cookie_name, cookie_data in response.cookies.items():
self.http_client.cookies.set(cookie_name, cookie_data)
logger.debug(f"New cookies: {dict(response.cookies)}")
return response
示例3: create_resource
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def create_resource(self, resource_file, **props) -> Response:
"""
POST /resources
Add a new resource
:param resource_file: string, name of the resource_file
:param props: dict
:return: res: json result of the post
"""
if 'title' not in props:
raise ValueError('`create_resource` requires `title` in `props` property')
data = {
'filename': os.path.basename(resource_file),
'resource_file': resource_file,
'props': props}
return await self.query('post', '/resources/', **data)
示例4: _read_error_body
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def _read_error_body(resp: Response):
return (await resp.aread()).decode(errors='replace')
示例5: _from_serialized_response
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def _from_serialized_response(request, serialized_response, history=None):
content = serialized_response.get("content").encode()
response = httpx.Response(
status_code=serialized_response.get("status_code"),
request=request,
http_version=serialized_response.get("http_version"),
headers=_from_serialized_headers(serialized_response.get("headers")),
content=content,
history=history or [],
)
response._content = content
return response
示例6: create_http_response
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def create_http_response(content, status=200):
# type: (Text, int) -> httpx.Response
"""
Creates an HTTP Response object for a test.
"""
return httpx.Response(
status,
request=httpx.Request('post','https://localhost:14265/'),
content=content
)
示例7: test_trytes_in_request
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def test_trytes_in_request(self):
"""
Sending a request that includes trytes.
"""
adapter = HttpAdapter('http://localhost:14265')
# Response is not important for this test; we just need to make
# sure that the request is converted correctly.
mocked_sender = mock.Mock(return_value=async_return(create_http_response('{}')))
with mock.patch.object(adapter, '_send_http_request', mocked_sender):
await adapter.send_request({
'command': 'helloWorld',
'trytes': [
TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA'),
TryteString(
b'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA',
),
],
})
mocked_sender.assert_called_once_with(
url = adapter.node_url,
payload = json.dumps({
'command': 'helloWorld',
# Tryte sequences are converted to strings for transport.
'trytes': [
'RBTC9D9DCDQAEASBYBCCKBFA',
'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA',
],
}),
headers = {
'Content-type': 'application/json',
'X-IOTA-API-Version': API_VERSION,
},
)
示例8: extract
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def extract(self, res: httpx.Response) -> Item:
item = self.item_cls()
for key, extractor in self.extractors.items():
set_value(item, key, extractor(res))
return item
示例9: __init__
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def __init__(
self,
item_class: typing.Type[Item],
root_extractor: typing.Callable[[httpx.Response], typing.Sequence],
):
self.root_extractor = root_extractor
super().__init__(item_class)
示例10: extract_items
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def extract_items(self, res: httpx.Response) -> typing.Generator[Item, None, None]:
for node in self.root_extractor(res):
yield super().extract(node)
示例11: process
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def process(self, obj: typing.Any) -> typing.Any:
"""Process objs, this method can be coroutine function
Raise Dropped when drop one obj
:raise Dropped
"""
return obj
# Response pipelines
示例12: _pipe
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def _pipe(
self,
obj: typing.Union[Item, httpx.Request, httpx.Response],
pipelines: typing.List[Pipeline],
) -> typing.Any:
self.logger.debug("Process obj: " + str(obj))
raw_obj = obj
for pipeline in pipelines:
try:
obj = await utils.run_cor_func(pipeline.process, obj)
except Exception as e:
if isinstance(e, Dropped):
self.reporter.report(raw_obj, dropped=True)
raise e
return obj
示例13: test_response_filter_error_pipeline
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def test_response_filter_error_pipeline():
pl = pls.ResponseFilterErrorPipeline()
res = httpx.Response(
200, request=httpx.Request("Get", "https://test.com"), content=b""
)
err_res = httpx.Response(
403, request=httpx.Request("Get", "https://test.com"), content=b""
)
assert res is pl.process(res)
with pytest.raises(Dropped):
pl.process(err_res)
示例14: test_ant
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def test_ant():
class TestPipeline(Pipeline):
def __init__(self):
super().__init__()
self.count = 0
async def on_spider_open(self):
pass
async def on_spider_close(self):
raise Exception("This exception will be suppressed")
def process(self, obj):
self.count += 1
return obj
class TestAnt(Ant):
item_pipelines = [TestPipeline()]
request_retries = 0
async def run(self):
await self.request("http://test.com")
await self.collect(object())
assert self.item_pipelines[0].count == 1
async def requset(self, *args, **kwargs):
return httpx.Response(
200, request=httpx.Request("Get", "https://test.com"), content=b""
)
ant = TestAnt()
assert ant.name == "TestAnt"
await ant.main()
class Test2Ant(TestAnt):
async def run(self):
raise Exception("This exception will be suppressed")
await Test2Ant().main()
示例15: _raw_request
# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import Response [as 別名]
def _raw_request(
self, method: str, subpath: str, **request_kw
) -> httpx.Response:
method = method.upper()
url = self._make_url(subpath)
headers = {**self._make_default_headers(), **request_kw.pop("headers", {})}
auth = request_kw.pop("auth", self._auth)
timeout = request_kw.pop("timeout", self._default_timeout)
if "json" in request_kw:
request_kw["data"] = json.dumps_str(request_kw.pop("json"))
kw = {**request_kw}
logger.debug(
f"Making {method.upper()} request to {url}. Keyword arguments: {kw}"
)
async with self._request_semaphore:
async with httpx.AsyncClient() as c:
resp = await c.request(
method=method,
url=url,
headers=headers,
auth=auth,
timeout=timeout,
**kw,
)
resp_headers_json = json.dumps_str(dict(resp.headers))
logger.debug(
f"Got response headers from NetSuite REST API: {resp_headers_json}"
)
return resp