<s> """<STR_LIT>""" <EOL> import requests <EOL> try : <EOL> import httplib as client <EOL> from urlparse import urlparse <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from http import client <EOL> from urllib . parse import urlparse <EOL> from urllib . parse import urlencode <EOL> try : <EOL> unicode <EOL> except NameError : <EOL> basestring = unicode = str <EOL> from . exceptions import HTTPError <EOL> class HTTPSession ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , headers = None ) : <EOL> self . headers = headers or { } <EOL> def request ( self , method , url , data = None , headers = None ) : <EOL> if data and isinstance ( data , bytes ) : <EOL> data = data . decode ( ) <EOL> if data and not isinstance ( data , basestring ) : <EOL> data = urlencode ( data ) <EOL> if data is not None : <EOL> data = data . encode ( '<STR_LIT:utf8>' ) <EOL> if data and not headers . get ( '<STR_LIT:Content-Type>' , None ) : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> request_headers = self . headers . copy ( ) <EOL> if headers : <EOL> for k , v in headers . items ( ) : <EOL> if v is None : <EOL> del request_headers [ k ] <EOL> else : <EOL> request_headers [ k ] = v <EOL> try : <EOL> func = getattr ( requests , method . lower ( ) ) <EOL> except AttributeError : <EOL> raise Exception ( "<STR_LIT>" . format ( method ) ) <EOL> response = func ( url , data = data , headers = request_headers ) <EOL> if response . status_code > <NUM_LIT> : <EOL> raise HTTPError ( response . status_code , "<STR_LIT>" . format ( <EOL> response . status_code , response . content ) ) <EOL> return response <EOL> def get ( self , url , ** kwargs ) : <EOL> return self . request ( '<STR_LIT:GET>' , url , ** kwargs ) <EOL> def delete ( self , url , ** kwargs ) : <EOL> return self . request ( '<STR_LIT>' , url , ** kwargs ) <EOL> def post ( self , url , data = None , headers = { } ) : <EOL> return self . request ( '<STR_LIT:POST>' , url , data = data , headers = headers ) <EOL> def put ( self , url , data = None , ** kwargs ) : <EOL> return self . request ( '<STR_LIT>' , url , data = data , ** kwargs ) <EOL> def add_header ( self , name , value ) : <EOL> self . headers [ name ] = value </s>
