def request(self, method, path, headers={}, data=None): auth_headers = sign_request(key=self.key, http_method=method, path=self.parsed_url.path + path.split('?', 1)[0], body=data, host=self.parsed_url.netloc, timestamp=datetime.datetime.utcnow(), user_id=self.client) request_headers = {} request_headers.update(self.headers) request_headers.update(dict( (k.lower(), v) for k, v in headers.items())) request_headers['x-chef-version'] = self.version request_headers.update(auth_headers) try: response = self._request( method, self.url + path, data, dict((k.capitalize(), v) for k, v in request_headers.items())) except requests.ConnectionError as e: raise ChefServerError(str(e)) except requests.Timeout as e: raise ChefServerError(e) if not response.ok: raise ChefServerError.from_error(response.reason, code=response.status_code) return response
def request(self, method, path, headers={}, data=None): auth_headers = sign_request(key=self.key, http_method=method, path=self.parsed_url.path + path.split('?', 1)[0], body=data, host=self.parsed_url.netloc, timestamp=datetime.datetime.utcnow(), user_id=self.client) request_headers = {} request_headers.update(self.headers) request_headers.update( dict((k.lower(), v) for k, v in headers.iteritems())) request_headers['x-chef-version'] = self.version request_headers.update(auth_headers) try: response = self._request( method, self.url + path, data, dict((k.capitalize(), v) for k, v in request_headers.iteritems())) except urllib2.HTTPError, e: e.content = e.read() try: e.content = json.loads(e.content) raise ChefServerError.from_error(e.content['error'], code=e.code) except ValueError: pass raise e
def request(self, method, path, headers={}, data=None): auth_headers = sign_request(key=self.key, http_method=method, path=self.parsed_url.path+path.split('?', 1)[0], body=data, host=self.parsed_url.netloc, timestamp=datetime.datetime.utcnow(), user_id=self.client) request_headers = {} request_headers.update(self.headers) request_headers.update(dict((k.lower(), v) for k, v in six.iteritems(headers))) request_headers['x-chef-version'] = self.version request_headers.update(auth_headers) try: response = self._request(method, self.url + path, data, dict( (k.capitalize(), v) for k, v in six.iteritems(request_headers))) response.raise_for_status() except six.moves.urllib.error.HTTPError as uhe: uhe.content = uhe.read() try: uhe.content = json.loads(uhe.content.decode()) raise ChefServerError.from_error(uhe.content['error'], code=uhe.code) except ValueError: pass raise uhe except requests.exceptions.HTTPError as rhe: try: content = response.json() raise ChefServerError.from_error(content['error'], code=rhe.response.status_code) except ValueError: pass raise rhe return response
def sign_request(self, path, **kwargs): d = dict( key=self.api.key, http_method="GET", path=self.api.parsed_url.path + path.split("?", 1)[0], body=None, host=self.api.parsed_url.netloc, timestamp=datetime.datetime.utcnow(), user_id=self.api.client, ) d.update(kwargs) auth_headers = sign_request(**d) headers = {} for key, value in auth_headers.iteritems(): headers["HTTP_" + key.upper().replace("-", "_")] = value return headers
def request(self, method, path, headers={}, data=None): auth_headers = sign_request(key=self.key, http_method=method, path=self.parsed_url.path+path.split('?', 1)[0], body=data, host=self.parsed_url.netloc, timestamp=datetime.datetime.utcnow(), user_id=self.client) headers = dict((k.lower(), v) for k, v in headers.iteritems()) headers['x-chef-version'] = self.version headers.update(auth_headers) try: response = self._request(method, self.url+path, data, dict((k.capitalize(), v) for k, v in headers.iteritems())) except urllib2.HTTPError, e: err = e.read() try: err = json.loads(err) raise ChefServerError.from_error(err['error'], code=e.code) except ValueError: pass raise
def request(self, method, path, headers={}, data=None): auth_headers = sign_request(key=self.key, http_method=method, path=self.parsed_url.path+path.split('?', 1)[0], body=data, host=self.parsed_url.netloc, timestamp=datetime.datetime.utcnow(), user_id=self.client) request_headers = {} request_headers.update(self.headers) request_headers.update(dict((k.lower(), v) for k, v in six.iteritems(headers))) request_headers['x-chef-version'] = self.version request_headers.update(auth_headers) try: response = self._request(method, self.url + path, data, dict( (k.capitalize(), v) for k, v in six.iteritems(request_headers))) except requests.ConnectionError as e: raise ChefServerError(e.message) if response.ok: return response raise ChefServerError.from_error(response.reason, code=response.status_code)