def _get_json_data(self): """ Return JSON data. :raises: :exc:`!RequestException` on ``requests`` error. :raises: :exc:`.RequestServerError` on 5xx response. :raises: :exc:`.RequestClientError` on errors caused client-side. """ response = requests.get( urlparse.urljoin(config.get('job_runner_worker', 'api_base_url'), self._resource_path), auth=HmacAuth(config.get('job_runner_worker', 'api_key'), config.get('job_runner_worker', 'secret')), headers={'content-type': 'application/json'}, verify=False, ) if response.status_code != 200: if response.status_code >= 500 and response.status_code <= 599: raise RequestServerError('Server returned {0} - {1}'.format( response.status_code, response.content)) else: raise RequestClientError('Server returned {0} - {1}'.format( response.status_code, response.content)) return response.json
def post(self, attributes={}): """ PATCH resource with given keyword arguments. :raises: :exc:`!RequestException` on ``requests`` error. :raises: :exc:`.RequestServerError` on 5xx response. :raises: :exc:`.RequestClientError` on errors caused client-side. """ response = requests.post( urlparse.urljoin(config.get('job_runner_worker', 'api_base_url'), self._resource_path), auth=HmacAuth(config.get('job_runner_worker', 'api_key'), config.get('job_runner_worker', 'secret')), headers={'content-type': 'application/json'}, data=json.dumps(attributes), verify=False, ) if response.status_code != 201: if response.status_code >= 500 and response.status_code <= 599: raise RequestServerError('Server returned {0} - {1}'.format( response.status_code, response.content)) else: raise RequestClientError('Server returned {0} - {1}'.format( response.status_code, response.content))
def test_hmac_calculation(self): """ Test HMAC calculation. """ auth = HmacAuth('public', 'key') r = Mock() r.method = 'patch' r.path_url = '/path/?foo=bar' r.data = 'data body' r.headers = {} self.assertEqual( 'ApiKey public:2b989ffc81712758d070fb46055b55f18a245d15', auth(r).headers['Authorization'])
def get_list(cls, resource_path, params={}): """ Return a list of models for ``resource_path``. :param resource_path: The path of the resource. :param params: A ``dict`` containing optional request params. Optional. :return: A ``list`` of class instances. :raises: :exc:`.RestError` when response code is not 200. """ response = requests.get( urlparse.urljoin(config.get('job_runner_worker', 'api_base_url'), resource_path), auth=HmacAuth(config.get('job_runner_worker', 'api_key'), config.get('job_runner_worker', 'secret')), params=params, headers={'content-type': 'application/json'}, verify=False, ) if response.status_code != 200: if response.status_code >= 500 and response.status_code <= 599: raise RequestServerError('Server returned {0} - {1}'.format( response.status_code, response.content)) else: raise RequestClientError('Server returned {0} - {1}'.format( response.status_code, response.content)) output = [] for obj_dict in response.json['objects']: output.append(cls(obj_dict['resource_uri'], obj_dict)) if 'next' in response.json['meta'] and response.json['meta']['next']: output.extend(cls.get_list(response.json['meta']['next'])) return output