<s> import json <EOL> from datetime import datetime <EOL> try : <EOL> from urlparse import urljoin <EOL> except ImportError : <EOL> from urllib . parse import urljoin <EOL> from requests . auth import AuthBase <EOL> from requests . sessions import Session <EOL> import xively <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class KeyAuth ( AuthBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key ) : <EOL> self . key = key <EOL> def __call__ ( self , r ) : <EOL> r . headers [ '<STR_LIT>' ] = self . key <EOL> return r <EOL> class Client ( Session ) : <EOL> r"""<STR_LIT>""" <EOL> BASE_URL = "<STR_LIT>" <EOL> def __init__ ( self , key , use_ssl = False , verify = True ) : <EOL> super ( Client , self ) . __init__ ( ) <EOL> self . auth = KeyAuth ( key ) <EOL> self . base_url = ( '<STR_LIT>' if use_ssl else '<STR_LIT>' ) + self . BASE_URL <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> self . headers [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> xively . __version__ , self . headers [ '<STR_LIT>' ] ) <EOL> self . _json_encoder = JSONEncoder ( ) <EOL> self . verify = verify <EOL> def request ( self , method , url , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> full_url = urljoin ( self . base_url , url ) <EOL> if '<STR_LIT:data>' in kwargs : <EOL> kwargs [ '<STR_LIT:data>' ] = self . _encode_data ( kwargs [ '<STR_LIT:data>' ] ) <EOL> return super ( Client , self ) . request ( method , full_url , * args , ** kwargs ) <EOL> def _encode_data ( self , data , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> encoder = JSONEncoder ( ** kwargs ) if kwargs else self . _json_encoder <EOL> return encoder . encode ( data ) <EOL> class JSONEncoder ( json . JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , datetime ) : <EOL> return obj . isoformat ( ) + '<STR_LIT>' <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . __getstate__ ( ) <EOL> else : <EOL> return json . JSONEncoder . default ( self , obj ) </s>
