<s> """<STR_LIT>""" <EOL> import cgi <EOL> import platform <EOL> import requests <EOL> from retdec . exceptions import AuthenticationError <EOL> from retdec . exceptions import ConnectionError <EOL> from retdec . exceptions import UnknownAPIError <EOL> from retdec . file import File <EOL> class APIConnection : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , base_url , api_key ) : <EOL> """<STR_LIT>""" <EOL> self . _base_url = base_url <EOL> self . _api_key = api_key <EOL> def send_get_request ( self , path = '<STR_LIT>' , params = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . _send_request ( '<STR_LIT>' , path , params = params ) <EOL> return response . json ( ) <EOL> def send_post_request ( self , path = '<STR_LIT>' , params = None , files = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . _send_request ( '<STR_LIT>' , path , params = params , files = files ) <EOL> return response . json ( ) <EOL> def get_file ( self , path = '<STR_LIT>' , params = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . _send_request ( '<STR_LIT>' , path , params = params , stream = True ) <EOL> return File ( response . raw , self . _get_file_name ( response . headers ) ) <EOL> @ property <EOL> def _session ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . __dict__ : <EOL> self . __dict__ [ '<STR_LIT>' ] = self . _start_new_session ( ) <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def _start_new_session ( self ) : <EOL> """<STR_LIT>""" <EOL> session = requests . Session ( ) <EOL> session . auth = ( self . _api_key , '<STR_LIT>' ) <EOL> session . headers [ '<STR_LIT>' ] = '<STR_LIT>' + platform . system ( ) <EOL> return session <EOL> def _send_request ( self , method , path , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> url = self . _base_url + path <EOL> try : <EOL> response = getattr ( self . _session , method ) ( url , ** kwargs ) <EOL> except ( requests . exceptions . Timeout , <EOL> requests . exceptions . ConnectionError ) as ex : <EOL> raise ConnectionError ( str ( ex ) ) <EOL> self . _ensure_request_succeeded ( response ) <EOL> return response <EOL> def _ensure_request_succeeded ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if response . ok : <EOL> return <EOL> json = response . json ( ) <EOL> if response . status_code == <NUM_LIT> : <EOL> raise AuthenticationError <EOL> raise UnknownAPIError ( <EOL> int ( json [ '<STR_LIT:code>' ] ) , <EOL> json [ '<STR_LIT:message>' ] , <EOL> json [ '<STR_LIT:description>' ] <EOL> ) <EOL> def _get_file_name ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> _ , params = cgi . parse_header ( headers . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return params . get ( '<STR_LIT:filename>' , None ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> __name__ + '<STR_LIT:.>' + self . __class__ . __name__ , <EOL> self . _base_url <EOL> ) </s>
