mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 14:03:30 +00:00 
			
		
		
		
	event_queue: Add requests 1.x compatibility.
The previous version of our code only worked with python-requests < 1.0 (as is the case on our servers), the new version will work with any python-requests new enough to have a .json at all. (imported from commit 77ffe3e0d890fe88776c313e0e3289aee1bb30ea)
This commit is contained in:
		@@ -39,7 +39,7 @@ __version__ = "0.1.5"
 | 
			
		||||
# Older versions don't provide the 'json' attribute on responses.
 | 
			
		||||
assert(LooseVersion(requests.__version__) >= LooseVersion('0.12.1'))
 | 
			
		||||
# In newer versions, the 'json' attribute is a function, not a property
 | 
			
		||||
requests_json_is_function = not isinstance(requests.Response.json, property)
 | 
			
		||||
requests_json_is_function = callable(requests.Response.json)
 | 
			
		||||
 | 
			
		||||
API_VERSTRING = "/api/v1/"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -205,6 +205,15 @@ def setup_event_queue():
 | 
			
		||||
 | 
			
		||||
# The following functions are called from Django
 | 
			
		||||
 | 
			
		||||
# Workaround to support the Python-requests 1.0 transition of .json
 | 
			
		||||
# from a property to a function
 | 
			
		||||
requests_json_is_function = callable(requests.Response.json)
 | 
			
		||||
def extract_json_response(resp):
 | 
			
		||||
    if requests_json_is_function:
 | 
			
		||||
        return resp.json()
 | 
			
		||||
    else:
 | 
			
		||||
        return resp.json
 | 
			
		||||
 | 
			
		||||
def request_event_queue(user_profile, apply_markdown, event_types=None):
 | 
			
		||||
    if settings.TORNADO_SERVER:
 | 
			
		||||
        req = {'dont_block'    : 'true',
 | 
			
		||||
@@ -219,7 +228,7 @@ def request_event_queue(user_profile, apply_markdown, event_types=None):
 | 
			
		||||
 | 
			
		||||
        resp.raise_for_status()
 | 
			
		||||
 | 
			
		||||
        return resp.json['queue_id']
 | 
			
		||||
        return extract_json_response(resp)['queue_id']
 | 
			
		||||
 | 
			
		||||
    return None
 | 
			
		||||
 | 
			
		||||
@@ -235,4 +244,4 @@ def get_user_events(user_profile, queue_id, last_event_id):
 | 
			
		||||
 | 
			
		||||
        resp.raise_for_status()
 | 
			
		||||
 | 
			
		||||
        return resp.json['events']
 | 
			
		||||
        return extract_json_response(resp)['events']
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user