@@ -22,12 +22,14 @@ class ApiClient(object):
2222 _api_headers = {}
2323
2424 _max_ttl = 60 * 12
25+ _requests_pause = 5
2526
26- def __init__ (self , api_key : str ) -> None :
27+ def __init__ (self , api_key : str , requests_pause : int = 5 ) -> None :
2728 self ._api_headers = {
2829 'X-API-KEY' : api_key ,
2930 'client' : f'Python SDK { VERSION } '
3031 }
32+ self ._requests_pause = requests_pause
3133
3234 def get_request_archive (self , request_id : str ) -> dict :
3335 '''
@@ -46,17 +48,17 @@ def get_request_archive(self, request_id: str) -> dict:
4648
4749 raise Exception (f'Response status code: { response .status_code } ' )
4850
49- def _wait_request_archive (self , request_id : str , requests_pause : int = 5 ) -> dict :
50- ttl = self ._max_ttl / requests_pause
51+ def _wait_request_archive (self , request_id : str ) -> dict :
52+ ttl = self ._max_ttl / self . _requests_pause
5153
5254 while ttl > 0 :
5355 ttl -= 1
5456
57+ sleep (self ._requests_pause )
58+
5559 result = self .get_request_archive (request_id )
5660 if result ['status' ] != 'Pending' : return result
5761
58- sleep (requests_pause )
59-
6062 raise Exception ('Timeout exceeded' )
6163
6264 def google_search (self , query : list , language : str = 'en' , region : str = None ) -> dict :
@@ -78,7 +80,6 @@ def google_search(self, query: list, language: str = 'en', region: str = None) -
7880 }, headers = self ._api_headers )
7981
8082 if 199 < response .status_code < 300 :
81- sleep (10 )
8283 return self ._wait_request_archive (response .json ()['id' ])
8384
8485 raise Exception (f'Response status code: { response .status_code } ' )
@@ -112,7 +113,6 @@ def google_maps_search(self, query: list, limit: int = 400, extract_contacts: bo
112113 }, headers = self ._api_headers )
113114
114115 if 199 < response .status_code < 300 :
115- sleep (15 )
116116 return self ._wait_request_archive (response .json ()['id' ]).get ('data' , [])
117117
118118 raise Exception (f'Response status code: { response .status_code } ' )
@@ -157,7 +157,6 @@ def google_maps_reviews(self, query: list, reviewsLimit: int = 100, limit: int =
157157 }, headers = self ._api_headers )
158158
159159 if 199 < response .status_code < 300 :
160- sleep (15 )
161160 return self ._wait_request_archive (response .json ()['id' ]).get ('data' , [])
162161
163162 raise Exception (f'Response status code: { response .status_code } ' )
@@ -188,7 +187,6 @@ def google_maps_photos(self, query: list, photosLimit: int = 100, limit: int = 1
188187 }, headers = self ._api_headers )
189188
190189 if 199 < response .status_code < 300 :
191- sleep (15 )
192190 return self ._wait_request_archive (response .json ()['id' ]).get ('data' , [])
193191
194192 raise Exception (f'Response status code: { response .status_code } ' )
@@ -223,7 +221,6 @@ def google_play_reviews(self, query: list, reviewsLimit: int = 100, sort: str =
223221 }, headers = self ._api_headers )
224222
225223 if 199 < response .status_code < 300 :
226- sleep (15 )
227224 return self ._wait_request_archive (response .json ()['id' ]).get ('data' , [])
228225
229226 raise Exception (f'Response status code: { response .status_code } ' )
0 commit comments