def submit(self, redirect_limit=0, product=None, **response_kwargs): """ Submit this request and return a :py:class:`Response <httpstream.Response>` object. """ uri = URI(self.uri) headers = dict(self.headers) headers.setdefault("User-Agent", user_agent(product)) while True: http, rs = submit(self.method, uri, self.body, headers) status_class = rs.status // 100 if status_class == 3: redirection = Redirection(http, uri, self, rs, **response_kwargs) if redirect_limit: redirect_limit -= 1 location = URI.resolve(uri, rs.getheader("Location")) if location == uri: raise RedirectionError("Circular redirection") if rs.status in (MOVED_PERMANENTLY, PERMANENT_REDIRECT): redirects[uri] = location uri = location redirection.close() else: return redirection elif status_class == 4: raise ClientError(http, uri, self, rs, **response_kwargs) elif status_class == 5: raise ServerError(http, uri, self, rs, **response_kwargs) else: return Response(http, uri, self, rs, **response_kwargs)
class Resource(object): """ A web resource identified by a URI. """ def __init__(self, uri): self._uri = URI(uri) def __str__(self): return "<{0}>".format(str(self._uri)) def __repr__(self): return "{0}({1})".format(self.__class__.__name__, repr(self._uri.string)) def __eq__(self, other): """ Determine equality of two objects based on URI. """ return self._uri == other._uri def __ne__(self, other): """ Determine inequality of two objects based on URI. """ return self._uri != other._uri def __bool__(self): return bool(self._uri) def __nonzero__(self): return bool(self._uri) @property def __uri__(self): return self._uri @property def uri(self): """ The URI of this resource. """ return self._uri def resolve(self, reference, strict=True): """ Resolve a URI reference against the URI for this resource, returning a new resource represented by the new target URI. """ return Resource(self._uri.resolve(reference, strict)) def get(self, headers=None, redirect_limit=5, **kwargs): """ Issue a ``GET`` request to this resource. :param headers: headers to be included in the request (optional) :type headers: dict :param redirect_limit: maximum number of redirects to be handled automatically (optional, default=5) :param product: name or (name, version) tuple for the client product to be listed in the ``User-Agent`` header (optional) :param chunk_size: number of bytes to retrieve per chunk (optional, default=4096) :return: file-like :py:class:`Response <httpstream.http.Response>` object from which content can be read """ rq = Request("GET", self._uri, None, headers) return rq.submit(redirect_limit=redirect_limit, **kwargs) def put(self, body=None, headers=None, **kwargs): """ Issue a ``PUT`` request to this resource. """ rq = Request("PUT", self._uri, body, headers) return rq.submit(**kwargs) def post(self, body=None, headers=None, **kwargs): """ Issue a ``POST`` request to this resource. """ rq = Request("POST", self._uri, body, headers) return rq.submit(**kwargs) def delete(self, headers=None, **kwargs): """ Issue a ``DELETE`` request to this resource. """ rq = Request("DELETE", self._uri, None, headers) return rq.submit(**kwargs) def head(self, headers=None, redirect_limit=5, **kwargs): """ Issue a ``HEAD`` request to this resource. """ rq = Request("HEAD", self._uri, None, headers) return rq.submit(redirect_limit=redirect_limit, **kwargs)
class Resource(object): """ A web resource identified by a URI. """ def __init__(self, uri): if isinstance(uri, URI): self._uri = uri else: self._uri = URI(uri) def __str__(self): return "<{0}>".format(str(self._uri)) def __repr__(self): return "{0}({1})".format(self.__class__.__name__, repr(self._uri.string)) def __eq__(self, other): """ Determine equality of two objects based on URI. """ return self._uri == other._uri def __ne__(self, other): """ Determine inequality of two objects based on URI. """ return self._uri != other._uri def __bool__(self): return bool(self._uri) def __nonzero__(self): return bool(self._uri) @property def __uri__(self): return self._uri @property def uri(self): """ The URI of this resource. """ return self._uri def resolve(self, reference, strict=True): """ Resolve a URI reference against the URI for this resource, returning a new resource represented by the new target URI. """ return Resource(self._uri.resolve(reference, strict)) def get(self, headers=None, redirect_limit=5, **kwargs): """ Issue a ``GET`` request to this resource. :param headers: headers to be included in the request (optional) :type headers: dict :param redirect_limit: maximum number of redirects to be handled automatically (optional, default=5) :param product: name or (name, version) tuple for the client product to be listed in the ``User-Agent`` header (optional) :param chunk_size: number of bytes to retrieve per chunk (optional, default=4096) :return: file-like :py:class:`Response <httpstream.http.Response>` object from which content can be read """ rq = Request("GET", self._uri, None, headers) return rq.submit(redirect_limit=redirect_limit, **kwargs) def put(self, body=None, headers=None, **kwargs): """ Issue a ``PUT`` request to this resource. """ rq = Request("PUT", self._uri, body, headers) return rq.submit(**kwargs) def post(self, body=None, headers=None, **kwargs): """ Issue a ``POST`` request to this resource. """ rq = Request("POST", self._uri, body, headers) return rq.submit(**kwargs) def delete(self, headers=None, **kwargs): """ Issue a ``DELETE`` request to this resource. """ rq = Request("DELETE", self._uri, None, headers) return rq.submit(**kwargs) def head(self, headers=None, redirect_limit=5, **kwargs): """ Issue a ``HEAD`` request to this resource. """ rq = Request("HEAD", self._uri, None, headers) return rq.submit(redirect_limit=redirect_limit, **kwargs)