當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。