def application(request): req_id_header = '' for header in request.headers: if header[0].lower() == 'x-request-id': req_id_header = header print(header[0], ': ', header[1]) headers = Headers() headers.add_header("X_Request_Id", req_id_header[1]) return Response("Hello, World! == {}".format(req_id_header), headers=headers)
def respaldo(): r = requests.get( 'http://' + apiURL + '/backup', stream=True, headers={'Authentication-Token': session['api_session_token']}) headers = Headers() headers.add_header('Content-Type', r.headers["content-type"]) headers.add_header( 'Content-Disposition', 'attachment; filename="' + 'respaldo-' + time.strftime("%Y%m%d-%H%M%S") + '.db"') return Response(stream_with_context(r.iter_content(chunk_size=2048)), headers=headers)
def exportar(): r = requests.get( 'http://' + apiURL + '/exports?filename=' + request.args['fecha'], stream=True, headers={'Authentication-Token': session['api_session_token']}) headers = Headers() headers.add_header('Content-Type', r.headers["content-type"]) headers.add_header( 'Content-Disposition', 'attachment; filename="' + 'exportacion-' + time.strftime("%Y%m%d-%H%M%S") + '.xls"') return Response(stream_with_context(r.iter_content(chunk_size=2048)), headers=headers)
def set_adsws_uid_header(valid, oauth): """ If the user is authenticated, inject the header "X-adsws-uid" into the incoming request header """ if current_user.is_authenticated(): h = Headers(request.headers.items()) h.add_header("X-Adsws-Uid", current_user.id) if current_user.ratelimit_level is not None: h.add_header("X-Adsws-Ratelimit-Level", current_user.ratelimit_level) request.headers = h return valid, oauth
def set_adsws_uid_header(valid, oauth): """ If the user is authenticated, inject the header "X-adsws-uid" into the incoming request header """ if current_user.is_authenticated(): h = Headers(request.headers.items()) h.add_header("X-Adsws-Uid", current_user.id) if current_user.ratelimit_level is not None: h.add_header( "X-Adsws-Ratelimit-Level", current_user.ratelimit_level ) request.headers = h return valid, oauth
def set_adsws_uid_header(valid, oauth): """ If the user is authenticated, inject the header "X-adsws-uid" into the incoming request header """ h = Headers(request.headers.items()) if current_user.is_authenticated(): h.add_header("X-Adsws-Uid", current_user.id) if valid: level = oauth.client.ratelimit if level is None: level = 1.0 h.add_header("X-Adsws-Ratelimit-Level", level) else: h.add_header("X-Adsws-Ratelimit-Level", 0.0) request.headers = h return valid, oauth
class Base: headers = None def set_jwt(self, jwt_item=None): if jwt_item is not None: item = jwt_item jwt_name = 'Bearer {}'.format(item) self.headers = Headers() self.headers.add_header('Authorization', jwt_name) def get(self, endpoint, jwt=None, **kwargs): url = url_for(endpoint, **kwargs) self.set_jwt(jwt) response = self.client.get(url, headers=self.headers) return response def post(self, endpoint, data, jwt=None, **kwargs): url = url_for(endpoint, **kwargs) self.set_jwt(jwt) response = self.client.post( url, data=json.dumps(data), content_type='application/json', headers=self.headers, ) return response def put(self, endpoint, data, jwt=None, **kwargs): url = url_for(endpoint, **kwargs) self.set_jwt(jwt) response = self.client.put( url, data=json.dumps(data), content_type='application/json', headers=self.headers, ) return response def patch(self, endpoint, data, csrf=None, **kwargs): url = url_for(endpoint, **kwargs) self.set_jwt(csrf) response = self.client.patch( url, data=json.dumps(data), content_type='application/json', headers=self.headers, ) return response def delete(self, endpoint, jwt=None, **kwargs): url = url_for(endpoint, **kwargs) self.set_jwt(jwt) response = self.client.delete(url, headers=self.headers) return response def login(self, endpoint, user, password): data = { 'email': user.email, 'password': password, } response = self.post(endpoint, data) self.access = response.json['access'] self.refresh = response.json['refresh'] self.set_jwt(self.access) return response
class Base: headers = None def set_csrf(self, csrf_item=None): if csrf_item is not None: csrf_name = 'csrf_{}_token'.format(csrf_item) csrf = getattr(self, csrf_name) self.headers = Headers() self.headers.add_header('X-CSRF-TOKEN', csrf) def get(self, endpoint): url = url_for(endpoint) return self.client.get(url) def post(self, endpoint, data, csrf=None): url = url_for(endpoint) self.set_csrf(csrf) response = self.client.post( url, data=json.dumps(data), content_type='application/json', headers=self.headers, ) self.headers = None return response def put(self, endpoint, data, csrf=None): url = url_for(endpoint) self.set_csrf(csrf) response = self.client.put( url, data=json.dumps(data), content_type='application/json', headers=self.headers, ) self.headers = None return response def patch(self, endpoint, data, csrf=None): url = url_for(endpoint) self.set_csrf(csrf) response = self.client.patch( url, data=json.dumps(data), content_type='application/json', ) self.headers = None return response def delete(self, endpoint): url = url_for(endpoint) return self.client.patch(url) def login(self, endpoint, user, password): data = { 'email': user.email, 'password': password, } response = self.post(endpoint, data) cookies = [ header for header in response.headers if header[0] == 'Set-Cookie' ] for cookie in cookies: name, value = cookie[1].split(';')[0].split('=') setattr(self, name, value) return response