<s> """<STR_LIT>""" <EOL> import json <EOL> try : <EOL> import urllib . request as urllib <EOL> from urllib . parse import urlencode <EOL> except ImportError : <EOL> import urllib2 as urllib <EOL> from urllib import urlencode <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response ) : <EOL> """<STR_LIT>""" <EOL> self . _status_code = response . getcode ( ) <EOL> self . _response_body = response . read ( ) <EOL> self . _response_headers = response . info ( ) <EOL> @ property <EOL> def status_code ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _status_code <EOL> @ property <EOL> def response_body ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _response_body <EOL> @ property <EOL> def response_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _response_headers <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> host , <EOL> request_headers = None , <EOL> version = None , <EOL> url_path = None ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . request_headers = request_headers or { } <EOL> self . _version = version <EOL> self . _url_path = url_path or [ ] <EOL> self . methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _build_versioned_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( self . host , str ( self . _version ) , url ) <EOL> def _build_url ( self , query_params ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' <EOL> count = <NUM_LIT:0> <EOL> while count < len ( self . _url_path ) : <EOL> url += '<STR_LIT>' . format ( self . _url_path [ count ] ) <EOL> count += <NUM_LIT:1> <EOL> if query_params : <EOL> url_values = urlencode ( sorted ( query_params . items ( ) ) ) <EOL> url = '<STR_LIT>' . format ( url , url_values ) <EOL> url = self . _build_versioned_url ( url ) if self . _version else self . host + url <EOL> return url <EOL> def _update_headers ( self , request_headers ) : <EOL> """<STR_LIT>""" <EOL> self . request_headers . update ( request_headers ) <EOL> def _build_client ( self , name = None ) : <EOL> """<STR_LIT>""" <EOL> url_path = self . _url_path + [ name ] if name else self . _url_path <EOL> return Client ( host = self . host , <EOL> version = self . _version , <EOL> request_headers = self . request_headers , <EOL> url_path = url_path ) <EOL> def _make_request ( self , opener , request ) : <EOL> """<STR_LIT>""" <EOL> return opener . open ( request ) <EOL> def _ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _build_client ( name ) <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT:version>' : <EOL> def get_version ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _version = args [ <NUM_LIT:0> ] <EOL> return self . _build_client ( ) <EOL> return get_version <EOL> if name in self . methods : <EOL> method = name . upper ( ) <EOL> def http_request ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> self . _update_headers ( kwargs [ '<STR_LIT>' ] ) <EOL> data = json . dumps ( kwargs [ '<STR_LIT>' ] ) . encode ( '<STR_LIT:utf-8>' ) if '<STR_LIT>' in kwargs else None <EOL> params = kwargs [ '<STR_LIT>' ] if '<STR_LIT>' in kwargs else None <EOL> opener = urllib . build_opener ( ) <EOL> request = urllib . Request ( self . _build_url ( params ) , data = data ) <EOL> if self . request_headers : <EOL> for key , value in self . request_headers . items ( ) : <EOL> request . add_header ( key , value ) <EOL> request . get_method = lambda : method <EOL> return Response ( self . _make_request ( opener , request ) ) <EOL> return http_request <EOL> else : <EOL> return self . _ ( name ) </s>
