2
2
import json
3
3
import urllib3
4
4
import inspect
5
+ from urllib3 import PoolManager , ProxyManager
5
6
from urllib3 .util .retry import Retry
6
7
from enum import Enum
7
- from typing import Optional , Any , Dict
8
+ from typing import Optional , Any , Dict , Union
8
9
from datetime import datetime
9
10
from importlib .metadata import version , PackageNotFoundError
10
11
from .models .request import RequestOptionBuilder
22
23
23
24
24
25
class BaseClient :
26
+ client : Union [PoolManager , ProxyManager ]
27
+
25
28
def __init__ (
26
29
self ,
27
30
api_key : Optional [str ],
@@ -33,6 +36,7 @@ def __init__(
33
36
verbose : bool ,
34
37
trace : bool ,
35
38
custom_json : Optional [Any ] = None ,
39
+ proxy : Optional [str ] = None ,
36
40
):
37
41
if api_key is None :
38
42
raise AuthError (
@@ -66,15 +70,25 @@ def __init__(
66
70
backoff_factor = 0.1 , # [0.0s, 0.2s, 0.4s, 0.8s, 1.6s, ...]
67
71
)
68
72
69
- # https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
70
- # https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool
71
- self .client = urllib3 .PoolManager (
72
- num_pools = num_pools ,
73
- headers = self .headers , # default headers sent with each request.
74
- ca_certs = certifi .where (),
75
- cert_reqs = "CERT_REQUIRED" ,
76
- retries = retry_strategy , # use the customized Retry instance
77
- )
73
+ if proxy :
74
+ self .client = urllib3 .ProxyManager (
75
+ proxy_url = proxy , # e.g., "http://proxy.example.com"
76
+ num_pools = num_pools ,
77
+ headers = self .headers ,
78
+ ca_certs = certifi .where (),
79
+ cert_reqs = "CERT_REQUIRED" ,
80
+ retries = retry_strategy ,
81
+ )
82
+ else :
83
+ # https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
84
+ # https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool
85
+ self .client = urllib3 .PoolManager (
86
+ num_pools = num_pools ,
87
+ headers = self .headers ,
88
+ ca_certs = certifi .where (),
89
+ cert_reqs = "CERT_REQUIRED" ,
90
+ retries = retry_strategy ,
91
+ )
78
92
79
93
self .timeout = urllib3 .Timeout (connect = connect_timeout , read = read_timeout )
80
94
@@ -102,6 +116,12 @@ def _get(
102
116
103
117
headers = self ._concat_headers (option .headers )
104
118
119
+ # Check if path is a full URL or a relative path
120
+ if path .startswith ("http" ):
121
+ url = path
122
+ else :
123
+ url = self .BASE + path
124
+
105
125
if self .trace :
106
126
full_url = f"{ self .BASE } { path } "
107
127
if params :
@@ -228,7 +248,7 @@ def _paginate_iter(
228
248
for t in decoded [result_key ]:
229
249
yield deserializer (t )
230
250
if "next_url" in decoded :
231
- path = decoded ["next_url" ]. replace ( self . BASE , "" )
251
+ path = decoded ["next_url" ] # Use full next_url
232
252
params = {}
233
253
else :
234
254
return
0 commit comments