diff options
-rw-r--r-- | DEVELOPMENT.md | 6 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 23 |
2 files changed, 20 insertions, 9 deletions
diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 5644dea..999550d 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md | |||
@@ -3,7 +3,7 @@ Here's some general stuff to keep in mind, and some work that needs to be done | |||
3 | * Mastodon.py tries to work for python2 as well as python3, so avoid things like annotations, | 3 | * Mastodon.py tries to work for python2 as well as python3, so avoid things like annotations, |
4 | use requests over urllib, et cetera. | 4 | use requests over urllib, et cetera. |
5 | 5 | ||
6 | * Current TODOs (2.3 support): | 6 | * Current TODOs: |
7 | * Add support for idempotency keys | 7 | * Testing - test 2.3 stuff and verify it works, test pinning |
8 | * Document error handling better | 8 | * 2.4 support |
9 | 9 | ||
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index b9e880d..2adc82e 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -951,9 +951,10 @@ class Mastodon: | |||
951 | ### | 951 | ### |
952 | # Writing data: Statuses | 952 | # Writing data: Statuses |
953 | ### | 953 | ### |
954 | @api_version("1.0.0", "2.0.0", __DICT_VERSION_STATUS) | 954 | @api_version("1.0.0", "2.3.0", __DICT_VERSION_STATUS) |
955 | def status_post(self, status, in_reply_to_id=None, media_ids=None, | 955 | def status_post(self, status, in_reply_to_id=None, media_ids=None, |
956 | sensitive=False, visibility=None, spoiler_text=None): | 956 | sensitive=False, visibility=None, spoiler_text=None, |
957 | idempotency_key=None): | ||
957 | """ | 958 | """ |
958 | Post a status. Can optionally be in reply to another status and contain | 959 | Post a status. Can optionally be in reply to another status and contain |
959 | media. | 960 | media. |
@@ -983,6 +984,11 @@ class Mastodon: | |||
983 | the text of the status. If no text is passed in, no warning will be | 984 | the text of the status. If no text is passed in, no warning will be |
984 | displayed. | 985 | displayed. |
985 | 986 | ||
987 | You can set `idempotency_key` to a value to uniquely identify an attempt | ||
988 | at posting a status. Even if you call this function more than once, | ||
989 | if you call it with the same `idempotency_key`, only one status will | ||
990 | be created. | ||
991 | |||
986 | Returns a `toot dict`_ with the new status. | 992 | Returns a `toot dict`_ with the new status. |
987 | """ | 993 | """ |
988 | if in_reply_to_id != None: | 994 | if in_reply_to_id != None: |
@@ -1003,6 +1009,10 @@ class Mastodon: | |||
1003 | if params_initial['sensitive'] is False: | 1009 | if params_initial['sensitive'] is False: |
1004 | del [params_initial['sensitive']] | 1010 | del [params_initial['sensitive']] |
1005 | 1011 | ||
1012 | headers = {} | ||
1013 | if idempotency_key != None: | ||
1014 | headers['Idempotency-Key'] = idempotency_key | ||
1015 | |||
1006 | if media_ids is not None: | 1016 | if media_ids is not None: |
1007 | try: | 1017 | try: |
1008 | media_ids_proper = [] | 1018 | media_ids_proper = [] |
@@ -1019,8 +1029,8 @@ class Mastodon: | |||
1019 | 1029 | ||
1020 | params_initial["media_ids"] = media_ids_proper | 1030 | params_initial["media_ids"] = media_ids_proper |
1021 | 1031 | ||
1022 | params = self.__generate_params(params_initial) | 1032 | params = self.__generate_params(params_initial, ['idempotency_key']) |
1023 | return self.__api_request('POST', '/api/v1/statuses', params) | 1033 | return self.__api_request('POST', '/api/v1/statuses', params, headers = headers) |
1024 | 1034 | ||
1025 | @api_version("1.0.0", "2.0.0", __DICT_VERSION_STATUS) | 1035 | @api_version("1.0.0", "2.0.0", __DICT_VERSION_STATUS) |
1026 | def toot(self, status): | 1036 | def toot(self, status): |
@@ -1640,7 +1650,7 @@ class Mastodon: | |||
1640 | json_object = Mastodon.__json_allow_dict_attrs(json_object) | 1650 | json_object = Mastodon.__json_allow_dict_attrs(json_object) |
1641 | return json_object | 1651 | return json_object |
1642 | 1652 | ||
1643 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): | 1653 | def __api_request(self, method, endpoint, params={}, files={}, headers={}, do_ratelimiting=True): |
1644 | """ | 1654 | """ |
1645 | Internal API request helper. | 1655 | Internal API request helper. |
1646 | """ | 1656 | """ |
@@ -1667,8 +1677,9 @@ class Mastodon: | |||
1667 | time.sleep(to_next) | 1677 | time.sleep(to_next) |
1668 | 1678 | ||
1669 | # Generate request headers | 1679 | # Generate request headers |
1680 | headers = copy.deepcopy(headers) | ||
1670 | if self.access_token is not None: | 1681 | if self.access_token is not None: |
1671 | headers = {'Authorization': 'Bearer ' + self.access_token} | 1682 | headers['Authorization'] = 'Bearer ' + self.access_token |
1672 | 1683 | ||
1673 | if self.debug_requests: | 1684 | if self.debug_requests: |
1674 | print('Mastodon: Request to endpoint "' + endpoint + '" using method "' + method + '".') | 1685 | print('Mastodon: Request to endpoint "' + endpoint + '" using method "' + method + '".') |