File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,12 +60,14 @@ ab_request/http_client/
6060### 基本使用
6161
6262``` python
63- from ab_request.http_client import BaseClient, JSONResponseParser
63+ from http_client import BaseClient, JSONResponseParser
64+
6465
6566class MyAPIClient (BaseClient ):
6667 base_url = " https://api.example.com"
6768 response_parser_class = JSONResponseParser
6869
70+
6971# 创建客户端实例
7072client = MyAPIClient()
7173
@@ -93,12 +95,14 @@ results = client.request([
9395### 使用缓存
9496
9597``` python
96- from ab_request.http_client import CacheClientMixin
98+ from http_client import CacheClientMixin
99+
97100
98101class CachedAPIClient (CacheClientMixin , BaseClient ):
99102 base_url = " https://api.example.com"
100103 default_cache_expire = 300 # 5分钟
101104
105+
102106client = CachedAPIClient()
103107
104108# 第一次请求会缓存
@@ -134,7 +138,7 @@ class AuthenticatedClient(BaseClient):
134138
135139## 📚 更多示例
136140
137- 查看 [ examples.py] ( ./ examples.py) 获取更多使用示例。
141+ 查看 [ examples.py] ( examples.py ) 获取更多使用示例。
138142
139143运行示例:
140144``` bash
Original file line number Diff line number Diff line change 1212 - 缓存: CacheClientMixin, InMemoryCacheBackend, RedisCacheBackend
1313
1414使用示例:
15- >>> from ab_request. http_client import BaseClient, JSONResponseParser
15+ >>> from http_client import BaseClient, JSONResponseParser
1616 >>>
1717 >>> class MyAPIClient(BaseClient):
1818 ... base_url = "https://api.example.com"
2323"""
2424
2525# 核心客户端
26- from ab_request . http_client .client import BaseClient
26+ from http_client .client import BaseClient
2727
2828# 异常类
29- from ab_request . http_client .exceptions import (
29+ from http_client .exceptions import (
3030 APIClientError ,
3131 APIClientHTTPError ,
3232 APIClientNetworkError ,
3636)
3737
3838# 响应解析器
39- from ab_request . http_client .parser import (
39+ from http_client .parser import (
4040 BaseResponseParser ,
4141 ContentResponseParser ,
4242 FileWriteResponseParser ,
4646)
4747
4848# 响应格式化器
49- from ab_request . http_client .formatter import (
49+ from http_client .formatter import (
5050 BaseResponseFormatter ,
5151 DefaultResponseFormatter ,
5252)
5353
5454# 异步执行器
55- from ab_request . http_client .async_executor import (
55+ from http_client .async_executor import (
5656 BaseAsyncExecutor ,
5757 ThreadPoolAsyncExecutor ,
5858)
5959
6060# 响应验证器
61- from ab_request . http_client .validator import (
61+ from http_client .validator import (
6262 BaseResponseValidator ,
6363 CustomValidator ,
6464 JSONFieldValidator ,
6565 StatusCodeValidator ,
6666)
6767
6868# 缓存支持
69- from ab_request . http_client .cache import (
69+ from http_client .cache import (
7070 BaseCacheBackend ,
7171 CacheClientMixin ,
7272 InMemoryCacheBackend ,
7373 RedisCacheBackend ,
7474)
7575
7676# 常量配置
77- from ab_request . http_client .constants import (
77+ from http_client .constants import (
7878 CACHEABLE_METHODS ,
7979 DEFAULT_CACHE_EXPIRE ,
8080 DEFAULT_MAX_WORKERS ,
Original file line number Diff line number Diff line change 1010from concurrent .futures import Future , ThreadPoolExecutor , as_completed
1111from typing import Any
1212
13- from ab_request . http_client .constants import LOG_FORMAT
14- from ab_request . http_client .exceptions import APIClientError
13+ from http_client .constants import LOG_FORMAT
14+ from http_client .exceptions import APIClientError
1515
1616logging .basicConfig (level = logging .INFO , format = LOG_FORMAT )
1717logger = logging .getLogger (__name__ )
Original file line number Diff line number Diff line change 1717
1818import redis
1919
20- from ab_request . http_client .constants import (
20+ from http_client .constants import (
2121 CACHEABLE_METHODS ,
2222 DEFAULT_CACHE_EXPIRE ,
2323 DEFAULT_CACHE_MAXSIZE ,
Original file line number Diff line number Diff line change 2626ResponseDict : TypeAlias = dict [str , Any ]
2727RequestDataType : TypeAlias = RequestConfig | list [RequestConfig ] | None
2828
29- from ab_request . http_client .constants import (
29+ from http_client .constants import (
3030 DEFAULT_MAX_WORKERS ,
3131 DEFAULT_POOL_CONFIG ,
3232 DEFAULT_RETRIES ,
3535 LOG_FORMAT ,
3636 RESPONSE_CODE_FORMATTING_ERROR ,
3737)
38- from ab_request . http_client .exceptions import (
38+ from http_client .exceptions import (
3939 APIClientError ,
4040 APIClientHTTPError ,
4141 APIClientNetworkError ,
4242 APIClientTimeoutError ,
4343 APIClientValidationError ,
4444)
45- from ab_request . http_client .async_executor import BaseAsyncExecutor , ThreadPoolAsyncExecutor
46- from ab_request . http_client .formatter import BaseResponseFormatter , DefaultResponseFormatter
47- from ab_request . http_client .parser import (
45+ from http_client .async_executor import BaseAsyncExecutor , ThreadPoolAsyncExecutor
46+ from http_client .formatter import BaseResponseFormatter , DefaultResponseFormatter
47+ from http_client .parser import (
4848 BaseResponseParser ,
4949 FileWriteResponseParser ,
5050 JSONResponseParser ,
5151 RawResponseParser ,
5252)
53- from ab_request . http_client .validator import BaseResponseValidator
53+ from http_client .validator import BaseResponseValidator
5454
5555# 配置日志
5656logging .basicConfig (level = logging .INFO , format = LOG_FORMAT )
File renamed without changes.
Original file line number Diff line number Diff line change 11"""
22HTTP 客户端使用示例
33
4- 演示如何使用 ab_request. http_client 模块的各种功能
4+ 演示如何使用 http_client 模块的各种功能
55"""
66
77from requests .auth import AuthBase
88
9- from ab_request . http_client import (
9+ from http_client import (
1010 BaseClient ,
1111 ContentResponseParser ,
1212 JSONResponseParser ,
File renamed without changes.
Original file line number Diff line number Diff line change 1010
1111import requests
1212
13- from ab_request . http_client .constants import (
13+ from http_client .constants import (
1414 LOG_FORMAT ,
1515 RESPONSE_CODE_NON_HTTP_ERROR ,
1616 RESPONSE_CODE_UNEXPECTED_TYPE ,
1717)
18- from ab_request . http_client .exceptions import APIClientError
18+ from http_client .exceptions import APIClientError
1919
2020logging .basicConfig (level = logging .INFO , format = LOG_FORMAT )
2121logger = logging .getLogger (__name__ )
Original file line number Diff line number Diff line change 1111
1212import requests
1313
14- from ab_request . http_client .constants import (
14+ from http_client .constants import (
1515 DEFAULT_CHUNK_SIZE ,
1616 DEFAULT_DOWNLOAD_PATH ,
1717 DEFAULT_FILENAME ,
You can’t perform that action at this time.
0 commit comments