@@ -57,33 +57,29 @@ def __init__(self, provider=None):
57
57
"""
58
58
QObject .__init__ (self )
59
59
60
- self .key = provider [' key' ]
61
- self .base_url = provider [' base_url' ]
62
- self .ENV_VARS = provider .get (' ENV_VARS' )
60
+ self .key = provider [" key" ]
61
+ self .base_url = provider [" base_url" ]
62
+ self .ENV_VARS = provider .get (" ENV_VARS" )
63
63
64
64
# self.session = requests.Session()
65
- retry_timeout = provider .get (' timeout' )
65
+ retry_timeout = provider .get (" timeout" )
66
66
67
67
self .nam = networkaccessmanager .NetworkAccessManager (debug = False , timeout = retry_timeout )
68
68
69
69
self .retry_timeout = timedelta (seconds = retry_timeout )
70
70
self .headers = {
71
- "User-Agent" : _USER_AGENT ,
72
- ' Content-type' : ' application/json' ,
73
- ' Authorization' : provider [' key' ]
74
- }
71
+ "User-Agent" : _USER_AGENT ,
72
+ " Content-type" : " application/json" ,
73
+ " Authorization" : provider [" key" ],
74
+ }
75
75
76
76
# Save some references to retrieve in client instances
77
77
self .url = None
78
78
self .warnings = None
79
79
80
80
overQueryLimit = pyqtSignal ()
81
- def request (self ,
82
- url ,
83
- params ,
84
- first_request_time = None ,
85
- retry_counter = 0 ,
86
- post_json = None ):
81
+
82
+ def request (self , url , params , first_request_time = None , retry_counter = 0 , post_json = None ):
87
83
"""Performs HTTP GET/POST with credentials, returning the body as
88
84
JSON.
89
85
@@ -124,14 +120,15 @@ def request(self,
124
120
# 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration,
125
121
# starting at 0.5s when retry_counter=1. The first retry will occur
126
122
# at 1, so subtract that first.
127
- delay_seconds = 1.5 ** (retry_counter - 1 )
123
+ delay_seconds = 1.5 ** (retry_counter - 1 )
128
124
129
125
# Jitter this value by 50% and pause.
130
126
time .sleep (delay_seconds * (random .random () + 0.5 ))
131
127
132
- authed_url = self ._generate_auth_url (url ,
133
- params ,
134
- )
128
+ authed_url = self ._generate_auth_url (
129
+ url ,
130
+ params ,
131
+ )
135
132
self .url = self .base_url + authed_url
136
133
137
134
# Default to the client-level self.requests_kwargs, with method-level
@@ -140,25 +137,20 @@ def request(self,
140
137
141
138
# Determine GET/POST
142
139
# requests_method = self.session.get
143
- requests_method = ' GET'
140
+ requests_method = " GET"
144
141
body = None
145
142
if post_json is not None :
146
143
# requests_method = self.session.post
147
144
# final_requests_kwargs["json"] = post_json
148
145
body = post_json
149
- requests_method = ' POST'
146
+ requests_method = " POST"
150
147
151
- logger .log (
152
- f"url: { self .url } \n Parameters: { json .dumps (body , indent = 2 )} " ,
153
- 0
154
- )
148
+ logger .log (f"url: { self .url } \n Parameters: { json .dumps (body , indent = 2 )} " , 0 )
155
149
156
150
try :
157
- response , content = self .nam .request (self .url ,
158
- method = requests_method ,
159
- body = body ,
160
- headers = self .headers ,
161
- blocking = True )
151
+ response , content = self .nam .request (
152
+ self .url , method = requests_method , body = body , headers = self .headers , blocking = True
153
+ )
162
154
except networkaccessmanager .RequestsExceptionTimeout :
163
155
raise exceptions .Timeout
164
156
@@ -167,7 +159,6 @@ def request(self,
167
159
self ._check_status ()
168
160
169
161
except exceptions .OverQueryLimit as e :
170
-
171
162
# Let the instances know something happened
172
163
# noinspection PyUnresolvedReferences
173
164
self .overQueryLimit .emit ()
@@ -176,17 +167,21 @@ def request(self,
176
167
return self .request (url , params , first_request_time , retry_counter + 1 , post_json )
177
168
178
169
except exceptions .ApiError as e :
179
- logger .log (f"Feature ID { post_json ['id' ]} caused a { e .__class__ .__name__ } : { str (e )} " , 2 )
170
+ logger .log (
171
+ f"Feature ID { post_json ['id' ]} caused a { e .__class__ .__name__ } : { str (e )} " , 2
172
+ )
180
173
raise
181
174
182
175
raise
183
176
184
177
# Write env variables if successful
185
178
if self .ENV_VARS :
186
179
for env_var in self .ENV_VARS :
187
- configmanager .write_env_var (env_var , response .headers .get (self .ENV_VARS [env_var ], 'None' ))
180
+ configmanager .write_env_var (
181
+ env_var , response .headers .get (self .ENV_VARS [env_var ], "None" )
182
+ )
188
183
189
- return json .loads (content .decode (' utf-8' ))
184
+ return json .loads (content .decode (" utf-8" ))
190
185
191
186
def _check_status (self ):
192
187
"""
@@ -202,34 +197,28 @@ def _check_status(self):
202
197
"""
203
198
204
199
status_code = self .nam .http_call_result .status_code
205
- message = self .nam .http_call_result .text if self .nam .http_call_result .text != '' else self .nam .http_call_result .reason
200
+ message = (
201
+ self .nam .http_call_result .text
202
+ if self .nam .http_call_result .text != ""
203
+ else self .nam .http_call_result .reason
204
+ )
206
205
207
206
if not status_code :
208
- raise Exception (f"{ message } . Are your provider settings correct and the provider ready?" )
207
+ raise Exception (
208
+ f"{ message } . Are your provider settings correct and the provider ready?"
209
+ )
209
210
210
211
elif status_code == 403 :
211
- raise exceptions .InvalidKey (
212
- str (status_code ),
213
- message
214
- )
212
+ raise exceptions .InvalidKey (str (status_code ), message )
215
213
216
214
elif status_code == 429 :
217
- raise exceptions .OverQueryLimit (
218
- str (status_code ),
219
- message
220
- )
215
+ raise exceptions .OverQueryLimit (str (status_code ), message )
221
216
# Internal error message for Bad Request
222
217
elif 400 <= status_code < 500 :
223
- raise exceptions .ApiError (
224
- str (status_code ),
225
- message
226
- )
218
+ raise exceptions .ApiError (str (status_code ), message )
227
219
# Other HTTP errors have different formatting
228
220
elif status_code != 200 :
229
- raise exceptions .GenericServerError (
230
- str (status_code ),
231
- message
232
- )
221
+ raise exceptions .GenericServerError (str (status_code ), message )
233
222
234
223
def _generate_auth_url (self , path , params ):
235
224
"""Returns the path and query string portion of the request URL, first
@@ -245,7 +234,7 @@ def _generate_auth_url(self, path, params):
245
234
:rtype: string
246
235
"""
247
236
248
- if type (params ) is dict :
237
+ if isinstance (params , dict ) :
249
238
params = sorted (dict (** params ).items ())
250
239
251
240
# Only auto-add API key when using ORS. If own instance, API key must
0 commit comments