def proxy(self, request, *args, **kwargs): url = self.get_request_url(request) params = self.get_request_params(request) data = self.get_request_data(request) files = self.get_request_files(request) headers = self.get_headers(request) verify_ssl = self.get_verify_ssl(request) cookies = self.get_cookies(request) try: if files: """ By default requests library uses chunked upload for files but it is much more easier for servers to handle streamed uploads. This new implementation is also lightweight as files are not read entirely into memory. """ boundary = generate_boundary() headers['Content-Type'] = 'multipart/form-data; boundary=%s' % boundary body = StreamingMultipart(data, files, boundary) session = sessions.Session() session.mount('http://', StreamingHTTPAdapter()) session.mount('https://', StreamingHTTPAdapter()) response = session.request(request.method, url, params=params, data=body, headers=headers, timeout=self.proxy_settings.TIMEOUT, verify=verify_ssl, cookies=cookies) else: response = requests.request(request.method, url, params=params, data=data, files=files, headers=headers, timeout=self.proxy_settings.TIMEOUT, verify=verify_ssl, cookies=cookies) except (ConnectionError, SSLError): status = requests.status_codes.codes.bad_gateway return self.create_error_response({ 'code': status, 'error': 'Bad gateway', }, status) except (Timeout): status = requests.status_codes.codes.gateway_timeout return self.create_error_response({ 'code': status, 'error': 'Gateway timed out', }, status) return self.create_response(response)
def proxy(self, request): url = self.get_request_url(request) params = self.get_request_params(request) data = self.get_request_data(request) files = self.get_request_files(request) headers = self.get_headers(request) try: if files: """ By default requests library uses chunked upload for files but it is much more easier for servers to handle streamed uploads. This new implementation is also lightweight as files are not read entirely into memory. """ boundary = generate_boundary() headers[ 'Content-Type'] = 'multipart/form-data; boundary=%s' % boundary body = StreamingMultipart(data, files, boundary) session = sessions.Session() session.mount('http://', StreamingHTTPAdapter()) session.mount('https://', StreamingHTTPAdapter()) response = session.request(request.method, url, params=params, data=body, headers=headers, timeout=self.proxy_settings.TIMEOUT) else: response = requests.request( request.method, url, params=params, data=data, files=files, headers=headers, timeout=self.proxy_settings.TIMEOUT) except (ConnectionError, SSLError): status = requests.status_codes.codes.bad_gateway return self.create_error_response( { 'code': status, 'error': 'Bad gateway', }, status) except (Timeout): status = requests.status_codes.codes.gateway_timeout return self.create_error_response( { 'code': status, 'error': 'Gateway timed out', }, status) return self.create_response(response)
def proxy(self, request, *args, **kwargs): url = self.get_request_url(request) params = self.get_request_params(request) data = self.get_request_data(request) files = self.get_request_files(request) headers = self.get_headers(request) verify_ssl = self.get_verify_ssl(request) cookies = self.get_cookies(request) try: if files: boundary = generate_boundary() headers[ "Content-Type"] = f"multipart/form-data; boundary={boundary}" body = CustomStreamingMultipart(data, files, boundary) session = sessions.Session() session.mount("http://", StreamingHTTPAdapter()) session.mount("https://", StreamingHTTPAdapter()) response = session.request( request.method, url, params=params, data=body, headers=headers, timeout=self.proxy_settings.TIMEOUT, verify=verify_ssl, cookies=cookies, ) else: headers = { "Content-Type": "application/json", "Accept": "application/json" } if not isinstance(data, str): data = json.dumps(data) response = requests.request( request.method, url, params=params, data=data, files=files, headers=headers, timeout=self.proxy_settings.TIMEOUT, verify=verify_ssl, cookies=cookies, ) except (ConnectionError, SSLError): status = requests.status_codes.codes.bad_gateway return self.create_error_response( { "code": status, "error": "Bad gateway" }, status) except Timeout: status = requests.status_codes.codes.gateway_timeout return self.create_error_response( { "code": status, "error": "Gateway timed out" }, status) return self.create_response(response)