当前位置: 首页>>代码示例>>Python>>正文


Python httpx.Response方法代码示例

本文整理汇总了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
        ) 
开发者ID:sudoguy,项目名称:tiktok_bot,代码行数:18,代码来源:client.py

示例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 
开发者ID:sudoguy,项目名称:tiktok_bot,代码行数:20,代码来源:client.py

示例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) 
开发者ID:foxmask,项目名称:joplin-api,代码行数:20,代码来源:core.py

示例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') 
开发者ID:maximdanilchenko,项目名称:aiochclient,代码行数:4,代码来源:httpx.py

示例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 
开发者ID:Mergifyio,项目名称:mergify-engine,代码行数:14,代码来源:httpx_vcr_stubs.py

示例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
  ) 
开发者ID:iotaledger,项目名称:iota.py,代码行数:12,代码来源:adapter_test.py

示例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,
      },
    ) 
开发者ID:iotaledger,项目名称:iota.py,代码行数:42,代码来源:adapter_test.py

示例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 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:8,代码来源:items.py

示例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) 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:9,代码来源:items.py

示例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) 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:5,代码来源:items.py

示例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 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:12,代码来源:pipelines.py

示例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 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:17,代码来源:ant.py

示例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) 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:13,代码来源:test_pipelines.py

示例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() 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:41,代码来源:test_ant.py

示例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 
开发者ID:jmagnusson,项目名称:netsuite,代码行数:37,代码来源:rest_api.py


注:本文中的httpx.Response方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。