Skip to content
Myr

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

1 file+86 added−88 removed

modified:requests/core.py+86 added-88 removed
24 unmodified lines
25AUTOAUTHS = []262728class _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 = method3637 def get_method(self):38 if self.method:39 return self.method
1 unmodified line
41 return urllib2.Request.get_method(self)424344class 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 = None
4 unmodified lines
57 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 line
75 object.__setattr__(self, name, value)76 77 78 def _checks(self):81 """Deterministic checks for consistiency."""79 """Deterministic checks for consistiency."""8081 if not self.url:82 raise URLRequired8384 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:8990 # create a password manager
9 unmodified lines
100 return urllib2.urlopen101102 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.107111 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.109110 :param anyway: If True, request will be sent, even if it has111 already been sent.112 """113114 self._checks()115116 success = False119 117118119 if self.method in ('GET', 'HEAD', 'DELETE'):120 if (not self.sent) or anyway:121122 # url encode GET params if it's a dict
45 unmodified lines
168 except urllib2.HTTPError, why:169 self.response.status_code = why.code170171173174172 elif self.method == 'POST':173 if (not self.sent) or anyway:174175 req = _Request(self.url, method='POST')
27 unmodified lines
203 return success204 205206class 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 """211212 def __init__(self):213 self.content = None214 self.status_code = None
7 unmodified lines
222 return repr223224 225class 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 = password238239240241def get(url, params={}, headers={}, auth=None):245 """Sends a GET request. Returns :class:`Response` object.242 """Sends a GET request. Returns :class:`Response` object.243247 :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 lines
260 return r.response261262263def head(url, params={}, headers={}, auth=None):267 """Sends a HEAD request. Returns :class:`Response` object.264 """Sends a HEAD request. Returns :class:`Response` object.265269 :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 lines
283 return r.response284285286def post(url, data={}, headers={}, auth=None):290 """Sends a POST request. Returns :class:`Response` object.287 """Sends a POST request. Returns :class:`Response` object.288292 :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()296297 r.url = url
8 unmodified lines
306 return r.response307 308 309def put(url, data='', headers={}, auth=None):313 """Sends a PUT request. Returns :class:`Response` object.310 """Sends a PUT request. Returns :class:`Response` object.311315 :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()319320 r.url = url
8 unmodified lines
329 return r.response330331 332def delete(url, params={}, headers={}, auth=None):336 """Sends a DELETE request. Returns :class:`Response` object.333 """Sends a DELETE request. Returns :class:`Response` object.334338 :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()342343 r.url = url
8 unmodified lines
352 return r.response353354355def 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.360364 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!366370 :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 """370371 global AUTOAUTHS372 373 AUTOAUTHS.append((url, authobject))374375376def _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 """380381 return _get_autoauth(url) if not auth else auth382383 384def _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 None392393class 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."""395396class AuthenticationError(RequestException):399 """The authentication credentials provided were invalid."""397 """The authentication credentials provided were invalid."""398 399class URLRequired(RequestException):402 """A valid URL is required to make a request."""400 """A valid URL is required to make a request."""401 402class InvalidMethod(RequestException):405 """An inappropriate method was attempted."""403 """An inappropriate method was attempted."""