diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 23 |
1 files changed, 17 insertions, 6 deletions
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 + '".') |