diff options
author | Lorenz Diener <[email protected]> | 2018-11-26 11:47:21 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2018-11-26 11:47:21 +0100 |
commit | 8b8626978752baf14347498640b2319db832145e (patch) | |
tree | db9e962df8ed4c9a8d83eb239f81daf439132b9d /mastodon | |
parent | 9c5c5b85cc1bf957f1b27f8105942b98e1d650c5 (diff) | |
parent | 975145ada615ab5262b7bee6706023843daea7e8 (diff) | |
download | mastodon.py-8b8626978752baf14347498640b2319db832145e.tar.gz |
Merge pull request #151 from jrabbit/seven_proxies
Made Session support more robust and added support to .create_app()
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 1735583..fc585ba 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -185,7 +185,7 @@ class Mastodon: | |||
185 | ### | 185 | ### |
186 | @staticmethod | 186 | @staticmethod |
187 | def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None, | 187 | def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None, |
188 | api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT): | 188 | api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT, session=None): |
189 | """ | 189 | """ |
190 | Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push" | 190 | Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push" |
191 | - more granular scopes are available, please refere to Mastodon documentation for which). | 191 | - more granular scopes are available, please refere to Mastodon documentation for which). |
@@ -194,6 +194,8 @@ class Mastodon: | |||
194 | Specify `to_file` to persist your apps info to a file so you can use them in the constructor. | 194 | Specify `to_file` to persist your apps info to a file so you can use them in the constructor. |
195 | Specify `api_base_url` if you want to register an app on an instance different from the flagship one. | 195 | Specify `api_base_url` if you want to register an app on an instance different from the flagship one. |
196 | 196 | ||
197 | Specify `session` with a requests.Session for it to be used instead of the deafult. | ||
198 | |||
197 | Presently, app registration is open by default, but this is not guaranteed to be the case for all | 199 | Presently, app registration is open by default, but this is not guaranteed to be the case for all |
198 | future mastodon instances or even the flagship instance in the future. | 200 | future mastodon instances or even the flagship instance in the future. |
199 | 201 | ||
@@ -213,9 +215,12 @@ class Mastodon: | |||
213 | request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob' | 215 | request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob' |
214 | if website is not None: | 216 | if website is not None: |
215 | request_data['website'] = website | 217 | request_data['website'] = website |
216 | 218 | if session: | |
217 | response = requests.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) | 219 | ret = session.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) |
218 | response = response.json() | 220 | response = ret.json() |
221 | else: | ||
222 | response = requests.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) | ||
223 | response = response.json() | ||
219 | except Exception as e: | 224 | except Exception as e: |
220 | raise MastodonNetworkError("Could not complete request: %s" % e) | 225 | raise MastodonNetworkError("Could not complete request: %s" % e) |
221 | 226 | ||
@@ -233,7 +238,7 @@ class Mastodon: | |||
233 | api_base_url=__DEFAULT_BASE_URL, debug_requests=False, | 238 | api_base_url=__DEFAULT_BASE_URL, debug_requests=False, |
234 | ratelimit_method="wait", ratelimit_pacefactor=1.1, | 239 | ratelimit_method="wait", ratelimit_pacefactor=1.1, |
235 | request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, | 240 | request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, |
236 | version_check_mode = "created"): | 241 | version_check_mode = "created", session=None): |
237 | """ | 242 | """ |
238 | Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you | 243 | Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you |
239 | give a `client_id` and it is not a file, you must also give a secret. If you specify an | 244 | give a `client_id` and it is not a file, you must also give a secret. If you specify an |
@@ -255,7 +260,9 @@ class Mastodon: | |||
255 | 260 | ||
256 | By default, a timeout of 300 seconds is used for all requests. If you wish to change this, | 261 | By default, a timeout of 300 seconds is used for all requests. If you wish to change this, |
257 | pass the desired timeout (in seconds) as `request_timeout`. | 262 | pass the desired timeout (in seconds) as `request_timeout`. |
258 | 263 | ||
264 | For fine-tuned control over the requests object use `session` with a requests.Session. | ||
265 | |||
259 | The `mastodon_version` parameter can be used to specify the version of Mastodon that Mastodon.py will | 266 | The `mastodon_version` parameter can be used to specify the version of Mastodon that Mastodon.py will |
260 | expect to be installed on the server. The function will throw an error if an unparseable | 267 | expect to be installed on the server. The function will throw an error if an unparseable |
261 | Version is specified. If no version is specified, Mastodon.py will set `mastodon_version` to the | 268 | Version is specified. If no version is specified, Mastodon.py will set `mastodon_version` to the |
@@ -286,7 +293,10 @@ class Mastodon: | |||
286 | 293 | ||
287 | self.request_timeout = request_timeout | 294 | self.request_timeout = request_timeout |
288 | 295 | ||
289 | self.session = requests.Session() | 296 | if session: |
297 | self.session = session | ||
298 | else: | ||
299 | self.session = requests.Session() | ||
290 | 300 | ||
291 | # Versioning | 301 | # Versioning |
292 | if mastodon_version == None: | 302 | if mastodon_version == None: |