<s> from . exceptions import * <EOL> import sys <EOL> BASE_URL = "<STR_LIT>" <EOL> TIMEOUT = <NUM_LIT:10> <EOL> py_version = sys . version_info [ <NUM_LIT:0> ] <EOL> if py_version >= <NUM_LIT:3> : <EOL> from urllib . request import urlopen <EOL> from urllib . error import HTTPError <EOL> from urllib . parse import urlencode <EOL> else : <EOL> from urllib2 import urlopen <EOL> from urllib2 import HTTPError <EOL> from urllib import urlencode <EOL> def call_api ( resource , data = None , base_url = BASE_URL ) : <EOL> try : <EOL> payload = None if data is None else urlencode ( data ) <EOL> if py_version >= <NUM_LIT:3> and payload is not None : <EOL> payload = payload . encode ( '<STR_LIT>' ) <EOL> response = urlopen ( base_url + resource , payload , timeout = TIMEOUT ) . read ( ) <EOL> return handle_response ( response ) <EOL> except HTTPError as e : <EOL> raise APIException ( handle_response ( e . read ( ) ) , e . code ) <EOL> def handle_response ( response ) : <EOL> if isinstance ( response , str ) : <EOL> return response <EOL> else : <EOL> return response . decode ( '<STR_LIT:utf-8>' ) </s>
