11import logging
22from abc import ABC
33from dataclasses import dataclass
4- from typing import Optional , Generator , Union
4+ from typing import Optional , Generator , Union , Type
55
66from ratelimit import limits , sleep_and_retry
77from requests import Response , request
@@ -49,16 +49,38 @@ def _set_max_rate_limit(self) -> None:
4949 if self .api_key :
5050 raise NotImplementedError ("I don't know what the rate limit is for calls with an API key is yet." )
5151
52-
52+ @ dataclass
5353class 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+ retry: bool
66+ OpenSea will occasionally return an empty response object, although the same query would yield
67+ a full response object afterwards.
68+ If True, will pause the request for one second before trying again once.
69+ If it fails again, the empty response is returned.
70+ If set to False, the client always returns the first response object, even if it is empty.
71+
72+ """
73+
5474 client_params : ClientParams
55- processed_pages : int = 0
56- response = None
57- parsed_http_response = None
5875 url = None
5976 rate_limiting : bool = True
60- _http_response = None
61- _rate_limiting_timer : float = 0.0
77+ retry : bool = True
78+
79+ def __post_init__ (self ):
80+ self .processed_pages : int = 0
81+ self .response = None
82+ self .parsed_http_response = None
83+ self ._http_response = None
6284
6385 @property
6486 def http_headers (self ) -> dict :
@@ -74,6 +96,15 @@ def _get_request(self, **kwargs) -> Response:
7496 updated_kwargs = kwargs | self .http_headers
7597 return request ('GET' , self .url , ** updated_kwargs )
7698
99+ def parse_http_response (self , response_type : Type [BaseResponse ], key : str )\
100+ -> list [Union [Type [BaseResponse ], BaseResponse ]]:
101+ if self ._http_response :
102+ the_json = self ._http_response .json ()
103+ the_json = the_json [key ] if isinstance (the_json , dict ) else the_json # the collections endpoint needs this
104+ responses = [response_type (element ) for element in the_json ]
105+ return responses
106+ return list ()
107+
77108 def get_pages (self ) -> Generator [list [list [BaseResponse ]], None , None ]:
78109 self .processed_pages = 0
79110 self .client_params .offset = 0 if self .client_params .offset is None else self .client_params .offset
0 commit comments