aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Laxson <[email protected]>2018-11-18 04:55:04 -0500
committerJack Laxson <[email protected]>2018-11-18 04:55:04 -0500
commit975145ada615ab5262b7bee6706023843daea7e8 (patch)
tree9b52730a3ed02fdb214e7fb37ac107082c526b5a
parent191ad84cef0b37e8d8a708812336858d447304c5 (diff)
downloadmastodon.py-975145ada615ab5262b7bee6706023843daea7e8.tar.gz
Made Session support more robust and added support to .create_app()
-rw-r--r--mastodon/Mastodon.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index e6d8268..e890b6d 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -180,7 +180,7 @@ class Mastodon:
180 ### 180 ###
181 @staticmethod 181 @staticmethod
182 def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None, 182 def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None,
183 api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT): 183 api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT, session=None):
184 """ 184 """
185 Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push" 185 Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push"
186 - more granular scopes are available, please refere to Mastodon documentation for which). 186 - more granular scopes are available, please refere to Mastodon documentation for which).
@@ -189,6 +189,8 @@ class Mastodon:
189 Specify `to_file` to persist your apps info to a file so you can use them in the constructor. 189 Specify `to_file` to persist your apps info to a file so you can use them in the constructor.
190 Specify `api_base_url` if you want to register an app on an instance different from the flagship one. 190 Specify `api_base_url` if you want to register an app on an instance different from the flagship one.
191 191
192 Specify `session` with a requests.Session for it to be used instead of the deafult.
193
192 Presently, app registration is open by default, but this is not guaranteed to be the case for all 194 Presently, app registration is open by default, but this is not guaranteed to be the case for all
193 future mastodon instances or even the flagship instance in the future. 195 future mastodon instances or even the flagship instance in the future.
194 196
@@ -208,9 +210,12 @@ class Mastodon:
208 request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob' 210 request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob'
209 if website is not None: 211 if website is not None:
210 request_data['website'] = website 212 request_data['website'] = website
211 213 if session:
212 response = requests.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout) 214 ret = session.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout)
213 response = response.json() 215 response = ret.json()
216 else:
217 response = requests.post(api_base_url + '/api/v1/apps', data=request_data, timeout=request_timeout)
218 response = response.json()
214 except Exception as e: 219 except Exception as e:
215 raise MastodonNetworkError("Could not complete request: %s" % e) 220 raise MastodonNetworkError("Could not complete request: %s" % e)
216 221
@@ -228,7 +233,7 @@ class Mastodon:
228 api_base_url=__DEFAULT_BASE_URL, debug_requests=False, 233 api_base_url=__DEFAULT_BASE_URL, debug_requests=False,
229 ratelimit_method="wait", ratelimit_pacefactor=1.1, 234 ratelimit_method="wait", ratelimit_pacefactor=1.1,
230 request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, 235 request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None,
231 version_check_mode = "created"): 236 version_check_mode = "created", session=None):
232 """ 237 """
233 Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you 238 Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you
234 give a `client_id` and it is not a file, you must also give a secret. If you specify an 239 give a `client_id` and it is not a file, you must also give a secret. If you specify an
@@ -250,7 +255,9 @@ class Mastodon:
250 255
251 By default, a timeout of 300 seconds is used for all requests. If you wish to change this, 256 By default, a timeout of 300 seconds is used for all requests. If you wish to change this,
252 pass the desired timeout (in seconds) as `request_timeout`. 257 pass the desired timeout (in seconds) as `request_timeout`.
253 258
259 For fine-tuned control over the requests object use `session` with a requests.Session.
260
254 The `mastodon_version` parameter can be used to specify the version of Mastodon that Mastodon.py will 261 The `mastodon_version` parameter can be used to specify the version of Mastodon that Mastodon.py will
255 expect to be installed on the server. The function will throw an error if an unparseable 262 expect to be installed on the server. The function will throw an error if an unparseable
256 Version is specified. If no version is specified, Mastodon.py will set `mastodon_version` to the 263 Version is specified. If no version is specified, Mastodon.py will set `mastodon_version` to the
@@ -281,7 +288,10 @@ class Mastodon:
281 288
282 self.request_timeout = request_timeout 289 self.request_timeout = request_timeout
283 290
284 self.session = requests.Session() 291 if session:
292 self.session = session
293 else:
294 self.session = requests.Session()
285 295
286 # Versioning 296 # Versioning
287 if mastodon_version == None: 297 if mastodon_version == None:
Powered by cgit v1.2.3 (git 2.41.0)