Skip to main content

一个对 httpx 库的轻量级封装,简化了 HTTP 客户端的创建和使用,支持同步和异步两种请求模式。A lightweight wrapper for the httpx library that simplifies HTTP client creation and usage, supporting both synchronous and asynchronous request modes.

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.0.tar.gz (5.0 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.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for py_httpx_toolkit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 abbbdd5764a813f52a1f6efdd51bfdce5f8012bbc54a094a60944e1cba21f8d3
MD5 b0f1bbc1ef4f62cdaaa9a2f8fdf9f863
BLAKE2b-256 fc7838443254390d5e25ac7610741e93fc7f09d739a75817c503ceca345e7ec0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_httpx_toolkit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e88630e370e8d011edeefc44e623d762a2a4b30762126f6ffd8635c205a99cc1
MD5 ffa44124ffa5008b24f4ee46082ef5d0
BLAKE2b-256 68a9da249edc0cf931f3dc47101265841f529d3af52e6bf095beae71b99f164f

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