diff options
author | Lorenz Diener <[email protected]> | 2017-09-17 18:00:38 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2017-09-17 18:00:38 +0200 |
commit | b6b503a1a9aac148642b9869d8ae5a3e882f6963 (patch) | |
tree | e22c2be5167209c94dabcce77066f69bf09c7b6d /mastodon | |
parent | 99249d153b355c99adf607afccf4d087bdbe522d (diff) | |
parent | 5b90a3c8302f7a13303f95d12eb3e3a7de635f4b (diff) | |
download | mastodon.py-b6b503a1a9aac148642b9869d8ae5a3e882f6963.tar.gz |
Merge pull request #85 from Elizafox/master
Workaround Mastodon issue with streaming API redirection
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index d427444..1fb7d69 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -1162,7 +1162,38 @@ class Mastodon: | |||
1162 | headers = {'Authorization': 'Bearer ' + self.access_token} | 1162 | headers = {'Authorization': 'Bearer ' + self.access_token} |
1163 | url = self.api_base_url + endpoint | 1163 | url = self.api_base_url + endpoint |
1164 | 1164 | ||
1165 | connection = requests.get(url, headers = headers, data = params, stream = True) | 1165 | # requests session subclass to disable stripping authorization headers |
1166 | class __no_auth_strip_session(requests.Session): | ||
1167 | def rebuild_auth(self, prepared_request, response): | ||
1168 | return | ||
1169 | |||
1170 | # Mastodon can be configured to use another address for streaming. | ||
1171 | # However, due to a bug, Mastodon does not issue 301 Permanently | ||
1172 | # Moved redirects for anything but /api/v1/streaming (including | ||
1173 | # subdirs), instead returning a 404 with no redirect and causing the | ||
1174 | # entire request to fail. | ||
1175 | # | ||
1176 | # The workaround is to hit /api/v1/streaming and see if there is a | ||
1177 | # redirection, and then use the domain it gives us to do the final | ||
1178 | # request. | ||
1179 | if endpoint.startswith("/api/v1/streaming"): | ||
1180 | stream_base = self.api_base_url + "/api/v1/streaming" | ||
1181 | |||
1182 | with __no_auth_strip_session() as session: | ||
1183 | connection = session.get(stream_base, headers = headers, data = params) | ||
1184 | |||
1185 | if connection.status_code not in (404, 200): | ||
1186 | # 404 is a normal error, raise on anything else | ||
1187 | raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) | ||
1188 | |||
1189 | url = connection.url.replace("/api/v1/streaming", endpoint) | ||
1190 | |||
1191 | with __no_auth_strip_session() as session: | ||
1192 | # Prevent stripping of authorisation headers on redirect | ||
1193 | connection = session.get(url, headers = headers, data = params, stream = True) | ||
1194 | |||
1195 | if connection.status_code != 200: | ||
1196 | raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) | ||
1166 | 1197 | ||
1167 | class __stream_handle(): | 1198 | class __stream_handle(): |
1168 | def __init__(self, connection): | 1199 | def __init__(self, connection): |