<s> import Cookie <EOL> import formatters <EOL> import httplib2 <EOL> import json <EOL> import oauth2 as oauth <EOL> import os <EOL> import subprocess <EOL> import version <EOL> class Http ( object ) : <EOL> def __init__ ( self , args , logger , verb ) : <EOL> self . args = args <EOL> self . logger = logger <EOL> self . verb = verb <EOL> def run ( self , url , path , pipe = None , headers = None , cookies = None , body = "<STR_LIT>" ) : <EOL> self . url = url <EOL> host = self . url . netloc <EOL> httpclient = self . init_httpclient ( ) <EOL> httpclient . follow_redirects = False <EOL> httplib2 . debuglevel = self . args . debuglevel <EOL> if "<STR_LIT:@>" in host : <EOL> split = host . split ( "<STR_LIT:@>" ) <EOL> if len ( split ) > <NUM_LIT:1> : <EOL> host = split [ <NUM_LIT:1> ] <EOL> creds = split [ <NUM_LIT:0> ] . split ( "<STR_LIT::>" ) <EOL> httpclient . add_credentials ( creds [ <NUM_LIT:0> ] , creds [ <NUM_LIT:1> ] ) <EOL> else : <EOL> host = split [ <NUM_LIT:0> ] <EOL> uri = "<STR_LIT>" . format ( self . url . scheme , host , path ) <EOL> if not self . args . disable_cookies : <EOL> self . set_request_cookies ( cookies , headers ) <EOL> if not "<STR_LIT:host>" in headers : <EOL> headers [ "<STR_LIT:host>" ] = host <EOL> if not "<STR_LIT>" in headers : <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if not "<STR_LIT>" in headers : <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" + version . VERSION <EOL> self . logger . print_text ( "<STR_LIT>" + uri ) <EOL> response , content = httpclient . request ( <EOL> uri , method = self . verb , body = body , headers = headers ) <EOL> self . handle_response ( response , content , headers , cookies , pipe ) <EOL> def init_httpclient ( self ) : <EOL> http = None <EOL> keysfile = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" , self . url . netloc + "<STR_LIT>" ) <EOL> if os . path . isfile ( keysfile ) : <EOL> try : <EOL> with open ( keysfile , "<STR_LIT:r>" ) as file : <EOL> keys = json . load ( file ) <EOL> token = None <EOL> consumer = oauth . Consumer ( keys [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> keys [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT>" in keys : <EOL> token = oauth . Token ( keys [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> keys [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> http = oauth . Client ( consumer , token ) <EOL> self . logger . print_text ( "<STR_LIT>" + keysfile ) <EOL> except : <EOL> self . logger . print_error ( <EOL> "<STR_LIT>" + keysfile ) <EOL> else : <EOL> http = httplib2 . Http ( ) <EOL> return http <EOL> def set_request_cookies ( self , cookies , headers ) : <EOL> if self . url . netloc in cookies : <EOL> l = [ ] <EOL> cookie = cookies [ self . url . netloc ] <EOL> for morsel in cookie . values ( ) : <EOL> l . append ( morsel . key + "<STR_LIT:=>" + morsel . coded_value ) <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" . join ( l ) <EOL> def handle_response ( self , response , content , headers , cookies , pipe = None ) : <EOL> self . logger . print_response_code ( response ) <EOL> if self . args . show_headers : <EOL> self . logger . print_headers ( headers . items ( ) , True ) <EOL> self . logger . print_headers ( response . items ( ) ) <EOL> if not self . args . disable_cookies : <EOL> self . store_response_cookies ( response , cookies ) <EOL> if self . args . auto_format : <EOL> mimetype = response [ "<STR_LIT>" ] <EOL> if mimetype : <EOL> content = formatters . format_by_mimetype ( <EOL> content , mimetype . split ( "<STR_LIT:;>" ) [ <NUM_LIT:0> ] ) <EOL> if pipe : <EOL> content = self . pipe_data ( pipe , content ) <EOL> self . logger . print_data ( content ) <EOL> def store_response_cookies ( self , response , cookies ) : <EOL> if "<STR_LIT>" in response : <EOL> header = response [ "<STR_LIT>" ] <EOL> cookie = Cookie . SimpleCookie ( header ) <EOL> cookies [ self . url . netloc ] = cookie <EOL> def pipe_data ( self , command , data ) : <EOL> result = None <EOL> p = subprocess . Popen ( command , shell = True , bufsize = - <NUM_LIT:1> , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> stdin = subprocess . PIPE ) <EOL> output , error = p . communicate ( data ) <EOL> if error : <EOL> self . logger . print_text ( ) <EOL> self . logger . print_error ( error . decode ( "<STR_LIT:utf-8>" ) ) <EOL> else : <EOL> result = output . decode ( "<STR_LIT:utf-8>" ) <EOL> return result </s>
