diff --git a/requests/core.py b/requests/core.py index e8357a20..60370b2d 100644 --- a/requests/core.py +++ b/requests/core.py @@ -26,16 +26,13 @@ AUTOAUTHS = [] class _Request(urllib2.Request): - """Hidden wrapper around the urllib2.Request object. Allows for manual - setting of HTTP methods. - """ + """Hidden wrapper around the urllib2.Request object. Allows for manual + setting of HTTP methods. + """ - def __init__(self, url, - data=None, headers={}, origin_req_host=None, - unverifiable=False, method=None): - urllib2.Request.__init__( self, url, data, headers, origin_req_host, - unverifiable) - self.method = method + def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None): + urllib2.Request.__init__( self, url, data, headers, origin_req_host, unverifiable) + self.method = method def get_method(self): if self.method: @@ -45,10 +42,9 @@ class _Request(urllib2.Request): class Request(object): - """The :class:`Request` object. It carries out all functionality of - Requests. Recommended interface is with the Requests functions. - - """ + """The :class:`Request` object. It carries out all functionality of + Requests. Recommended interface is with the Requests functions. + """ _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE') @@ -62,6 +58,7 @@ class Request(object): self.auth = None self.sent = False + def __repr__(self): try: repr = '' % (self.method) @@ -69,6 +66,7 @@ class Request(object): repr = '' return repr + def __setattr__(self, name, value): if (name == 'method') and (value): if not value in self._METHODS: @@ -78,15 +76,14 @@ class Request(object): def _checks(self): - """Deterministic checks for consistiency.""" + """Deterministic checks for consistiency.""" if not self.url: raise URLRequired def _get_opener(self): - """ Creates appropriate opener object for urllib2. - """ + """Creates appropriate opener object for urllib2.""" if self.auth: @@ -104,19 +101,21 @@ class Request(object): def send(self, anyway=False): - """Sends the request. Returns True of successfull, false if not. - If there was an HTTPError during transmission, - self.response.status_code will contain the HTTPError code. + """Sends the request. Returns True of successfull, false if not. + If there was an HTTPError during transmission, + self.response.status_code will contain the HTTPError code. + + Once a request is successfully sent, `sent` will equal True. + + :param anyway: If True, request will be sent, even if it has + already been sent. + """ - Once a request is successfully sent, `sent` will equal True. - - :param anyway: If True, request will be sent, even if it has - already been sent. - """ self._checks() success = False - + + if self.method in ('GET', 'HEAD', 'DELETE'): if (not self.sent) or anyway: @@ -170,8 +169,6 @@ class Request(object): self.response.status_code = why.code - - elif self.method == 'POST': if (not self.sent) or anyway: @@ -207,10 +204,10 @@ class Request(object): class Response(object): - """The :class:`Request` object. All :class:`Request` objects contain a - :class:`Request.response ` attribute, which is an instance of - this class. - """ + """The :class:`Request` object. All :class:`Request` objects contain a + :class:`Request.response ` attribute, which is an instance of + this class. + """ def __init__(self): self.content = None @@ -226,14 +223,14 @@ class Response(object): class AuthObject(object): - """The :class:`AuthObject` is a simple HTTP Authentication token. When - given to a Requests function, it enables Basic HTTP Authentication for that - Request. You can also enable Authorization for domain realms with AutoAuth. - See AutoAuth for more details.s - - :param username: Username to authenticate with. + """The :class:`AuthObject` is a simple HTTP Authentication token. When + given to a Requests function, it enables Basic HTTP Authentication for that + Request. You can also enable Authorization for domain realms with AutoAuth. + See AutoAuth for more details. + + :param username: Username to authenticate with. :param password: Password for given username. - """ + """ def __init__(self, username, password): self.username = username @@ -242,13 +239,13 @@ class AuthObject(object): def get(url, params={}, headers={}, auth=None): - """Sends a GET request. Returns :class:`Response` object. + """Sends a GET request. Returns :class:`Response` object. - :param url: URL for the new :class:`Request` object. - :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. - :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. - :param auth: (optional) AuthObject to enable Basic HTTP Auth. - """ + :param url: URL for the new :class:`Request` object. + :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. + :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. + :param auth: (optional) AuthObject to enable Basic HTTP Auth. + """ r = Request() @@ -264,13 +261,13 @@ def get(url, params={}, headers={}, auth=None): def head(url, params={}, headers={}, auth=None): - """Sends a HEAD request. Returns :class:`Response` object. + """Sends a HEAD request. Returns :class:`Response` object. - :param url: URL for the new :class:`Request` object. - :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. - :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. - :param auth: (optional) AuthObject to enable Basic HTTP Auth. - """ + :param url: URL for the new :class:`Request` object. + :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. + :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. + :param auth: (optional) AuthObject to enable Basic HTTP Auth. + """ r = Request() @@ -287,13 +284,13 @@ def head(url, params={}, headers={}, auth=None): def post(url, data={}, headers={}, auth=None): - """Sends a POST request. Returns :class:`Response` object. + """Sends a POST request. Returns :class:`Response` object. - :param url: URL for the new :class:`Request` object. - :param data: (optional) Dictionary of POST Data to send with the :class:`Request`. - :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. - :param auth: (optional) AuthObject to enable Basic HTTP Auth. - """ + :param url: URL for the new :class:`Request` object. + :param data: (optional) Dictionary of POST Data to send with the :class:`Request`. + :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. + :param auth: (optional) AuthObject to enable Basic HTTP Auth. + """ r = Request() @@ -310,13 +307,13 @@ def post(url, data={}, headers={}, auth=None): def put(url, data='', headers={}, auth=None): - """Sends a PUT request. Returns :class:`Response` object. + """Sends a PUT request. Returns :class:`Response` object. - :param url: URL for the new :class:`Request` object. - :param data: (optional) Bytes of PUT Data to send with the :class:`Request`. - :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. - :param auth: (optional) AuthObject to enable Basic HTTP Auth. - """ + :param url: URL for the new :class:`Request` object. + :param data: (optional) Bytes of PUT Data to send with the :class:`Request`. + :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. + :param auth: (optional) AuthObject to enable Basic HTTP Auth. + """ r = Request() @@ -333,13 +330,13 @@ def put(url, data='', headers={}, auth=None): def delete(url, params={}, headers={}, auth=None): - """Sends a DELETE request. Returns :class:`Response` object. + """Sends a DELETE request. Returns :class:`Response` object. - :param url: URL for the new :class:`Request` object. - :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. - :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. - :param auth: (optional) AuthObject to enable Basic HTTP Auth. - """ + :param url: URL for the new :class:`Request` object. + :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. + :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. + :param auth: (optional) AuthObject to enable Basic HTTP Auth. + """ r = Request() @@ -356,35 +353,36 @@ def delete(url, params={}, headers={}, auth=None): def add_autoauth(url, authobject): - """Registers given AuthObject to given URL domain. for auto-activation. - Once a URL is registered with an AuthObject, the configured HTTP - Authentication will be used for all requests with URLS containing the given - URL string. + """Registers given AuthObject to given URL domain. for auto-activation. + Once a URL is registered with an AuthObject, the configured HTTP + Authentication will be used for all requests with URLS containing the given + URL string. - Example: :: - >>> c_auth = requests.AuthObject('kennethreitz', 'xxxxxxx') - >>> requests.add_autoauth('https://convore.com/api/', c_auth) - >>> r = requests.get('https://convore.com/api/account/verify.json') - # Automatically HTTP Authenticated! Wh00t! + Example: :: + >>> c_auth = requests.AuthObject('kennethreitz', 'xxxxxxx') + >>> requests.add_autoauth('https://convore.com/api/', c_auth) + >>> r = requests.get('https://convore.com/api/account/verify.json') + # Automatically HTTP Authenticated! Wh00t! + + :param url: Base URL for given AuthObject to auto-activate for. + :param authobject: AuthObject to auto-activate. + """ - :param url: Base URL for given AuthObject to auto-activate for. - :param authobject: AuthObject to auto-activate. - """ global AUTOAUTHS AUTOAUTHS.append((url, authobject)) def _detect_auth(url, auth): - """Returns registered AuthObject for given url if available, defaulting to - given AuthObject.""" + """Returns registered AuthObject for given url if available, defaulting to + given AuthObject. + """ return _get_autoauth(url) if not auth else auth def _get_autoauth(url): - """Returns registered AuthObject for given url if available. - """ + """Returns registered AuthObject for given url if available.""" for (autoauth_url, auth) in AUTOAUTHS: if autoauth_url in url: @@ -393,13 +391,13 @@ def _get_autoauth(url): return None class RequestException(Exception): - """There was an ambiguous exception that occured while handling your request.""" + """There was an ambiguous exception that occured while handling your request.""" class AuthenticationError(RequestException): - """The authentication credentials provided were invalid.""" + """The authentication credentials provided were invalid.""" class URLRequired(RequestException): - """A valid URL is required to make a request.""" + """A valid URL is required to make a request.""" class InvalidMethod(RequestException): - """An inappropriate method was attempted.""" + """An inappropriate method was attempted."""