55from enum import Enum
66from typing import Optional , Any
77
8- base = ' https://api.polygon.io'
8+ base = " https://api.polygon.io"
99env_key = "POLYGON_API_KEY"
1010
1111# https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
1212class BaseClient :
1313 def __init__ (
14- self ,
15- api_key : Optional [str ] = os .getenv (env_key ),
16- connect_timeout : float = 10.0 ,
17- read_timeout : float = 10.0 ,
18- num_pools : int = 10 ,
19- retries = 3 ,
20- base : str = base
21- ):
14+ self ,
15+ api_key : Optional [str ] = os .getenv (env_key ),
16+ connect_timeout : float = 10.0 ,
17+ read_timeout : float = 10.0 ,
18+ num_pools : int = 10 ,
19+ retries = 3 ,
20+ base : str = base ,
21+ ):
2222 if api_key is None :
23- raise Exception (f"Must specify env var { env_key } or pass api_key in constructor" )
23+ raise Exception (
24+ f"Must specify env var { env_key } or pass api_key in constructor"
25+ )
2426 self .API_KEY = api_key
2527 self .BASE = base
2628
2729 # https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool
28- self .client = urllib3 .PoolManager (num_pools = num_pools , headers = {
29- ' Authorization' : ' Bearer ' + self .API_KEY
30- } )
31- self .timeout = urllib3 .Timeout (connect = connect_timeout , read = read_timeout )
30+ self .client = urllib3 .PoolManager (
31+ num_pools = num_pools , headers = { " Authorization" : " Bearer " + self .API_KEY }
32+ )
33+ self .timeout = urllib3 .Timeout (connect = connect_timeout , read = read_timeout )
3234 self .retries = retries
3335
3436 def _decode (self , resp ):
35- return json .loads (resp .data .decode (' utf-8' ))
37+ return json .loads (resp .data .decode (" utf-8" ))
3638
37- def _get (self , path : str , params : Optional [dict ] = None , resultKey : Optional [str ] = None , deserializer = None , raw : bool = False ) -> Any :
39+ def _get (
40+ self ,
41+ path : str ,
42+ params : Optional [dict ] = None ,
43+ resultKey : Optional [str ] = None ,
44+ deserializer = None ,
45+ raw : bool = False ,
46+ ) -> Any :
3847 if params is None :
3948 params = {}
4049 params = {str (k ): str (v ) for k , v in params .items () if v is not None }
41- resp = self .client .request ('GET' , self .BASE + path , fields = params , retries = self .retries )
50+ resp = self .client .request (
51+ "GET" , self .BASE + path , fields = params , retries = self .retries
52+ )
4253
4354 if resp .status != 200 :
44- raise Exception (resp .data .decode (' utf-8' ))
55+ raise Exception (resp .data .decode (" utf-8" ))
4556
4657 if raw :
4758 return resp
@@ -50,7 +61,7 @@ def _get(self, path: str, params: Optional[dict] = None, resultKey: Optional[str
5061
5162 if resultKey :
5263 obj = obj [resultKey ]
53-
64+
5465 if deserializer :
5566 obj = [deserializer (o ) for o in obj ]
5667
@@ -63,7 +74,7 @@ def _get_params(self, fn, caller_locals):
6374 # https://docs.python.org/3.7/library/inspect.html#inspect.Signature
6475 for argname , v in inspect .signature (fn ).parameters .items ():
6576 # https://docs.python.org/3.7/library/inspect.html#inspect.Parameter
66- if argname in [' params' , ' raw' ]:
77+ if argname in [" params" , " raw" ]:
6778 continue
6879 if v .default != v .empty :
6980 # timestamp_lt -> timestamp.lt
@@ -77,14 +88,16 @@ def _get_params(self, fn, caller_locals):
7788
7889 def _paginate (self , path : str , params : dict , raw : bool , deserializer ):
7990 while True :
80- resp = self ._get (path = path , params = params , deserializer = deserializer , raw = True )
91+ resp = self ._get (
92+ path = path , params = params , deserializer = deserializer , raw = True
93+ )
8194 if raw :
8295 return resp
8396 decoded = self ._decode (resp )
8497 for t in decoded ["results" ]:
8598 yield deserializer (t )
8699 if "next_url" in decoded :
87- path = decoded ["next_url" ].replace (self .BASE , '' )
100+ path = decoded ["next_url" ].replace (self .BASE , "" )
88101 params = {}
89102 else :
90103 return
0 commit comments