<s> import requests <EOL> from webservices . models import BaseConsumer <EOL> class SyncConsumer ( BaseConsumer ) : <EOL> def __init__ ( self , base_url , public_key , private_key ) : <EOL> super ( SyncConsumer , self ) . __init__ ( base_url , public_key , private_key ) <EOL> self . session = requests . session ( ) <EOL> def send_request ( self , url , data , headers ) : <EOL> response = self . session . post ( url , data = data , headers = headers ) <EOL> self . raise_for_status ( response . status_code , response . content ) <EOL> return response . content <EOL> class DjangoTestingConsumer ( SyncConsumer ) : <EOL> def __init__ ( self , test_client , base_url , public_key , private_key ) : <EOL> self . test_client = test_client <EOL> super ( DjangoTestingConsumer , self ) . __init__ ( <EOL> base_url , public_key , private_key ) <EOL> def build_url ( self , path ) : <EOL> return path <EOL> def send_request ( self , url , data , headers ) : <EOL> headers = { <EOL> '<STR_LIT>' % header . upper ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) : value <EOL> for header , value in headers . items ( ) <EOL> } <EOL> response = self . test_client . post ( <EOL> url , <EOL> data = data , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ** headers <EOL> ) <EOL> self . raise_for_status ( response . status_code , response . content ) <EOL> return response . content <EOL> class FlaskTestingConsumer ( DjangoTestingConsumer ) : <EOL> def send_request ( self , url , data , headers ) : <EOL> response = self . test_client . post ( url , data = data , headers = headers ) <EOL> self . raise_for_status ( response . status_code , response . data ) <EOL> return response . data <EOL> def provider_for_django ( provider ) : <EOL> from django . http import HttpResponse <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> def provider_view ( request ) : <EOL> def get_header ( key , default ) : <EOL> django_key = '<STR_LIT>' % key . upper ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> return request . META . get ( django_key , default ) <EOL> method = request . method <EOL> if getattr ( request , '<STR_LIT:body>' , None ) : <EOL> signed_data = request . body <EOL> else : <EOL> signed_data = request . raw_post_data <EOL> status_code , data = provider . get_response ( <EOL> method , <EOL> signed_data , <EOL> get_header , <EOL> ) <EOL> return HttpResponse ( data , status = status_code ) <EOL> return csrf_exempt ( provider_view ) <EOL> def provider_for_flask ( app , url , provider ) : <EOL> from flask import request <EOL> def provider_view ( ) : <EOL> def get_header ( key , default ) : <EOL> return request . headers . get ( key , default ) <EOL> method = request . method <EOL> signed_data = request . data <EOL> status_code , data = provider . get_response ( <EOL> method , <EOL> signed_data , <EOL> get_header , <EOL> ) <EOL> return data , status_code <EOL> return app . route ( url , methods = [ '<STR_LIT:POST>' ] ) ( provider_view ) </s>
