<s> """<STR_LIT>""" <EOL> from copy import copy <EOL> from django . http import Http404 <EOL> from django . conf import settings <EOL> from wapi . responses import get_response <EOL> from wapi . formatters import UnknownFormat <EOL> from wapi . exceptions import ApiError , ApiLoginRequired <EOL> from wapi . function import ApiFunction <EOL> from wapi . utils import api_iterate , get_instance <EOL> class Binding ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , api ) : <EOL> self . api = get_instance ( api ) <EOL> self . registry = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> for func in api_iterate ( api ) : <EOL> registered_name = self . mangle_name ( func . func_name ) <EOL> apifunc = ApiFunction ( func ) <EOL> if settings . DEBUG or apifunc . is_read : <EOL> self . registry [ '<STR_LIT>' ] [ registered_name ] = apifunc <EOL> if settings . DEBUG or apifunc . is_write : <EOL> self . registry [ '<STR_LIT>' ] [ registered_name ] = apifunc <EOL> def mangle_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return name <EOL> def get_method ( self , read_or_write , method_name ) : <EOL> try : <EOL> return self . registry [ read_or_write ] [ method_name ] <EOL> except KeyError : <EOL> raise Http404 ( '<STR_LIT>' % method_name ) <EOL> class RestBinding ( Binding ) : <EOL> """<STR_LIT>""" <EOL> PATTERN = '<STR_LIT>' <EOL> READ_WRITE_MAPPING = { <EOL> '<STR_LIT:GET>' : '<STR_LIT>' , <EOL> '<STR_LIT:POST>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , api , auth = None ) : <EOL> super ( RestBinding , self ) . __init__ ( api ) <EOL> self . auth = get_instance ( auth ) <EOL> def mangle_name ( self , name ) : <EOL> return name . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> def __call__ ( self , request , method_name , format ) : <EOL> """<STR_LIT>""" <EOL> if self . auth : <EOL> response = self . auth . login ( request ) <EOL> if response : <EOL> return response <EOL> try : <EOL> response_cls = get_response ( format ) <EOL> except UnknownFormat : <EOL> raise Http404 ( '<STR_LIT>' % format ) <EOL> try : <EOL> method = self . get_method ( self . READ_WRITE_MAPPING [ request . method ] , <EOL> method_name ) <EOL> except KeyError : <EOL> raise Http404 ( '<STR_LIT>' % method_name ) <EOL> try : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> response = method ( request , copy ( request . POST ) ) <EOL> else : <EOL> response = method ( request , copy ( request . GET ) ) <EOL> except ApiLoginRequired : <EOL> return self . auth . login_required ( request ) <EOL> except ApiError , e : <EOL> return e . get_response ( ) <EOL> response . kwargs [ '<STR_LIT>' ] = request <EOL> return response . transform ( response_cls ) </s>
