aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon/internals.py')
-rw-r--r--mastodon/internals.py40
1 files changed, 18 insertions, 22 deletions
diff --git a/mastodon/internals.py b/mastodon/internals.py
index 758a1d4..d94a116 100644
--- a/mastodon/internals.py
+++ b/mastodon/internals.py
@@ -182,11 +182,10 @@ class Mastodon():
182 base_url = base_url_override 182 base_url = base_url_override
183 183
184 if self.debug_requests: 184 if self.debug_requests:
185 print('Mastodon: Request to endpoint "' + base_url + 185 print(f'Mastodon: Request to endpoint "{base_url}{endpoint}" using method "{method}".')
186 endpoint + '" using method "' + method + '".') 186 print(f'Parameters: {params}')
187 print('Parameters: ' + str(params)) 187 print(f'Headers: {headers}')
188 print('Headers: ' + str(headers)) 188 print(f'Files: {files}')
189 print('Files: ' + str(files))
190 189
191 # Make request 190 # Make request
192 request_complete = False 191 request_complete = False
@@ -205,7 +204,7 @@ class Mastodon():
205 204
206 response_object = self.session.request(method, base_url + endpoint, **kwargs) 205 response_object = self.session.request(method, base_url + endpoint, **kwargs)
207 except Exception as e: 206 except Exception as e:
208 raise MastodonNetworkError("Could not complete request: %s" % e) 207 raise MastodonNetworkError(f"Could not complete request: {e}")
209 208
210 if response_object is None: 209 if response_object is None:
211 raise MastodonIllegalArgumentError("Illegal request.") 210 raise MastodonIllegalArgumentError("Illegal request.")
@@ -219,8 +218,7 @@ class Mastodon():
219 218
220 # For gotosocial, we need an int representation, but for non-ints this would crash 219 # For gotosocial, we need an int representation, but for non-ints this would crash
221 try: 220 try:
222 ratelimit_intrep = str( 221 ratelimit_intrep = str(int(response_object.headers['X-RateLimit-Reset']))
223 int(response_object.headers['X-RateLimit-Reset']))
224 except: 222 except:
225 ratelimit_intrep = None 223 ratelimit_intrep = None
226 224
@@ -240,13 +238,13 @@ class Mastodon():
240 self.ratelimit_reset += server_time_diff 238 self.ratelimit_reset += server_time_diff
241 self.ratelimit_lastcall = time.time() 239 self.ratelimit_lastcall = time.time()
242 except Exception as e: 240 except Exception as e:
243 raise MastodonRatelimitError("Rate limit time calculations failed: %s" % e) 241 raise MastodonRatelimitError(f"Rate limit time calculations failed: {e}")
244 242
245 # Handle response 243 # Handle response
246 if self.debug_requests: 244 if self.debug_requests:
247 print('Mastodon: Response received with code ' + str(response_object.status_code) + '.') 245 print(f'Mastodon: Response received with code {response_object.status_code}.')
248 print('response headers: ' + str(response_object.headers)) 246 print(f'response headers: {response_object.headers}')
249 print('Response text content: ' + str(response_object.text)) 247 print(f'Response text content: {response_object.text}')
250 248
251 if not response_object.ok: 249 if not response_object.ok:
252 try: 250 try:
@@ -306,9 +304,9 @@ class Mastodon():
306 response = response_object.json(object_hook=self.__json_hooks) 304 response = response_object.json(object_hook=self.__json_hooks)
307 except: 305 except:
308 raise MastodonAPIError( 306 raise MastodonAPIError(
309 "Could not parse response as JSON, response code was %s, " 307 f"Could not parse response as JSON, response code was {response_object.status_code}, "
310 "bad json content was '%s'" % (response_object.status_code, 308 f"bad json content was {response_object.content!r}."
311 response_object.content)) 309 )
312 else: 310 else:
313 response = response_object.content 311 response = response_object.content
314 312
@@ -413,8 +411,8 @@ class Mastodon():
413 url = "http://" + parse.netloc 411 url = "http://" + parse.netloc
414 else: 412 else:
415 raise MastodonAPIError( 413 raise MastodonAPIError(
416 "Could not parse streaming api location returned from server: {}.".format( 414 f"Could not parse streaming api location returned from server: {instance['urls']['streaming_api']}."
417 instance["urls"]["streaming_api"])) 415 )
418 else: 416 else:
419 url = self.api_base_url 417 url = self.api_base_url
420 return url 418 return url
@@ -436,16 +434,14 @@ class Mastodon():
436 434
437 # Connect function (called and then potentially passed to async handler) 435 # Connect function (called and then potentially passed to async handler)
438 def connect_func(): 436 def connect_func():
439 headers = {"Authorization": "Bearer " + 437 headers = {"Authorization": "Bearer " + self.access_token} if self.access_token else {}
440 self.access_token} if self.access_token else {}
441 if self.user_agent: 438 if self.user_agent:
442 headers['User-Agent'] = self.user_agent 439 headers['User-Agent'] = self.user_agent
443 connection = self.session.get(url + endpoint, headers=headers, data=params, stream=True, 440 connection = self.session.get(url + endpoint, headers=headers, data=params, stream=True,
444 timeout=(self.request_timeout, timeout)) 441 timeout=(self.request_timeout, timeout))
445 442
446 if connection.status_code != 200: 443 if connection.status_code != 200:
447 raise MastodonNetworkError( 444 raise MastodonNetworkError(f"Could not connect to streaming server: {connection.reason}")
448 "Could not connect to streaming server: %s" % connection.reason)
449 return connection 445 return connection
450 connection = None 446 connection = None
451 447
@@ -638,7 +634,7 @@ class Mastodon():
638 raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.') 634 raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
639 if file_name is None: 635 if file_name is None:
640 random_suffix = uuid.uuid4().hex 636 random_suffix = uuid.uuid4().hex
641 file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type) 637 file_name = f"mastodonpyupload_{time.time()}_{random_suffix}{mimetypes.guess_extension(mime_type)}"
642 return (file_name, media_file, mime_type) 638 return (file_name, media_file, mime_type)
643 639
644 @staticmethod 640 @staticmethod
Powered by cgit v1.2.3 (git 2.41.0)