<s> import json <EOL> try : <EOL> from urllib . parse import urlencode , urlparse <EOL> except ( ImportError ) : <EOL> from urllib import urlencode <EOL> from urlparse import urlparse <EOL> from . client_exception import ClientException <EOL> from . . download_manager import downloader <EOL> class JSONApiClient ( ) : <EOL> def __init__ ( self , settings ) : <EOL> self . settings = settings <EOL> def fetch ( self , url , prefer_cached = False ) : <EOL> """<STR_LIT>""" <EOL> extra_params = self . settings . get ( '<STR_LIT>' ) <EOL> domain_name = urlparse ( url ) . netloc <EOL> if extra_params and domain_name in extra_params : <EOL> params = urlencode ( extra_params [ domain_name ] ) <EOL> joiner = '<STR_LIT>' if url . find ( '<STR_LIT:?>' ) == - <NUM_LIT:1> else '<STR_LIT>' <EOL> url += joiner % params <EOL> with downloader ( url , self . settings ) as manager : <EOL> content = manager . fetch ( url , '<STR_LIT>' , <EOL> prefer_cached ) <EOL> return content <EOL> def fetch_json ( self , url , prefer_cached = False ) : <EOL> """<STR_LIT>""" <EOL> repository_json = self . fetch ( url , prefer_cached ) <EOL> try : <EOL> return json . loads ( repository_json . decode ( '<STR_LIT:utf-8>' ) ) <EOL> except ( ValueError ) : <EOL> error_string = u'<STR_LIT>' % url <EOL> raise ClientException ( error_string ) </s>
