<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division <EOL> from __future__ import unicode_literals , print_function <EOL> try : <EOL> from urllib2 import Request as Py2Request , HTTPError , URLError <EOL> from urllib2 import build_opener , HTTPCookieProcessor <EOL> from urllib import urlencode <EOL> import httplib <EOL> import cookielib <EOL> pyversion = <NUM_LIT:2> <EOL> except ImportError : <EOL> from urllib . request import Request as PythonRequest , build_opener <EOL> from urllib . request import HTTPCookieProcessor <EOL> from urllib . error import HTTPError , URLError <EOL> from urllib . parse import urlencode <EOL> import http . client as httplib <EOL> import http . cookiejar as cookielib <EOL> pyversion = <NUM_LIT:3> <EOL> import socket <EOL> import functools <EOL> from beekeeper . variable_handlers import render <EOL> from beekeeper . data_handlers import decode <EOL> from beekeeper . exceptions import TraversalError , TooMuchBodyData , RequestTimeout <EOL> if pyversion == <NUM_LIT:2> : <EOL> class PythonRequest ( Py2Request ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _method = kwargs . pop ( '<STR_LIT>' , None ) <EOL> Py2Request . __init__ ( self , * args , ** kwargs ) <EOL> def get_method ( self ) : <EOL> return self . _method if self . _method else super ( PythonRequest , self ) . get_method ( ) <EOL> elif pyversion == <NUM_LIT:3> : <EOL> basestring = str <EOL> COOKIE_JAR = cookielib . CookieJar ( ) <EOL> REQUEST_OPENER = build_opener ( HTTPCookieProcessor ( COOKIE_JAR ) ) <EOL> def download_as_json ( url ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return Response ( '<STR_LIT:application/json>' , request ( url = url ) ) . read ( ) <EOL> except HTTPError as err : <EOL> raise ResponseException ( '<STR_LIT:application/json>' , err ) <EOL> def request ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> timeout = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:5> ) <EOL> req = PythonRequest ( * args , ** kwargs ) <EOL> return REQUEST_OPENER . open ( req , timeout = timeout ) <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , action , variables ) : <EOL> self . action = action <EOL> self . url = self . action . endpoint . url ( ) <EOL> self . replacements = { } <EOL> self . params = { } <EOL> self . output = { <EOL> '<STR_LIT:data>' : None , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : self . action . method <EOL> } <EOL> for var_type in variables . types ( ) : <EOL> render ( self , var_type , ** variables . vals ( var_type ) ) <EOL> def send ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return_full_object = kwargs . get ( '<STR_LIT>' , False ) <EOL> _verbose = kwargs . get ( '<STR_LIT>' , False ) <EOL> traversal = kwargs . get ( '<STR_LIT>' , None ) <EOL> timeout = kwargs . get ( '<STR_LIT>' , <NUM_LIT:5> ) <EOL> self . output [ '<STR_LIT:url>' ] = self . render_url ( ) <EOL> with VerboseContextManager ( verbose = _verbose ) : <EOL> try : <EOL> resp = Response ( self . action . format ( ) , request ( timeout = timeout , ** self . output ) , traversal ) <EOL> except HTTPError as err : <EOL> raise ResponseException ( self . action . format ( ) , err ) <EOL> except socket . timeout : <EOL> raise RequestTimeout ( functools . partial ( self . send , ** kwargs ) ) <EOL> except URLError as err : <EOL> if isinstance ( err . reason , socket . timeout ) : <EOL> raise RequestTimeout ( functools . partial ( self . send , ** kwargs ) ) <EOL> else : <EOL> raise <EOL> if return_full_object : <EOL> return resp <EOL> else : <EOL> return resp . read ( ) <EOL> def set_headers ( self , ** headers ) : <EOL> self . output [ '<STR_LIT>' ] . update ( headers ) <EOL> def set_data ( self , data , override = False ) : <EOL> if self . output [ '<STR_LIT:data>' ] is None or override : <EOL> self . output [ '<STR_LIT:data>' ] = data <EOL> else : <EOL> raise TooMuchBodyData ( self . output [ '<STR_LIT:data>' ] , data ) <EOL> def set_url_params ( self , ** params ) : <EOL> self . params . update ( params ) <EOL> def set_url_replacements ( self , ** replacements ) : <EOL> self . replacements . update ( replacements ) <EOL> def render_url ( self ) : <EOL> """<STR_LIT>""" <EOL> url = self . url . format ( ** self . replacements ) <EOL> if self . params : <EOL> return url + '<STR_LIT:?>' + urlencode ( self . params ) <EOL> return url <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , static_format , response , traversal = None ) : <EOL> self . static_format = static_format <EOL> self . headers = response . headers <EOL> self . data = response . read ( ) <EOL> self . code = response . getcode ( ) <EOL> self . message = response . msg <EOL> self . traversal = traversal <EOL> def mimetype ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:;>' in self . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) : <EOL> return self . headers [ '<STR_LIT:Content-Type>' ] . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> return self . headers . get ( '<STR_LIT:Content-Type>' , self . static_format ) <EOL> def encoding ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) : <EOL> return self . headers [ '<STR_LIT:Content-Type>' ] . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> return '<STR_LIT:utf-8>' <EOL> def read ( self , raw = False , perform_traversal = True ) : <EOL> """<STR_LIT>""" <EOL> if not raw : <EOL> response_body = decode ( self . data , self . mimetype ( ) , encoding = self . encoding ( ) ) <EOL> if perform_traversal and self . traversal is not None : <EOL> return traverse ( response_body , * self . traversal ) <EOL> return response_body <EOL> else : <EOL> return self . data <EOL> def traverse ( obj , * path , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if path : <EOL> if isinstance ( obj , list ) or isinstance ( obj , tuple ) : <EOL> return [ traverse ( x , * path ) for x in obj ] <EOL> elif isinstance ( obj , dict ) : <EOL> if isinstance ( path [ <NUM_LIT:0> ] , list ) or isinstance ( path [ <NUM_LIT:0> ] , tuple ) : <EOL> for branch in path [ <NUM_LIT:0> ] : <EOL> if not isinstance ( branch , basestring ) : <EOL> raise TraversalError ( obj , path [ <NUM_LIT:0> ] ) <EOL> return { name : traverse ( obj [ name ] , * path [ <NUM_LIT:1> : ] , split = True ) for name in path [ <NUM_LIT:0> ] } <EOL> elif not isinstance ( path [ <NUM_LIT:0> ] , basestring ) : <EOL> raise TraversalError ( obj , path [ <NUM_LIT:0> ] ) <EOL> elif path [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return { name : traverse ( item , * path [ <NUM_LIT:1> : ] , split = True ) for name , item in obj . items ( ) } <EOL> elif path [ <NUM_LIT:0> ] in obj : <EOL> return traverse ( obj [ path [ <NUM_LIT:0> ] ] , * path [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> raise TraversalError ( obj , path [ <NUM_LIT:0> ] ) <EOL> else : <EOL> if kwargs . get ( '<STR_LIT>' , False ) : <EOL> return obj <EOL> else : <EOL> raise TraversalError ( obj , path [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return obj <EOL> class ResponseException ( Response , Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , static_format , response ) : <EOL> Response . __init__ ( self , static_format , response , traversal = None ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . code , self . message ) <EOL> class VerboseContextManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose = False ) : <EOL> self . verbose = verbose <EOL> self . previous_state = httplib . HTTPConnection . debuglevel <EOL> def __enter__ ( self ) : <EOL> if self . verbose : <EOL> httplib . HTTPConnection . debuglevel = <NUM_LIT:1> <EOL> def __exit__ ( self , * args , ** kwargs ) : <EOL> httplib . HTTPConnection . debuglevel = self . previous_state </s>
