diff options
author | Lorenz Diener <[email protected]> | 2019-05-08 17:43:55 +0200 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2019-05-08 17:43:55 +0200 |
commit | 8675b25dcfc4a0da1e2dbcff07a359a83d32dd4a (patch) | |
tree | e8954891f28afa5d30a70b94379e02c6d10670d4 | |
parent | 35c43562dd3d34d6ebf7a0f757c09e8fcccc957c (diff) | |
download | mastodon.py-8675b25dcfc4a0da1e2dbcff07a359a83d32dd4a.tar.gz |
Improce search() versioning
-rw-r--r-- | docs/index.rst | 3 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 39 |
2 files changed, 34 insertions, 8 deletions
diff --git a/docs/index.rst b/docs/index.rst index 9988a5f..637e771 100644 --- a/docs/index.rst +++ b/docs/index.rst | |||
@@ -780,6 +780,9 @@ Mastodon.py throws a `MastodonVersionError`. | |||
780 | 780 | ||
781 | With the following functions, you can make Mastodon.py re-check the server | 781 | With the following functions, you can make Mastodon.py re-check the server |
782 | version or explicitly determine if a specific minimum Version is available. | 782 | version or explicitly determine if a specific minimum Version is available. |
783 | Long-running applications that aim to support multiple Mastodon versions | ||
784 | should do this from time to time in case a server they are running against | ||
785 | updated. | ||
783 | 786 | ||
784 | .. automethod:: Mastodon.retrieve_mastodon_version | 787 | .. automethod:: Mastodon.retrieve_mastodon_version |
785 | .. automethod:: Mastodon.verify_minimum_version | 788 | .. automethod:: Mastodon.verify_minimum_version |
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 27acbaa..ebdeffc 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -356,13 +356,16 @@ class Mastodon: | |||
356 | self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version_str) | 356 | self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version_str) |
357 | return version_str | 357 | return version_str |
358 | 358 | ||
359 | def verify_minimum_version(self, version_str): | 359 | def verify_minimum_version(self, version_str, cached=False): |
360 | """ | 360 | """ |
361 | Update version info from server and verify that at least the specified version is present. | 361 | Update version info from server and verify that at least the specified version is present. |
362 | 362 | ||
363 | If you specify "cached", the version info update part is skipped. | ||
364 | |||
363 | Returns True if version requirement is satisfied, False if not. | 365 | Returns True if version requirement is satisfied, False if not. |
364 | """ | 366 | """ |
365 | self.retrieve_mastodon_version() | 367 | if not cached: |
368 | self.retrieve_mastodon_version() | ||
366 | major, minor, patch = parse_version_string(version_str) | 369 | major, minor, patch = parse_version_string(version_str) |
367 | if major > self.mastodon_major: | 370 | if major > self.mastodon_major: |
368 | return False | 371 | return False |
@@ -1106,7 +1109,16 @@ class Mastodon: | |||
1106 | ### | 1109 | ### |
1107 | # Reading data: Searching | 1110 | # Reading data: Searching |
1108 | ### | 1111 | ### |
1109 | @api_version("2.8.0", "2.8.0", __DICT_VERSION_SEARCHRESULT) | 1112 | def __ensure_search_params_acceptable(self, account_id, offset, min_id, max_id): |
1113 | """ | ||
1114 | Internal Helper: Throw a MastodonVersionError if version is < 2.8.0 but parameters | ||
1115 | for search that are available only starting with 2.8.0 are specified. | ||
1116 | """ | ||
1117 | if not account_id is None or not offset is None or not min_id is None or not max_id is None: | ||
1118 | if self.verify_minimum_version("2.8.0", cached=True) == False: | ||
1119 | raise MastodonVersionError("Advanced search parameters require Mastodon 2.8.0+") | ||
1120 | |||
1121 | @api_version("1.1.0", "2.8.0", __DICT_VERSION_SEARCHRESULT) | ||
1110 | def search(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None): | 1122 | def search(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None): |
1111 | """ | 1123 | """ |
1112 | Fetch matching hashtags, accounts and statuses. Will perform webfinger | 1124 | Fetch matching hashtags, accounts and statuses. Will perform webfinger |
@@ -1121,12 +1133,22 @@ class Mastodon: | |||
1121 | 1133 | ||
1122 | `offset`, `min_id` and `max_id` can be used to paginate. | 1134 | `offset`, `min_id` and `max_id` can be used to paginate. |
1123 | 1135 | ||
1136 | Will use search_v1 (no tag dicts in return values) on Mastodon versions before | ||
1137 | 2.4.1), search_v2 otherwise. Parameters other than resolve are only available | ||
1138 | on Mastodon 2.8.0 or above - this function will throw a MastodonVersionError | ||
1139 | if you try to use them on versions before that. Note that the cached version | ||
1140 | number will be used for this to avoid uneccesary requests. | ||
1141 | |||
1124 | Returns a `search result dict`_, with tags as `hashtag dicts`_. | 1142 | Returns a `search result dict`_, with tags as `hashtag dicts`_. |
1125 | """ | 1143 | """ |
1126 | return self.search_v2(q, resolve=resolve, result_type=result_type, account_id=account_id, | 1144 | if self.verify_minimum_version("2.4.1", cached=True) == True: |
1127 | offset=offset, min_id=min_id, max_id=max_id) | 1145 | return self.search_v2(q, resolve=resolve, result_type=result_type, account_id=account_id, |
1128 | 1146 | offset=offset, min_id=min_id, max_id=max_id) | |
1129 | @api_version("1.1.0", "2.1.0", __DICT_VERSION_SEARCHRESULT) | 1147 | else: |
1148 | self.__ensure_search_params_acceptable(account_id, offset, min_id, max_id) | ||
1149 | return self.search_v1(q, resolve=resolve) | ||
1150 | |||
1151 | @api_version("1.1.0", "2.1.0", "2.1.0") | ||
1130 | def search_v1(self, q, resolve=False): | 1152 | def search_v1(self, q, resolve=False): |
1131 | """ | 1153 | """ |
1132 | Identical to `search_v2()`, except in that it does not return | 1154 | Identical to `search_v2()`, except in that it does not return |
@@ -1139,7 +1161,7 @@ class Mastodon: | |||
1139 | del params['resolve'] | 1161 | del params['resolve'] |
1140 | return self.__api_request('GET', '/api/v1/search', params) | 1162 | return self.__api_request('GET', '/api/v1/search', params) |
1141 | 1163 | ||
1142 | @api_version("2.8.0", "2.8.0", __DICT_VERSION_SEARCHRESULT) | 1164 | @api_version("2.4.1", "2.8.0", __DICT_VERSION_SEARCHRESULT) |
1143 | def search_v2(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None): | 1165 | def search_v2(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None): |
1144 | """ | 1166 | """ |
1145 | Identical to `search_v1()`, except in that it returns tags as | 1167 | Identical to `search_v1()`, except in that it returns tags as |
@@ -1147,6 +1169,7 @@ class Mastodon: | |||
1147 | 1169 | ||
1148 | Returns a `search result dict`_. | 1170 | Returns a `search result dict`_. |
1149 | """ | 1171 | """ |
1172 | self.__ensure_search_params_acceptable(account_id, offset, min_id, max_id) | ||
1150 | params = self.__generate_params(locals()) | 1173 | params = self.__generate_params(locals()) |
1151 | 1174 | ||
1152 | if resolve == False: | 1175 | if resolve == False: |