Docstring whitespace
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent d2427ec · Browse files · 1 file·+86 added−88 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+86 added−88 removed
- other:+86 added−88 removed
Provenance
- Commit
f66eafd- Tree
d930ef2- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
d2427ec
1 file changed+86 added−88 removed
modified:requests/core.py+86 added-88 removed
24 unmodified lines25 AUTOAUTHS = []26 27 28 class _Request(urllib2.Request):29- """Hidden wrapper around the urllib2.Request object. Allows for manual30- setting of HTTP methods.31- """29+ """Hidden wrapper around the urllib2.Request object. Allows for manual30+ setting of HTTP methods.31+ """32 33- def __init__(self, url,34- data=None, headers={}, origin_req_host=None,35- unverifiable=False, method=None):36- urllib2.Request.__init__( self, url, data, headers, origin_req_host,37- unverifiable)38- self.method = method33+ def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None):34+ urllib2.Request.__init__( self, url, data, headers, origin_req_host, unverifiable)35+ self.method = method36 37 def get_method(self):38 if self.method:39 return self.method1 unmodified line41 return urllib2.Request.get_method(self)42 43 44 class Request(object):48- """The :class:`Request` object. It carries out all functionality of49- Requests. Recommended interface is with the Requests functions.45+ """The :class:`Request` object. It carries out all functionality of46+ Requests. Recommended interface is with the Requests functions.47+ """48 51- """52- 49 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')50 51 def __init__(self):52 self.url = None4 unmodified lines57 self.response = Response()58 self.auth = None59 self.sent = False60 61+ 62 def __repr__(self):63 try:64 repr = '<Request [%s]>' % (self.method)65 except:66 repr = '<Request object>'67 return repr68 69+ 70 def __setattr__(self, name, value):71 if (name == 'method') and (value):72 if not value in self._METHODS:73 raise InvalidMethod()1 unmodified line75 object.__setattr__(self, name, value)76 77 78 def _checks(self):81- """Deterministic checks for consistiency."""79+ """Deterministic checks for consistiency."""80 81 if not self.url:82 raise URLRequired83 84 85 def _get_opener(self):88- """ Creates appropriate opener object for urllib2.89- """86+ """Creates appropriate opener object for urllib2."""87 88 if self.auth:89 90 # create a password manager9 unmodified lines100 return urllib2.urlopen101 102 103 def send(self, anyway=False):107- """Sends the request. Returns True of successfull, false if not.108- If there was an HTTPError during transmission,109- self.response.status_code will contain the HTTPError code.104+ """Sends the request. Returns True of successfull, false if not.105+ If there was an HTTPError during transmission,106+ self.response.status_code will contain the HTTPError code.107 111- Once a request is successfully sent, `sent` will equal True.112- 113- :param anyway: If True, request will be sent, even if it has114- already been sent.115- """108+ Once a request is successfully sent, `sent` will equal True.109+110+ :param anyway: If True, request will be sent, even if it has111+ already been sent.112+ """113+114 self._checks()115 116 success = False119- 117+118+119 if self.method in ('GET', 'HEAD', 'DELETE'):120 if (not self.sent) or anyway:121 122 # url encode GET params if it's a dict45 unmodified lines168 except urllib2.HTTPError, why:169 self.response.status_code = why.code170 171 173-174-172 elif self.method == 'POST':173 if (not self.sent) or anyway:174 175 req = _Request(self.url, method='POST')27 unmodified lines203 return success204 205 206 class Response(object):210- """The :class:`Request` object. All :class:`Request` objects contain a211- :class:`Request.response <response>` attribute, which is an instance of212- this class.213- """207+ """The :class:`Request` object. All :class:`Request` objects contain a208+ :class:`Request.response <response>` attribute, which is an instance of209+ this class.210+ """211 212 def __init__(self):213 self.content = None214 self.status_code = None7 unmodified lines222 return repr223 224 225 class AuthObject(object):229- """The :class:`AuthObject` is a simple HTTP Authentication token. When230- given to a Requests function, it enables Basic HTTP Authentication for that231- Request. You can also enable Authorization for domain realms with AutoAuth.232- See AutoAuth for more details.s233- 234- :param username: Username to authenticate with.226+ """The :class:`AuthObject` is a simple HTTP Authentication token. When227+ given to a Requests function, it enables Basic HTTP Authentication for that228+ Request. You can also enable Authorization for domain realms with AutoAuth.229+ See AutoAuth for more details.230+ 231+ :param username: Username to authenticate with.232 :param password: Password for given username.236- """233+ """234 235 def __init__(self, username, password):236 self.username = username237 self.password = password238 239 240 241 def get(url, params={}, headers={}, auth=None):245- """Sends a GET request. Returns :class:`Response` object.242+ """Sends a GET request. Returns :class:`Response` object.243 247- :param url: URL for the new :class:`Request` object.248- :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.249- :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.250- :param auth: (optional) AuthObject to enable Basic HTTP Auth.251- """244+ :param url: URL for the new :class:`Request` object.245+ :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.246+ :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.247+ :param auth: (optional) AuthObject to enable Basic HTTP Auth.248+ """249 250 r = Request()251 252 r.method = 'GET'7 unmodified lines260 return r.response261 262 263 def head(url, params={}, headers={}, auth=None):267- """Sends a HEAD request. Returns :class:`Response` object.264+ """Sends a HEAD request. Returns :class:`Response` object.265 269- :param url: URL for the new :class:`Request` object.270- :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.271- :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.272- :param auth: (optional) AuthObject to enable Basic HTTP Auth.273- """266+ :param url: URL for the new :class:`Request` object.267+ :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.268+ :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.269+ :param auth: (optional) AuthObject to enable Basic HTTP Auth.270+ """271 272 r = Request()273 274 r.method = 'HEAD'8 unmodified lines283 return r.response284 285 286 def post(url, data={}, headers={}, auth=None):290- """Sends a POST request. Returns :class:`Response` object.287+ """Sends a POST request. Returns :class:`Response` object.288 292- :param url: URL for the new :class:`Request` object.293- :param data: (optional) Dictionary of POST Data to send with the :class:`Request`.294- :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.295- :param auth: (optional) AuthObject to enable Basic HTTP Auth.296- """289+ :param url: URL for the new :class:`Request` object.290+ :param data: (optional) Dictionary of POST Data to send with the :class:`Request`.291+ :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.292+ :param auth: (optional) AuthObject to enable Basic HTTP Auth.293+ """294 295 r = Request()296 297 r.url = url8 unmodified lines306 return r.response307 308 309 def put(url, data='', headers={}, auth=None):313- """Sends a PUT request. Returns :class:`Response` object.310+ """Sends a PUT request. Returns :class:`Response` object.311 315- :param url: URL for the new :class:`Request` object.316- :param data: (optional) Bytes of PUT Data to send with the :class:`Request`.317- :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.318- :param auth: (optional) AuthObject to enable Basic HTTP Auth.319- """312+ :param url: URL for the new :class:`Request` object.313+ :param data: (optional) Bytes of PUT Data to send with the :class:`Request`.314+ :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.315+ :param auth: (optional) AuthObject to enable Basic HTTP Auth.316+ """317 318 r = Request()319 320 r.url = url8 unmodified lines329 return r.response330 331 332 def delete(url, params={}, headers={}, auth=None):336- """Sends a DELETE request. Returns :class:`Response` object.333+ """Sends a DELETE request. Returns :class:`Response` object.334 338- :param url: URL for the new :class:`Request` object.339- :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.340- :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.341- :param auth: (optional) AuthObject to enable Basic HTTP Auth.342- """335+ :param url: URL for the new :class:`Request` object.336+ :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.337+ :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.338+ :param auth: (optional) AuthObject to enable Basic HTTP Auth.339+ """340 341 r = Request()342 343 r.url = url8 unmodified lines352 return r.response353 354 355 def add_autoauth(url, authobject):359- """Registers given AuthObject to given URL domain. for auto-activation.360- Once a URL is registered with an AuthObject, the configured HTTP361- Authentication will be used for all requests with URLS containing the given362- URL string.356+ """Registers given AuthObject to given URL domain. for auto-activation.357+ Once a URL is registered with an AuthObject, the configured HTTP358+ Authentication will be used for all requests with URLS containing the given359+ URL string.360 364- Example: ::365- >>> c_auth = requests.AuthObject('kennethreitz', 'xxxxxxx')366- >>> requests.add_autoauth('https://convore.com/api/', c_auth)367- >>> r = requests.get('https://convore.com/api/account/verify.json')368- # Automatically HTTP Authenticated! Wh00t!361+ Example: ::362+ >>> c_auth = requests.AuthObject('kennethreitz', 'xxxxxxx')363+ >>> requests.add_autoauth('https://convore.com/api/', c_auth)364+ >>> r = requests.get('https://convore.com/api/account/verify.json')365+ # Automatically HTTP Authenticated! Wh00t!366 370- :param url: Base URL for given AuthObject to auto-activate for.371- :param authobject: AuthObject to auto-activate.372- """367+ :param url: Base URL for given AuthObject to auto-activate for.368+ :param authobject: AuthObject to auto-activate.369+ """370+371 global AUTOAUTHS372 373 AUTOAUTHS.append((url, authobject))374 375 376 def _detect_auth(url, auth):379- """Returns registered AuthObject for given url if available, defaulting to380- given AuthObject."""377+ """Returns registered AuthObject for given url if available, defaulting to378+ given AuthObject.379+ """380 381 return _get_autoauth(url) if not auth else auth382 383 384 def _get_autoauth(url):386- """Returns registered AuthObject for given url if available.387- """385+ """Returns registered AuthObject for given url if available."""386 387 for (autoauth_url, auth) in AUTOAUTHS:388 if autoauth_url in url: 389 return auth390 391 return None392 393 class RequestException(Exception):396- """There was an ambiguous exception that occured while handling your request."""394+ """There was an ambiguous exception that occured while handling your request."""395 396 class AuthenticationError(RequestException):399- """The authentication credentials provided were invalid."""397+ """The authentication credentials provided were invalid."""398 399 class URLRequired(RequestException):402- """A valid URL is required to make a request."""400+ """A valid URL is required to make a request."""401 402 class InvalidMethod(RequestException):405- """An inappropriate method was attempted."""403+ """An inappropriate method was attempted."""