def request(self, *args, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = 'application/json' if 'body' in kwargs: kwargs['headers']['Content-Type'] = 'application/json' kwargs['body'] = json.dumps(kwargs['body']) LOG.debug("REQ HEADERS:" + str(kwargs['headers'])) LOG.debug("REQ BODY:" + str(kwargs['body'])) resp, body = super(HTTPClient, self).request(*args, **kwargs) self.http_log(args, kwargs, resp, body) if body: try: body = json.loads(body) except ValueError: pass else: body = None if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): raise exceptions.from_response(resp, body) return resp, body
def request(self, *args, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = self.accept kwargs['headers']['Accept-Charset'] = self.encoding if 'body' in kwargs: kwargs['headers']['Content-Type'] = self.content_type if '/json' in self.content_type: kwargs['body'] = json.dumps(kwargs['body']) # the server side needs to call json.loads else: kwargs['body'] = urlencode(kwargs['body']) # this is for most case, such as a form post LOG.debug('sending request: %s || %s' % (args, kwargs)) resp, body = super(HTTPClient, self).request(*args, **kwargs) content_type = resp.get('content-type') # may not be the same as ['headers']['Accept'] if body and content_type: body = self._extra_body(content_type, body) if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): raise exceptions.from_response(resp, body) return resp, body
def delete(self, instance): """ Delete the specified instance. :param instance_id: The instance id to delete """ resp, body = self.api.client.delete("/instances/%s" % base.getid(instance)) if resp.status in (422, 500): raise exceptions.from_response(resp, body)
def delete(self, backup_id): """ Delete the specified backup. :param backup_id: The backup id to delete """ resp, body = self.api.client.delete("/backups/%s" % backup_id) if resp.status in (422, 500): raise exceptions.from_response(resp, body)
def _list(self, url, response_key): resp, body = self.api.client.get(url) if resp is None or resp.status != 200: raise exceptions.from_response(resp, body) if not body: raise Exception("Call to " + url + " did not return a body.") return [self.resource_class(self, res) for res in body[response_key]]
def delete(self, security_group_rule): """ Delete the specified security group rule. :param security_group_rule: The security group rule to delete """ resp, body = self.api.client.delete("/security-group-rules/%s" % base.getid(security_group_rule)) if resp.status in (422, 500): raise exceptions.from_response(resp, body)
def delete(self, security_group_rule): """ Delete the specified security group. :param security_group_id: The security group id to delete """ resp, body = self.api.client.delete("/security_group_rules/%s" % base.getid(security_group_rule)) if resp.status in (422, 500): raise exceptions.from_response(resp, body)
def send_message(self, message, username='******', icon_url=None, icon_emoji=None): """ :param message: message which you want to send. :param username: username shown on chat.(default: PythonClient) :param icon_url: icon shown on chat. you specify url for image.(default: None) :param icon_emoji: icon shown on chat. specify :emoji: (default: None) Usage:: >>> mmc = MatterMostClient(MATTERMOSTURL) >>> mmc.send_message("This is test") """ header = {'Content-Type': 'application/json'} payload = { "text": message, "username": username, } if icon_url is not None: payload['icon_url'] = icon_url if icon_emoji is not None: payload['icon_emoji'] = icon_emoji resp = requests.post(self.__mattermosturl, headers=header, data=json.dumps(payload)) if resp.status_code >= 400: if resp.text: try: body = json.load(resp.text) except (ValueError, AttributeError): pass body = None else: body = None raise exceptions.from_response(resp, body) return resp
def request(self, url, method, **kwargs): """ Send an http request with the specified characteristics. Wrapper around httplib2.Http.request to handle tasks such as setting headers, JSON encoding/decoding, and error handling. """ # Copy the kwargs so we can reuse the original in case of redirects request_kwargs = copy.copy(kwargs) request_kwargs.setdefault('headers', kwargs.get('headers', {})) request_kwargs['headers']['User-Agent'] = self.USER_AGENT if 'body' in kwargs: request_kwargs['headers']['Content-Type'] = 'application/json' request_kwargs['body'] = json.dumps(kwargs['body']) self.http_log_req(( url, method, ), request_kwargs) resp, body = super(HTTPClient, self).request(url, method, **request_kwargs) self.http_log_resp(resp, body) if body: try: body = json.loads(body) except ValueError: _logger.debug("Could not decode JSON from body: %s" % body) else: _logger.debug("No body was returned.") body = None if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): _logger.exception("Request returned failure status.") raise exceptions.from_response(resp, body) elif resp.status in (301, 302, 305): # Redirected. Reissue the request to the new location. return self.request(resp['location'], method, **kwargs) return resp, body
def request(self, url, method, **kwargs): """ Send an http request with the specified characteristics. Wrapper around httplib2.Http.request to handle tasks such as setting headers, JSON encoding/decoding, and error handling. """ # Copy the kwargs so we can reuse the original in case of redirects request_kwargs = copy.copy(kwargs) request_kwargs.setdefault('headers', kwargs.get('headers', {})) request_kwargs['headers']['User-Agent'] = self.USER_AGENT if 'body' in kwargs: request_kwargs['headers']['Content-Type'] = 'application/json' request_kwargs['body'] = json.dumps(kwargs['body']) self.http_log_req((url, method,), request_kwargs) resp, body = super(HTTPClient, self).request(url, method, **request_kwargs) self.http_log_resp(resp, body) if body: try: body = json.loads(body) except ValueError: _logger.debug("Could not decode JSON from body: %s" % body) else: _logger.debug("No body was returned.") body = None if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): _logger.exception("Request returned failure status.") raise exceptions.from_response(resp, body) elif resp.status in (301, 302, 305): # Redirected. Reissue the request to the new location. return self.request(resp['location'], method, **kwargs) return resp, body
def check_for_exceptions(resp, body): if resp.status in (400, 422, 500): raise exceptions.from_response(resp, body)