aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2017-12-11 11:49:14 +0100
committerLorenz Diener <[email protected]>2017-12-11 11:49:14 +0100
commit3ba917d363fd019fee009955cbae385b3b84319b (patch)
tree8bc687951141e818b0fc0c3ca6b55a96bc911307 /mastodon/Mastodon.py
parentedb3aaa28c9c847271b63bca563b4adfb891b3a2 (diff)
downloadmastodon.py-3ba917d363fd019fee009955cbae385b3b84319b.tar.gz
Add versionind decorator
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r--mastodon/Mastodon.py36
1 files changed, 34 insertions, 2 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 83a8081..06c436d 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -18,6 +18,7 @@ import copy
18import threading 18import threading
19import sys 19import sys
20import six 20import six
21from decorator import decorate
21 22
22try: 23try:
23 from urllib.parse import urlparse 24 from urllib.parse import urlparse
@@ -25,6 +26,24 @@ except ImportError:
25 from urlparse import urlparse 26 from urlparse import urlparse
26 27
27 28
29"""
30Version check decorator
31"""
32def api_version(version):
33 def api_min_version_decorator(function):
34 def wrapper(self, *args, **kwargs):
35 major, minor, patch = list(map(int, version.split(".")))
36 if major > self.mastodon_major:
37 raise MastodonVersionError("Specified version does not support this API endpoint (Available from " + version + ")")
38 elif minor > self.mastodon_minor:
39 raise MastodonVersionError("Specified version does not support this API endpoint (Available from " + version + ")")
40 elif patch > self.mastodon_patch:
41 raise MastodonVersionError("Specified version does not support this API endpoint (Available from " + version + ")")
42 function(self, *args, **kwargs)
43 function.__doc__ = function.__doc__ + "\n\n *Minumum Mastodon version: " + version + "*"
44 return decorate(function, wrapper)
45 return api_min_version_decorator
46
28class Mastodon: 47class Mastodon:
29 """ 48 """
30 Super basic but thorough and easy to use Mastodon 49 Super basic but thorough and easy to use Mastodon
@@ -87,7 +106,7 @@ class Mastodon:
87 def __init__(self, client_id, client_secret=None, access_token=None, 106 def __init__(self, client_id, client_secret=None, access_token=None,
88 api_base_url=__DEFAULT_BASE_URL, debug_requests=False, 107 api_base_url=__DEFAULT_BASE_URL, debug_requests=False,
89 ratelimit_method="wait", ratelimit_pacefactor=1.1, 108 ratelimit_method="wait", ratelimit_pacefactor=1.1,
90 request_timeout=__DEFAULT_TIMEOUT): 109 request_timeout=__DEFAULT_TIMEOUT, version="2.0.0"):
91 """ 110 """
92 Create a new API wrapper instance based on the given client_secret and client_id. If you 111 Create a new API wrapper instance based on the given client_secret and client_id. If you
93 give a client_id and it is not a file, you must also give a secret. 112 give a client_id and it is not a file, you must also give a secret.
@@ -108,6 +127,10 @@ class Mastodon:
108 127
109 By default, a timeout of 300 seconds is used for all requests. If you wish to change this, 128 By default, a timeout of 300 seconds is used for all requests. If you wish to change this,
110 pass the desired timeout (in seconds) as request_timeout. 129 pass the desired timeout (in seconds) as request_timeout.
130
131 The version parameter can be used to specify the version of Mastodon that Mastodon.py will
132 expect to be installed on the server. The function will throw an error if an unparseable
133 Version is specified. By default, Mastodon.py assumes the latest supported version.
111 """ 134 """
112 self.api_base_url = Mastodon.__protocolize(api_base_url) 135 self.api_base_url = Mastodon.__protocolize(api_base_url)
113 self.client_id = client_id 136 self.client_id = client_id
@@ -126,9 +149,14 @@ class Mastodon:
126 149
127 self.request_timeout = request_timeout 150 self.request_timeout = request_timeout
128 151
152 try:
153 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = list(map(int, version.split(".")))
154 except:
155 raise MastodonVersionError("Bad version specified")
156
129 if ratelimit_method not in ["throw", "wait", "pace"]: 157 if ratelimit_method not in ["throw", "wait", "pace"]:
130 raise MastodonIllegalArgumentError("Invalid ratelimit method.") 158 raise MastodonIllegalArgumentError("Invalid ratelimit method.")
131 159
132 if os.path.isfile(self.client_id): 160 if os.path.isfile(self.client_id):
133 with open(self.client_id, 'r') as secret_file: 161 with open(self.client_id, 'r') as secret_file:
134 self.client_id = secret_file.readline().rstrip() 162 self.client_id = secret_file.readline().rstrip()
@@ -1412,6 +1440,10 @@ class MastodonError(Exception):
1412 """Base class for Mastodon.py exceptions""" 1440 """Base class for Mastodon.py exceptions"""
1413 1441
1414 1442
1443class MastodonVersionError(MastodonError):
1444 """Raised when a function is called that the version of Mastodon for which
1445 Mastodon.py was instantiated does not support"""
1446
1415class MastodonIllegalArgumentError(ValueError, MastodonError): 1447class MastodonIllegalArgumentError(ValueError, MastodonError):
1416 """Raised when an incorrect parameter is passed to a function""" 1448 """Raised when an incorrect parameter is passed to a function"""
1417 pass 1449 pass
Powered by cgit v1.2.3 (git 2.41.0)