diff options
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r-- | mastodon/Mastodon.py | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index f0d9cc5..e7ec143 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -167,7 +167,6 @@ class Mastodon: | |||
167 | If anything is unclear, check the official API docs at | 167 | If anything is unclear, check the official API docs at |
168 | https://github.com/mastodon/documentation/blob/master/content/en/client/intro.md | 168 | https://github.com/mastodon/documentation/blob/master/content/en/client/intro.md |
169 | """ | 169 | """ |
170 | __DEFAULT_BASE_URL = 'https://mastodon.social' | ||
171 | __DEFAULT_TIMEOUT = 300 | 170 | __DEFAULT_TIMEOUT = 300 |
172 | __DEFAULT_STREAM_TIMEOUT = 300 | 171 | __DEFAULT_STREAM_TIMEOUT = 300 |
173 | __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5 | 172 | __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5 |
@@ -260,17 +259,17 @@ class Mastodon: | |||
260 | ### | 259 | ### |
261 | @staticmethod | 260 | @staticmethod |
262 | def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None, | 261 | def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None, |
263 | api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT, session=None): | 262 | api_base_url=None, request_timeout=__DEFAULT_TIMEOUT, session=None): |
264 | """ | 263 | """ |
265 | Create a new app with given `client_name` and `scopes` (The basic scopes are "read", "write", "follow" and "push" | 264 | Create a new app with given `client_name` and `scopes` (The basic scopes are "read", "write", "follow" and "push" |
266 | - more granular scopes are available, please refer to Mastodon documentation for which). | 265 | - more granular scopes are available, please refer to Mastodon documentation for which) on the instance given |
267 | 266 | by `api_base_url`. | |
267 | |||
268 | Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating in an OAuth flow. | 268 | Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating in an OAuth flow. |
269 | You can specify multiple URLs by passing a list. Note that if you wish to use OAuth authentication with redirects, | 269 | You can specify multiple URLs by passing a list. Note that if you wish to use OAuth authentication with redirects, |
270 | the redirect URI must be one of the URLs specified here. | 270 | the redirect URI must be one of the URLs specified here. |
271 | 271 | ||
272 | Specify `to_file` to persist your app's info to a file so you can use it in the constructor. | 272 | Specify `to_file` to persist your app's info to a file so you can use it in the constructor. |
273 | Specify `api_base_url` if you want to register an app on an instance different from the flagship one. | ||
274 | Specify `website` to give a website for your app. | 273 | Specify `website` to give a website for your app. |
275 | 274 | ||
276 | Specify `session` with a requests.Session for it to be used instead of the default. This can be | 275 | Specify `session` with a requests.Session for it to be used instead of the default. This can be |
@@ -282,6 +281,8 @@ class Mastodon: | |||
282 | 281 | ||
283 | Returns `client_id` and `client_secret`, both as strings. | 282 | Returns `client_id` and `client_secret`, both as strings. |
284 | """ | 283 | """ |
284 | if api_base_url is None: | ||
285 | raise MastodonIllegalArgumentError("API base URL is required.") | ||
285 | api_base_url = Mastodon.__protocolize(api_base_url) | 286 | api_base_url = Mastodon.__protocolize(api_base_url) |
286 | 287 | ||
287 | request_data = { | 288 | request_data = { |
@@ -299,12 +300,10 @@ class Mastodon: | |||
299 | if website is not None: | 300 | if website is not None: |
300 | request_data['website'] = website | 301 | request_data['website'] = website |
301 | if session: | 302 | if session: |
302 | ret = session.post(api_base_url + '/api/v1/apps', | 303 | ret = session.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) |
303 | data=request_data, timeout=request_timeout) | ||
304 | response = ret.json() | 304 | response = ret.json() |
305 | else: | 305 | else: |
306 | response = requests.post( | 306 | response = requests.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) |
307 | api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) | ||
308 | response = response.json() | 307 | response = response.json() |
309 | except Exception as e: | 308 | except Exception as e: |
310 | raise MastodonNetworkError("Could not complete request: %s" % e) | 309 | raise MastodonNetworkError("Could not complete request: %s" % e) |
@@ -327,11 +326,11 @@ class Mastodon: | |||
327 | request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, | 326 | request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, |
328 | version_check_mode="created", session=None, feature_set="mainline", user_agent="mastodonpy"): | 327 | version_check_mode="created", session=None, feature_set="mainline", user_agent="mastodonpy"): |
329 | """ | 328 | """ |
330 | Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you | 329 | Create a new API wrapper instance based on the given `client_secret` and `client_id` on the |
331 | give a `client_id` and it is not a file, you must also give a secret. If you specify an | 330 | instance given by `api_base_url`. If you give a `client_id` and it is not a file, you must |
332 | `access_token` then you don't need to specify a `client_id`. It is allowed to specify | 331 | also give a secret. If you specify an `access_token` then you don't need to specify a `client_id`. |
333 | neither - in this case, you will be restricted to only using endpoints that do not | 332 | It is allowed to specify neither - in this case, you will be restricted to only using endpoints |
334 | require authentication. If a file is given as `client_id`, client ID, secret and | 333 | that do not require authentication. If a file is given as `client_id`, client ID, secret and |
335 | base url are read from that file. | 334 | base url are read from that file. |
336 | 335 | ||
337 | You can also specify an `access_token`, directly or as a file (as written by `log_in()`_). If | 336 | You can also specify an `access_token`, directly or as a file (as written by `log_in()`_). If |
@@ -347,10 +346,6 @@ class Mastodon: | |||
347 | even in "wait" and "pace" mode, requests can still fail due to network or other problems! Also | 346 | even in "wait" and "pace" mode, requests can still fail due to network or other problems! Also |
348 | note that "pace" and "wait" are NOT thread safe. | 347 | note that "pace" and "wait" are NOT thread safe. |
349 | 348 | ||
350 | Specify `api_base_url` if you wish to talk to an instance other than the flagship one. When | ||
351 | reading from client id or access token files as written by Mastodon.py 1.5.0 or larger, | ||
352 | this can be omitted. | ||
353 | |||
354 | By default, a timeout of 300 seconds is used for all requests. If you wish to change this, | 349 | By default, a timeout of 300 seconds is used for all requests. If you wish to change this, |
355 | pass the desired timeout (in seconds) as `request_timeout`. | 350 | pass the desired timeout (in seconds) as `request_timeout`. |
356 | 351 | ||
@@ -378,9 +373,9 @@ class Mastodon: | |||
378 | 373 | ||
379 | If no other `User-Agent` is specified, "mastodonpy" will be used. | 374 | If no other `User-Agent` is specified, "mastodonpy" will be used. |
380 | """ | 375 | """ |
381 | self.api_base_url = None | 376 | if api_base_url is None: |
382 | if api_base_url is not None: | 377 | raise MastodonIllegalArgumentError("API base URL is required.") |
383 | self.api_base_url = Mastodon.__protocolize(api_base_url) | 378 | self.api_base_url = Mastodon.__protocolize(api_base_url) |
384 | 379 | ||
385 | self.client_id = client_id | 380 | self.client_id = client_id |
386 | self.client_secret = client_secret | 381 | self.client_secret = client_secret |