def _request(self, method, data=None, params=None): s = self.get_serializer() url = self._store["base_url"] if self._store["append_slash"] and not url.endswith("/"): url = url + "/" resp = self._store["session"].request(method, url, data=data, params=params, headers={ "content-type": s.get_content_type(), "accept": s.get_content_type() }) if 400 <= resp.status_code <= 499: raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) elif 500 <= resp.status_code <= 599: raise exceptions.HttpServerError("Server Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) return resp
def _request(self, method, data=None, files=None, params=None): s = self._store["serializer"] url = self._store["base_url"] if self._store["append_slash"] and not url.endswith("/"): url = url + "/" headers = {"accept": s.get_content_type()} if not files: headers["content-type"] = s.get_content_type() if data is not None: data = s.dumps(data) resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers) if 400 <= resp.status_code <= 499: raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) elif 500 <= resp.status_code <= 599: raise exceptions.HttpServerError("Server Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) self._ = resp return resp
def _request(self, method, data=None, params=None, headers=None): """ Overwrite so we can pass through custom headers, like oauth or something useful. """ s = self._store["serializer"] url = self._store["base_url"] if self._store["append_slash"] and not url.endswith("/"): url = url + "/" hdrs = {"accept": s.get_content_type(), "content-type": s.get_content_type()} hdrs.update(headers or {}) for callback in self._store.get('callbacks', []): callback['method'](self, data=data, extra=callback.get('extra'), headers=hdrs, method=method, params=merge(params, callback.get('params')), url=url) stats_key = _key(url, method) with statsd.timer(stats_key): try: resp = self._call_request(method, url, data, params, hdrs) except ConnectionError, err: # In the case of connection errors, there isn't a response # so let's explicitly set up to None. raise exceptions.HttpServerError('Connection Error', response=None, content=None)
def _request(self, method, data=None, params=None, headers=None): """ Overwrite so we can pass through custom headers, like oauth or something useful. """ s = self._store["serializer"] url = self._store["base_url"] if self._store["append_slash"] and not url.endswith("/"): url = url + "/" hdrs = { "accept": s.get_content_type(), "content-type": s.get_content_type() } hdrs.update(headers or {}) for callback in self._store.get('callbacks', []): callback['method'](self, data=data, extra=callback.get('extra'), headers=hdrs, method=method, params=params, url=url) stats_key = _key(url, method) with statsd.timer(stats_key): try: resp = self._call_request(method, url, data, params, hdrs) except ConnectionError: raise exceptions.HttpServerError('Connection Error') statsd.incr('%s.%s' % (stats_key, resp.status_code)) if 400 <= resp.status_code <= 499: raise exceptions.HttpClientError( "Client Error %s: %s" % (resp.status_code, url), response=resp, content=self._try_to_serialize_error(resp)) elif 500 <= resp.status_code <= 599: raise exceptions.HttpServerError( "Server Error %s: %s" % (resp.status_code, url), response=resp, content=self._try_to_serialize_error(resp)) self._ = resp return resp
def _try_to_serialize_response(self, resp): headers = resp.headers # 204 specifically does not return any data so we shouldn't try and # parse it. if resp.status_code == 204: if resp.content: raise exceptions.HttpServerError( 'Server Error, not empty: %s' % resp.status_code, response=resp) return resp = super(TastypieResource, self)._try_to_serialize_response(resp) if isinstance(resp, dict) and u'meta' in resp: resp[u'meta'][u'headers'] = headers if self.format_lists and self._is_list(resp): return self._format_list(resp) return resp
def _request(self, method, data=None, params=None, files=None): s = self.get_serializer() url = self._store["base_url"] if self._store["append_slash"] and not url.endswith("/"): url = url + "/" headers = {'accept': s.get_content_type()} print 'data:' print data print 'params:' print params print 'files:' print files if not files: # files imply a content-type of multipart/form-data headers['content-type'] = s.get_content_type() resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers) if 400 <= resp.status_code <= 499: raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) elif 500 <= resp.status_code <= 599: print resp print resp.content raise exceptions.HttpServerError("Server Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) return resp
def _request(self, method, data=None, files=None, params=None): serializer = self._store["serializer"] url = self.url() headers = {"accept": serializer.get_content_type()} if not files: headers["content-type"] = serializer.get_content_type() if data is not None: data = serializer.dumps(data) req = Request(method=method, url=url, data=data, params=params, files=files, headers=headers) s = self._store["session"] prepped = s.prepare_request(req) g.log['out'].append(dict(request=None, response=None)) g.log['out'][-1]['request'] = log_outgoing_request(prepped) resp = s.send(prepped) g.log['out'][-1]['response'] = log_outgoing_response(resp) if 400 <= resp.status_code <= 499: exception_class = exceptions.HttpNotFoundError if resp.status_code == 404 \ else exceptions.HttpClientError raise exception_class("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) elif 500 <= resp.status_code <= 599: raise exceptions.HttpServerError("Server Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) self._ = resp return resp
def _request(self, method, **kwargs): s = self.get_serializer() url = self._meta.base_url if self._meta.append_slash and not url.endswith("/"): url = url + "/" if "body" in kwargs: body = kwargs.pop("body") else: body = None if kwargs: url = "?".join([url, urllib.urlencode(kwargs)]) resp, content = self._http.request(url, method, body=body, headers={"content-type": s.get_content_type()}) if 400 <= resp.status <= 499: raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status, url), response=resp, content=content) elif 500 <= resp.status <= 599: raise exceptions.HttpServerError("Server Error %s: %s" % (resp.status, url), response=resp, content=content) return resp, content
resp = self._call_request(method, url, data, params, hdrs) except ConnectionError, err: # In the case of connection errors, there isn't a response # so let's explicitly set up to None. raise exceptions.HttpServerError('Connection Error', response=None, content=None) statsd.incr('%s.%s' % (stats_key, resp.status_code)) if 400 <= resp.status_code <= 499: raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=self._try_to_serialize_error(resp)) elif 500 <= resp.status_code <= 599: raise exceptions.HttpServerError("Server Error %s: %s" % (resp.status_code, url), response=resp, content=self._try_to_serialize_error(resp)) self._ = resp return resp def _try_to_serialize_error(self, response): try: return self._try_to_serialize_response(response) except ValueError: return response class CurlingBase(object):