Skip to content

Commit 3231625

Browse files
committed
Adds proxy support
1 parent 0bbcb67 commit 3231625

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

polygon/rest/base.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import json
33
import urllib3
44
import inspect
5+
from urllib3 import PoolManager, ProxyManager
56
from urllib3.util.retry import Retry
67
from enum import Enum
7-
from typing import Optional, Any, Dict
8+
from typing import Optional, Any, Dict, Union
89
from datetime import datetime
910
from importlib.metadata import version, PackageNotFoundError
1011
from .models.request import RequestOptionBuilder
@@ -22,6 +23,8 @@
2223

2324

2425
class BaseClient:
26+
client: Union[PoolManager, ProxyManager]
27+
2528
def __init__(
2629
self,
2730
api_key: Optional[str],
@@ -33,6 +36,7 @@ def __init__(
3336
verbose: bool,
3437
trace: bool,
3538
custom_json: Optional[Any] = None,
39+
proxy: Optional[str] = None,
3640
):
3741
if api_key is None:
3842
raise AuthError(
@@ -66,15 +70,25 @@ def __init__(
6670
backoff_factor=0.1, # [0.0s, 0.2s, 0.4s, 0.8s, 1.6s, ...]
6771
)
6872

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+
)
7892

7993
self.timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout)
8094

@@ -102,6 +116,12 @@ def _get(
102116

103117
headers = self._concat_headers(option.headers)
104118

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+
105125
if self.trace:
106126
full_url = f"{self.BASE}{path}"
107127
if params:
@@ -228,7 +248,7 @@ def _paginate_iter(
228248
for t in decoded[result_key]:
229249
yield deserializer(t)
230250
if "next_url" in decoded:
231-
path = decoded["next_url"].replace(self.BASE, "")
251+
path = decoded["next_url"] # Use full next_url
232252
params = {}
233253
else:
234254
return

0 commit comments

Comments
 (0)