一个对 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_httpx_toolkit-1.0.3.tar.gz.
File metadata
- Download URL: py_httpx_toolkit-1.0.3.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
485a41ed141b0e15d37b4b3e90f69f40059c7f8389c0a29f84c8d1b83f272515
|
|
| MD5 |
283e9beedba80daadd5ad65cbcf0c395
|
|
| BLAKE2b-256 |
282744c3611c12ba174b09d0e189f8ffe169f001398b451e549095e720041b87
|
File details
Details for the file py_httpx_toolkit-1.0.3-py3-none-any.whl.
File metadata
- Download URL: py_httpx_toolkit-1.0.3-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d0c63667215034d44809ee2b84594f112ba57d57a7d1c7faaacf5c5405d1575
|
|
| MD5 |
cb916f01728d4766bf2b9b44e5a64b5c
|
|
| BLAKE2b-256 |
0b01d192a54fb558b192d3fbe7da3bb1577e250536d3e0407490f1d3c2a187a1
|