def get_response(wsgi_request): ''' Given a WSGI request, makes a call to a corresponding view function and returns the response. ''' service_start_time = datetime.now() # Get the view / handler for this request view, args, kwargs = resolve(wsgi_request.path_info) kwargs.update({"request": wsgi_request}) # Let the view do his task. try: resp = view(*args, **kwargs) except Exception as exc: resp = HttpResponseServerError(content=exc.message) headers = dict(resp._headers.values()) # Convert HTTP response into simple dict type. d_resp = {"status_code": resp.status_code, "reason_phrase": resp.reason_phrase, "headers": headers} try: d_resp.update({"body": resp.content}) except ContentNotRenderedError: resp.render() d_resp.update({"body": resp.content}) # Check if we need to send across the duration header. if _settings.ADD_DURATION_HEADER: d_resp['headers'].update({_settings.DURATION_HEADER_NAME: (datetime.now() - service_start_time).seconds}) return d_resp
def get_response(wsgi_request): ''' Given a WSGI request, makes a call to a corresponding view function and returns the response. ''' # Get the view / handler for this request view, args, kwargs = resolve(wsgi_request.path_info) kwargs.update({"request": wsgi_request}) # Let the view do his task. try: resp = view(*args, **kwargs) except Exception as exc: resp = HttpResponseServerError(content=exc.message) headers = dict(resp._headers.values()) # Convert HTTP response into simple dict type. d_resp = {"status_code": resp.status_code, "reason_phrase": resp.reason_phrase, "headers": headers} try: d_resp.update({"body": resp.content}) except ContentNotRenderedError: resp.render() d_resp.update({"body": resp.content}) return d_resp
def get_response(wsgi_request): ''' Given a WSGI request, makes a call to a corresponding view function and returns the response. ''' service_start_time = datetime.now() # Get the view / handler for this request view, args, kwargs = resolve(wsgi_request.path_info) kwargs.update({"request": wsgi_request}) # Let the view do his task. try: resp = view(*args, **kwargs) except Exception as exc: resp = HttpResponseServerError(content=str(exc)) headers = dict(resp._headers.values()) # Convert HTTP response into simple dict type. d_resp = {"status_code": resp.status_code, "reason_phrase": resp.reason_phrase, "headers": headers} try: d_resp.update({"body": resp.content.decode(resp.charset)}) except ContentNotRenderedError: resp.render() d_resp.update({"body": resp.content.decode(resp.charset)}) # Check if we need to send across the duration header. if _settings.ADD_DURATION_HEADER: d_resp['headers'].update({_settings.DURATION_HEADER_NAME: (datetime.now() - service_start_time).seconds}) return d_resp
def get_response(wsgi_request): ''' Given a WSGI request, makes a call to a corresponding view function and returns the response. ''' # Get the view / handler for this request try: view, args, kwargs = resolve(wsgi_request.path_info) except Http404 as error: return {'status_code': 404, 'reason_phrase': 'Page not found'} # Let the view do his task. kwargs.update({'request': wsgi_request}) try: response = view(*args, **kwargs) except Exception as exc: response = HttpResponseServerError(content=str(exc)) # Convert HTTP response into simple dict type. result = { 'status_code': response.status_code, 'reason_phrase': response.reason_phrase, 'headers': dict(response._headers.values()), } # There's some kind of bug here where JSON-API calls are getting # their content-type changed to "text/html". This interferes with # clients detecting and decoding properly. If the request is in # JSON-API, so too shall be the response. # TODO: Figure out what's going on here. if wsgi_request.content_type.startswith('application/vnd.api+json'): result['headers']['Content-Type'] = 'application/vnd.api+json' # Make sure that the response has been rendered if hasattr(response, 'render') and callable(response.render): response.render() content = response.content if isinstance(content, bytes): content = content.decode('utf-8') try: content = json.loads(content) except json.JSONDecodeError: pass result['body'] = content return result