11import logging
22from abc import ABC
33from dataclasses import dataclass
4- from typing import Optional , Generator , Union , Type
4+ from typing import Optional , Generator
55
6- from ratelimit import limits , sleep_and_retry
76from requests import Response , request
87
98from open_sea_v1 .responses .abc import BaseResponse
109
1110logger = logging .getLogger (__name__ )
1211
13- MAX_CALLS_PER_SECOND = 2 # gets overriden if API key is passed to ClientParams instance
14- RATE_LIMIT = 1 # second
15-
1612@dataclass
1713class ClientParams :
1814 """Common OpenSea Endpoint parameters to pass in."""
@@ -25,7 +21,6 @@ class ClientParams:
2521 def __post_init__ (self ):
2622 self ._validate_attrs ()
2723 self ._decrement_max_pages_attr ()
28- self ._set_max_rate_limit ()
2924
3025 def _validate_attrs (self ) -> None :
3126 if self .limit is not None and not 0 < self .limit <= 300 :
@@ -43,35 +38,14 @@ def _decrement_max_pages_attr(self) -> None:
4338 if self .max_pages is not None :
4439 self .max_pages -= 1
4540
46- def _set_max_rate_limit (self ) -> None :
47- global MAX_CALLS_PER_SECOND
48- MAX_CALLS_PER_SECOND = 2 # per second
49- if self .api_key :
50- raise NotImplementedError ("I don't know what the rate limit is for calls with an API key is yet." )
5141
52- @dataclass
5342class BaseClient (ABC ):
54- """
55- Parameters
56- ----------
57- client_params:
58- ClientParams instance.
59-
60- rate_limiting: bool
61- If True, will throttle the amount of requests per second to the OpenSea API.
62- If you pass an API key into the client_params instance, the rate limiting will change accordingly.
63- If False, will not throttle.
64- """
65-
6643 client_params : ClientParams
44+ processed_pages : int = 0
45+ response = None
46+ parsed_http_response = None
6747 url = None
68- rate_limiting : bool = True
69-
70- def __post_init__ (self ):
71- self .processed_pages : int = 0
72- self .response = None
73- self .parsed_http_response = None
74- self ._http_response = None
48+ _http_response = None
7549
7650 @property
7751 def http_headers (self ) -> dict :
@@ -80,22 +54,10 @@ def http_headers(self) -> dict:
8054 params ['headers' ] = {'X-API-Key' : self .client_params .api_key }
8155 return params
8256
83- @sleep_and_retry
84- @limits (calls = MAX_CALLS_PER_SECOND , period = RATE_LIMIT )
8557 def _get_request (self , ** kwargs ) -> Response :
86- """Get requests with a rate limiter."""
8758 updated_kwargs = kwargs | self .http_headers
8859 return request ('GET' , self .url , ** updated_kwargs )
8960
90- def parse_http_response (self , response_type : Type [BaseResponse ], key : str )\
91- -> list [Union [Type [BaseResponse ], BaseResponse ]]:
92- if self ._http_response :
93- the_json = self ._http_response .json ()
94- the_json = the_json [key ] if isinstance (the_json , dict ) else the_json # the collections endpoint needs this
95- responses = [response_type (element ) for element in the_json ]
96- return responses
97- return list ()
98-
9961 def get_pages (self ) -> Generator [list [list [BaseResponse ]], None , None ]:
10062 self .processed_pages = 0
10163 self .client_params .offset = 0 if self .client_params .offset is None else self .client_params .offset
0 commit comments