Skip to main content

一个对 httpx 库的轻量级封装,简化了 HTTP 客户端的创建和使用,支持同步和异步两种请求模式。

Project description

py-httpx-toolkit

English | 中文

一个对 httpx 库的轻量级封装,简化了 HTTP 客户端的创建和使用,支持同步和异步两种请求模式。

功能特点

  • 支持同步和异步 HTTP 请求
  • 支持预配置客户端参数
  • 支持复用已有的客户端实例
  • 自动管理客户端生命周期

安装

pip install py-httpx-toolkit

或使用 uv:

uv add py-httpx-toolkit

快速开始

基本使用

from py_httpx_toolkit import Httpx

# 创建 Httpx 实例
httpx_inst = Httpx()

# 同步请求
response = httpx_inst.request(method='GET', url='https://example.com')
print(response.status_code)
print(response.text)

# 异步请求
import asyncio


async def fetch():
    response = await httpx_inst.async_request(method='GET', url='https://example.com')
    print(response.status_code)
    print(response.text)


asyncio.run(fetch())

预配置客户端

from py_httpx_toolkit import Httpx

# 创建带有默认配置的 Httpx 实例
httpx_inst = Httpx(client_kwargs={
    'headers': {'Authorization': 'Bearer token'},
    'timeout': 30,
    'follow_redirects': True
})

# 使用默认配置发起请求
response = httpx_inst.request(method='GET', url='https://api.example.com/data')

使用自定义客户端

from py_httpx_toolkit import Httpx
import httpx

httpx_inst = Httpx()

# 创建自定义客户端
client = httpx.Client(headers={'X-Custom': 'value'})

# 使用自定义客户端发起请求
response = httpx_inst.request(client=client, method='GET', url='https://example.com')

API 文档

Httpx 类

初始化

Httpx(client_kwargs: Optional[dict] = None)

创建 Httpx 实例,可传入默认客户端配置。

参数:

  • client_kwargs: 客户端的默认配置参数,如 headers、timeout 等

client()

client(**kwargs) -> httpx.Client

创建同步 HTTP 客户端。

参数:

  • **kwargs: 额外的客户端配置参数,会覆盖默认配置

返回:

  • httpx.Client: 配置好的同步 HTTP 客户端实例

async_client()

async_client(**kwargs) -> httpx.AsyncClient

创建异步 HTTP 客户端。

参数:

  • **kwargs: 额外的客户端配置参数,会覆盖默认配置

返回:

  • httpx.AsyncClient: 配置好的异步 HTTP 客户端实例

request()

request(
    client: Optional[httpx.Client] = None,
    client_kwargs: Optional[dict] = None,
    **kwargs
) -> httpx.Response

发起同步 HTTP 请求。

参数:

  • client: 已有的同步客户端实例,可选
  • client_kwargs: 创建新客户端时的配置参数,可选
  • **kwargs: 请求参数,如 method、url、headers、data、json 等

返回:

  • httpx.Response: HTTP 响应对象

async_request()

async_request(
    client: Optional[httpx.AsyncClient] = None,
    client_kwargs: Optional[dict] = None,
    **kwargs
) -> httpx.Response

发起异步 HTTP 请求。

参数:

  • client: 已有的异步客户端实例,可选
  • client_kwargs: 创建新客户端时的配置参数,可选
  • **kwargs: 请求参数,如 method、url、headers、data、json 等

返回:

  • httpx.Response: HTTP 响应对象

依赖

该项目依赖以下 Python 包:

依赖包 版本要求 说明
httpx ≥ 0.27.0 高性能 HTTP 客户端库,支持同步和异步请求

项目主页

https://gitee.com/guolei19850528/py_httpx_toolkit

作者

Author: 郭磊

Email: 174000902@qq.com

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_httpx_toolkit-1.0.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_httpx_toolkit-1.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file py_httpx_toolkit-1.0.2.tar.gz.

File metadata

  • Download URL: py_httpx_toolkit-1.0.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.11

File hashes

Hashes for py_httpx_toolkit-1.0.2.tar.gz
Algorithm Hash digest
SHA256 5c46cb25873f3e0251795366bea1e33f38e6f5aa383c821744af040739f215d8
MD5 1f90f27f5a60708ff647e14737088c63
BLAKE2b-256 b4498580c22266a22d7c5b12fcddf81a18cdd740c313ef7c18a72ed9667935bc

See more details on using hashes here.

File details

Details for the file py_httpx_toolkit-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for py_httpx_toolkit-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fd10030a086d56e26db077422f2350377c7bcd8f347aede5c1eff8862aecff5
MD5 52d6ad2b7d15633b8431d6d5a06b1fc0
BLAKE2b-256 bf968e2abafaba87d6ba26be8adec139a8e33c8ee56af01c9dca4f8d11bcf954

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page