<s> import sublime <EOL> import json <EOL> import socket <EOL> from . . urllib3 import * <EOL> from . gdrive_util import * <EOL> class GDriveConnection ( ) : <EOL> def request ( self , method , url , params = None , body = None , headers = None , raw_response = False , as_json = False ) : <EOL> try : <EOL> import ssl <EOL> pool_manager = PoolManager ( <EOL> num_pools = <NUM_LIT:4> , <EOL> maxsize = <NUM_LIT:8> , <EOL> block = False , <EOL> timeout = <NUM_LIT> , <EOL> cert_reqs = ssl . CERT_REQUIRED , <EOL> ca_certs = GDriveUtil . get_cert_file ( ) , <EOL> ssl_version = ssl . PROTOCOL_TLSv1 , <EOL> ) <EOL> except ( ImportError ) : <EOL> pool_manager = PoolManager ( <EOL> num_pools = <NUM_LIT:4> , <EOL> maxsize = <NUM_LIT:8> , <EOL> block = False , <EOL> timeout = <NUM_LIT> , <EOL> ) <EOL> params = params or { } <EOL> headers = headers or { } <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if params : <EOL> if body : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if as_json : <EOL> body = sublime . encode_value ( params ) <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> headers [ "<STR_LIT>" ] = len ( body ) <EOL> else : <EOL> body = urllib . parse . urlencode ( params ) <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if hasattr ( body , "<STR_LIT>" ) : <EOL> body = str ( body . getvalue ( ) ) <EOL> headers [ "<STR_LIT>" ] = len ( body ) <EOL> if hasattr ( body , "<STR_LIT>" ) : <EOL> headers [ "<STR_LIT>" ] = len ( body . read ( ) ) <EOL> for key , value in headers . items ( ) : <EOL> if type ( value ) == str and "<STR_LIT:\n>" in value : <EOL> raise ValueError ( "<STR_LIT>" + key + "<STR_LIT>" + value + "<STR_LIT:)>" ) <EOL> try : <EOL> response = pool_manager . urlopen ( method = method , url = url , body = body , headers = headers , preload_content = False ) <EOL> except socket . error as e : <EOL> raise SocketError ( url , e ) <EOL> except exceptions . SSLError as e : <EOL> raise SocketError ( url , "<STR_LIT>" % e ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise ErrorResponse ( response , response . read ( ) ) <EOL> return { "<STR_LIT>" : response . headers , "<STR_LIT:data>" : self . process_response ( response , raw_response ) } <EOL> def process_response ( self , r , raw_response ) : <EOL> if raw_response : <EOL> return r <EOL> else : <EOL> resp = json . loads ( r . read ( ) . decode ( "<STR_LIT:utf-8>" ) ) <EOL> r . close ( ) <EOL> return resp <EOL> def get ( self , url , headers = None , raw_response = False ) : <EOL> return self . request ( "<STR_LIT:GET>" , url , headers = headers , raw_response = raw_response ) <EOL> def delete ( self , url , headers = None , raw_response = False ) : <EOL> return self . request ( "<STR_LIT>" , url , headers = headers , raw_response = raw_response ) <EOL> def post ( self , url , params = None , headers = None , raw_response = False , as_json = False ) : <EOL> return self . request ( "<STR_LIT:POST>" , url , params = params , headers = headers , raw_response = raw_response , as_json = as_json ) <EOL> def post_multipart ( self , url , fields = { } , headers = None , raw_response = False ) : <EOL> boundary = "<STR_LIT>" <EOL> body = b"<STR_LIT>" <EOL> headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" + boundary + "<STR_LIT>" <EOL> for part in fields : <EOL> body += ( "<STR_LIT>" + boundary ) . encode ( "<STR_LIT:utf-8>" ) <EOL> if "<STR_LIT>" in part : <EOL> for hk in part [ "<STR_LIT>" ] : <EOL> body += ( "<STR_LIT>" % ( hk , part [ "<STR_LIT>" ] [ hk ] ) ) . encode ( "<STR_LIT:utf-8>" ) <EOL> body += b"<STR_LIT>" + part [ "<STR_LIT:body>" ] <EOL> body += ( "<STR_LIT>" + boundary + "<STR_LIT>" ) . encode ( "<STR_LIT:utf-8>" ) <EOL> headers [ "<STR_LIT>" ] = len ( body ) <EOL> return self . request ( "<STR_LIT:POST>" , url , body = body , headers = headers , raw_response = raw_response ) <EOL> def put ( self , url , body , headers = None , raw_response = False ) : <EOL> return self . request ( "<STR_LIT>" , url , body = body , headers = headers , raw_response = raw_response ) <EOL> class SocketError ( socket . error ) : <EOL> def __init__ ( self , host , e ) : <EOL> msg = "<STR_LIT>" % ( host , str ( e ) ) <EOL> socket . error . __init__ ( self , msg ) <EOL> class ErrorResponse ( Exception ) : <EOL> def __init__ ( self , http_resp , body ) : <EOL> self . status = http_resp . status <EOL> self . reason = http_resp . reason <EOL> self . body = body <EOL> self . headers = http_resp . getheaders ( ) <EOL> http_resp . close ( ) <EOL> try : <EOL> self . body = json . loads ( self . body . decode ( "<STR_LIT:utf-8>" ) ) <EOL> self . error_msg = self . body . get ( '<STR_LIT:error>' ) <EOL> self . user_error_msg = self . body . get ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> self . error_msg = None <EOL> self . user_error_msg = None <EOL> def __str__ ( self ) : <EOL> if self . user_error_msg and self . user_error_msg != self . error_msg : <EOL> msg = "<STR_LIT>" % ( self . user_error_msg , self . error_msg ) <EOL> elif self . error_msg : <EOL> msg = repr ( self . error_msg ) <EOL> elif not self . body : <EOL> msg = repr ( self . reason ) <EOL> else : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" % ( self . body , self . headers ) <EOL> return "<STR_LIT>" % ( self . status , msg ) </s>
