Skip to main content

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

Project description

py_httpx_toolkit

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

功能特性

  • 🚀 支持同步和异步 HTTP 客户端
  • ⚙️ 支持预配置默认参数
  • 🔄 支持复用已有的客户端实例
  • 📡 支持统一的请求接口,自动管理客户端生命周期

安装方式

使用 pip

pip install py-httpx-toolkit

使用 uv

uv add py-httpx-toolkit

快速开始

同步请求

from py_httpx_toolkit import Httpx

# 创建 HTTP 客户端实例
http = Httpx(client_kwargs={"timeout": 30})

# 发起同步请求
response = http.request(method='GET', url='https://api.example.com/data')
print(response.status_code)
print(response.json())

异步请求

import asyncio
from py_httpx_toolkit import Httpx

async def main():
    http = Httpx()
    
    # 发起异步请求
    response = await http.async_request(method='GET', url='https://api.example.com/data')
    print(response.status_code)
    print(response.json())

asyncio.run(main())

API 文档

Httpx 类

初始化

http = Httpx(client_kwargs=None)
  • client_kwargs: 客户端的默认配置参数,如 headers、timeout 等

创建同步客户端

client = http.client(**kwargs)

返回 httpx.Client 实例

创建异步客户端

async_client = http.async_client(**kwargs)

返回 httpx.AsyncClient 实例

同步请求

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

异步请求

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

responses 模块

Base 模型

from py_httpx_toolkit.responses import Base

# 创建响应模型实例
result = Base(**response.json())

用于将 HTTP 响应转换为 Pydantic 模型,支持额外字段。

项目结构

py_httpx_toolkit/
├── src/
│   └── py_httpx_toolkit/
│       ├── __init__.py      # Httpx 客户端封装
│       └── responses.py     # 响应模型定义
├── .venv/                   # 虚拟环境
├── .gitignore              # Git 忽略配置
└── README.md               # 项目文档

依赖

  • httpx >= 0.27.0
  • pydantic >= 2.0.0

项目主页

https://gitee.com/guolei19850528/py_httpx_toolkit

作者

郭磊

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.6.tar.gz (4.9 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.6-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for py_httpx_toolkit-1.0.6.tar.gz
Algorithm Hash digest
SHA256 4118c417a783ddd03ce83a4769931bead7405a153d25fff315b100664968473b
MD5 0296b096e9315b74b2ff066ec63895c6
BLAKE2b-256 3a8d500063774717efb8e3898a3e55e19dcc2e7e7e72fffc4c5e44fffcb805da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_httpx_toolkit-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 efaf188232da20e6c47f0dd1c97367b06e6285f887625ed10de61faa0320b350
MD5 d7b3c29a9d33084c94c22bf89a0f925e
BLAKE2b-256 95298d3976c25d346b8611c98905f8afdf2a07345547574d0ce43edbfa10c34a

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