aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAljoscha Rittner <[email protected]>2022-06-27 09:47:30 +0200
committerAljoscha Rittner <[email protected]>2022-06-27 11:13:35 +0200
commit7fd2a6a39cf7362e9542699f6f948365fd273505 (patch)
treef62af20b3ab266f1eb2cd7e1c24f54b68aa88990
parentf8f988710c853525b20c2a02acc3ce9dd95d4167 (diff)
downloadmastodon.py-7fd2a6a39cf7362e9542699f6f948365fd273505.tar.gz
Supports a User-Agent header
fixes #213
-rw-r--r--mastodon/Mastodon.py22
-rw-r--r--tests/conftest.py3
2 files changed, 23 insertions, 2 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 3851fbf..1645225 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -293,6 +293,7 @@ class Mastodon:
293 secret_file.write(response['client_id'] + "\n") 293 secret_file.write(response['client_id'] + "\n")
294 secret_file.write(response['client_secret'] + "\n") 294 secret_file.write(response['client_secret'] + "\n")
295 secret_file.write(api_base_url + "\n") 295 secret_file.write(api_base_url + "\n")
296 secret_file.write(client_name + "\n")
296 297
297 return (response['client_id'], response['client_secret']) 298 return (response['client_id'], response['client_secret'])
298 299
@@ -303,7 +304,7 @@ class Mastodon:
303 api_base_url=None, debug_requests=False, 304 api_base_url=None, debug_requests=False,
304 ratelimit_method="wait", ratelimit_pacefactor=1.1, 305 ratelimit_method="wait", ratelimit_pacefactor=1.1,
305 request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, 306 request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None,
306 version_check_mode = "created", session=None, feature_set="mainline"): 307 version_check_mode = "created", session=None, feature_set="mainline", user_agent=None):
307 """ 308 """
308 Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you 309 Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you
309 give a `client_id` and it is not a file, you must also give a secret. If you specify an 310 give a `client_id` and it is not a file, you must also give a secret. If you specify an
@@ -348,6 +349,11 @@ class Mastodon:
348 `feature_set` can be used to enable behaviour specific to non-mainline Mastodon API implementations. 349 `feature_set` can be used to enable behaviour specific to non-mainline Mastodon API implementations.
349 Details are documented in the functions that provide such functionality. Currently supported feature 350 Details are documented in the functions that provide such functionality. Currently supported feature
350 sets are `mainline`, `fedibird` and `pleroma`. 351 sets are `mainline`, `fedibird` and `pleroma`.
352
353 For some mastodon-instances a `User-Agent` header is needed. This can be set by parameter `user_agent`. From now
354 `create_app()` stores the application name into the client secret file. If `client_id` points to this file,
355 the app name will be used as `User-Agent` header as default. It's possible to modify old secret files and append
356 a client app name to use it as a `User-Agent` name.
351 """ 357 """
352 self.api_base_url = None 358 self.api_base_url = None
353 if not api_base_url is None: 359 if not api_base_url is None:
@@ -379,6 +385,9 @@ class Mastodon:
379 self.feature_set = feature_set 385 self.feature_set = feature_set
380 if not self.feature_set in ["mainline", "fedibird", "pleroma"]: 386 if not self.feature_set in ["mainline", "fedibird", "pleroma"]:
381 raise MastodonIllegalArgumentError('Requested invalid feature set') 387 raise MastodonIllegalArgumentError('Requested invalid feature set')
388
389 # General defined user-agent
390 self.user_agent = user_agent
382 391
383 # Token loading 392 # Token loading
384 if self.client_id is not None: 393 if self.client_id is not None:
@@ -393,6 +402,11 @@ class Mastodon:
393 if not (self.api_base_url is None or try_base_url == self.api_base_url): 402 if not (self.api_base_url is None or try_base_url == self.api_base_url):
394 raise MastodonIllegalArgumentError('Mismatch in base URLs between files and/or specified') 403 raise MastodonIllegalArgumentError('Mismatch in base URLs between files and/or specified')
395 self.api_base_url = try_base_url 404 self.api_base_url = try_base_url
405
406 # With new registrations we support the 4th line to store a client_name and use it as user-agent
407 client_name = secret_file.readline()
408 if client_name and self.user_agent is None:
409 self.user_agent = client_name.rstrip()
396 else: 410 else:
397 if self.client_secret is None: 411 if self.client_secret is None:
398 raise MastodonIllegalArgumentError('Specified client id directly, but did not supply secret') 412 raise MastodonIllegalArgumentError('Specified client id directly, but did not supply secret')
@@ -3334,6 +3348,10 @@ class Mastodon:
3334 if not access_token_override is None: 3348 if not access_token_override is None:
3335 headers['Authorization'] = 'Bearer ' + access_token_override 3349 headers['Authorization'] = 'Bearer ' + access_token_override
3336 3350
3351 # Add user-agent
3352 if self.user_agent:
3353 headers['User-Agent'] = self.user_agent
3354
3337 # Determine base URL 3355 # Determine base URL
3338 base_url = self.api_base_url 3356 base_url = self.api_base_url
3339 if not base_url_override is None: 3357 if not base_url_override is None:
@@ -3592,6 +3610,8 @@ class Mastodon:
3592 # Connect function (called and then potentially passed to async handler) 3610 # Connect function (called and then potentially passed to async handler)
3593 def connect_func(): 3611 def connect_func():
3594 headers = {"Authorization": "Bearer " + self.access_token} if self.access_token else {} 3612 headers = {"Authorization": "Bearer " + self.access_token} if self.access_token else {}
3613 if self.user_agent:
3614 headers['User-Agent'] = self.user_agent
3595 connection = self.session.get(url + endpoint, headers = headers, data = params, stream = True, 3615 connection = self.session.get(url + endpoint, headers = headers, data = params, stream = True,
3596 timeout=(self.request_timeout, timeout)) 3616 timeout=(self.request_timeout, timeout))
3597 3617
diff --git a/tests/conftest.py b/tests/conftest.py
index 22a4b75..8e64919 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -8,7 +8,8 @@ def _api(access_token='__MASTODON_PY_TEST_ACCESS_TOKEN', version="3.1.1", versio
8 client_secret='__MASTODON_PY_TEST_CLIENT_SECRET', 8 client_secret='__MASTODON_PY_TEST_CLIENT_SECRET',
9 access_token=access_token, 9 access_token=access_token,
10 mastodon_version=version, 10 mastodon_version=version,
11 version_check_mode=version_check_mode) 11 version_check_mode=version_check_mode,
12 user_agent='tests/v311')
12 13
13 14
14@pytest.fixture 15@pytest.fixture
Powered by cgit v1.2.3 (git 2.41.0)