def read(self, url, data=None, method='GET', fmt='json'): """ Returns parsed Python object or raises an error. *Parameters:* :url: Target url :data: Dictionary with parameters :method: (optional, default ``GET``) HTTP method, possible values: * ``GET`` * ``POST`` * ``PUT`` * ``DELETE`` :fmt: (optional, default ``json``) API response format. Currently only ``'json'`` is supported """ assert fmt == 'json', "Only JSON format is supported at the moment" if '/gds/' not in url: url = '{0}.{1}'.format(url, fmt) logger = logging.getLogger('python-upwork') logger.debug('Prepairing to make Upwork call') logger.debug('URL: {0}'.format(url)) try: logger.debug('Data: {0}'.format( json.dumps(data, default=decimal_default))) except TypeError: logger.debug('Data: {0}'.format(str(data))) logger.debug('Method: {0}'.format(method)) response = self.urlopen(url, data, method) if response.status != 200: logger.debug('Error: {0}'.format(response)) raise_http_error(url, response) result = response.data logger.debug('Response: {0}'.format(result)) if fmt == 'json': try: result = json.loads(result.decode('utf-8')) except ValueError: # Not a valid json string logger.debug('Response is not a valid json string') raise IncorrectJsonResponseError( json.dumps({ 'status': response.status, 'body': result }, default=decimal_default)) return result
def read(self, url, data=None, method='GET', fmt='json'): """ Returns parsed Python object or raises an error. *Parameters:* :url: Target url :data: Dictionary with parameters :method: (optional, default ``GET``) HTTP method, possible values: * ``GET`` * ``POST`` * ``PUT`` * ``DELETE`` :fmt: (optional, default ``json``) API response format. Currently only ``'json'`` is supported """ assert fmt == 'json', "Only JSON format is supported at the moment" if '/gds/' not in url: url = '{0}.{1}'.format(url, fmt) logger = logging.getLogger('python-upwork') logger.debug('Prepairing to make Upwork call') logger.debug('URL: {0}'.format(url)) try: logger.debug('Data: {0}'.format( json.dumps(data, default=decimal_default))) except TypeError: logger.debug('Data: {0}'.format(str(data))) logger.debug('Method: {0}'.format(method)) response = self.urlopen(url, data, method) if response.status != 200: logger.debug('Error: {0}'.format(response)) raise_http_error(url, response) result = response.data logger.debug('Response: {0}'.format(result)) if fmt == 'json': try: result = json.loads(result) except ValueError: # Not a valid json string logger.debug('Response is not a valid json string') raise IncorrectJsonResponseError( json.dumps({'status': response.status, 'body': result}, default=decimal_default) ) return result