<s> import sys <EOL> import os . path <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , "<STR_LIT>" ) ) <EOL> import re <EOL> import requests <EOL> from requests . status_codes import codes <EOL> try : <EOL> import http . client as httplib <EOL> except ImportError : <EOL> import httplib <EOL> import commandline <EOL> import sublime <EOL> from io import BytesIO <EOL> import logging <EOL> logging . basicConfig ( format = '<STR_LIT>' ) <EOL> logger = logging . getLogger ( ) <EOL> class CurlSession ( object ) : <EOL> ERR_UNKNOWN_CODE = "<STR_LIT>" <EOL> CURL_ERRORS = { <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:5> : "<STR_LIT>" , <EOL> <NUM_LIT:6> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> } <EOL> class FakeSocket ( BytesIO ) : <EOL> def makefile ( self , * args , ** kw ) : <EOL> return self <EOL> def __init__ ( self , verify = None ) : <EOL> self . verify = verify <EOL> def _parse_http ( self , text ) : <EOL> text = str ( text , encoding = '<STR_LIT:utf-8>' ) <EOL> if re . match ( r'<STR_LIT>' , text ) : <EOL> m = re . search ( r'<STR_LIT>' , text , re . S ) <EOL> if not m : <EOL> raise Exception ( "<STR_LIT>" % text ) <EOL> else : <EOL> text = m . group ( <NUM_LIT:1> ) <EOL> if re . match ( r'<STR_LIT>' , text ) : <EOL> m = re . search ( r'<STR_LIT>' , text , re . S ) <EOL> if not m : <EOL> raise Exception ( "<STR_LIT>" % text ) <EOL> else : <EOL> text = m . group ( <NUM_LIT:1> ) <EOL> if "<STR_LIT>" in text : <EOL> text = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , text , count = <NUM_LIT:1> ) <EOL> logger . debug ( "<STR_LIT>" % text ) <EOL> socket = self . FakeSocket ( text . encode ( ) ) <EOL> response = httplib . HTTPResponse ( socket ) <EOL> response . begin ( ) <EOL> return response <EOL> def _build_response ( self , text ) : <EOL> logger . debug ( "<STR_LIT>" % text ) <EOL> raw_response = self . _parse_http ( text ) <EOL> response = requests . models . Response ( ) <EOL> response . encoding = '<STR_LIT:utf-8>' <EOL> response . status_code = raw_response . status <EOL> response . headers = dict ( raw_response . getheaders ( ) ) <EOL> response . _content = raw_response . read ( ) <EOL> return response <EOL> def request ( self , method , url , headers = None , params = None , data = None , auth = None , allow_redirects = False , config = None , proxies = None ) : <EOL> try : <EOL> curl = commandline . find_binary ( '<STR_LIT>' ) <EOL> except commandline . BinaryNotFoundError : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> curl_options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if auth : <EOL> curl_options . extend ( [ '<STR_LIT>' , "<STR_LIT>" % auth ] ) <EOL> if self . verify : <EOL> curl_options . extend ( [ '<STR_LIT>' , self . verify ] ) <EOL> if headers : <EOL> for k , v in headers . items ( ) : <EOL> curl_options . extend ( [ '<STR_LIT>' , "<STR_LIT>" % ( k , v ) ] ) <EOL> if method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> curl_options . extend ( [ '<STR_LIT>' , data ] ) <EOL> if method == '<STR_LIT>' : <EOL> curl_options . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if params : <EOL> url += '<STR_LIT:?>' + '<STR_LIT:&>' . join ( [ '<STR_LIT:=>' . join ( [ k , str ( v ) ] ) for k , v in params . items ( ) ] ) <EOL> if proxies and proxies . get ( '<STR_LIT>' , None ) : <EOL> curl_options . extend ( [ '<STR_LIT>' , proxies [ '<STR_LIT>' ] ] ) <EOL> command = [ curl ] + curl_options + [ url ] <EOL> logger . debug ( "<STR_LIT>" % command ) <EOL> try : <EOL> command_response = commandline . execute ( command ) <EOL> except commandline . CommandExecutionError as e : <EOL> logger . error ( "<STR_LIT>" % repr ( e ) ) <EOL> self . _handle_curl_error ( e . errorcode ) <EOL> return <EOL> response = self . _build_response ( command_response ) <EOL> response . url = url <EOL> return response <EOL> def post ( self , * args , ** kwargs ) : <EOL> return self . request ( "<STR_LIT>" , * args , ** kwargs ) <EOL> def _handle_curl_error ( self , error ) : <EOL> sublime . error_message ( <EOL> self . CURL_ERRORS . get ( error , "<STR_LIT>" % ( self . ERR_UNKNOWN_CODE , error ) ) ) <EOL> def session ( verify = None , force_curl = False ) : <EOL> if not force_curl and hasattr ( httplib , "<STR_LIT>" ) : <EOL> session = requests . Session ( ) <EOL> session . verify = verify <EOL> return session <EOL> else : <EOL> return CurlSession ( verify = verify ) </s>
