Merge branch 'feature/timeout' into develop
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 · parents 59dd3ed, 45ca6d8 · Browse files · 2 files·+44 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+44 added−14 removed
- other:+44 added−14 removed
Provenance
- Commit
a046a04- Tree
73146c3- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
59dd3ed- Parent 2
45ca6d8
2 files changed+44 added−14 removed
modified:requests/__init__.py+25 added
1 # -*- coding: utf-8 -*-2 3+import inspect4+5 import packages6 from core import *7 8 from core import __version__9+10+timeout = None11+12+class settings:13+ """Context manager for settings."""14+ 15+ cache = {}16+ 17+ def __init__(self, timeout):18+ self.module = inspect.getmodule(self)19+ 20+ # Cache settings21+ self.cache['timeout'] = self.module.timeout22+ 23+ self.module.timeout = timeout24+ 25+ def __enter__(self):26+ pass27+ 28+ def __exit__(self, type, value, traceback):29+ # Restore settings 30+ for key in self.cache:31+ setattr(self.module, key, self.cache[key])modified:requests/core.py+19 added-14 removed
10 unmodified lines11 """12 13 from __future__ import absolute_import14 15+import requests16 import urllib17 import urllib218 import socket19 import zlib4 unmodified lines24 from .packages.poster.encode import multipart_encode25 from .packages.poster.streaminghttp import register_openers, get_handlers26 27 27-28 __title__ = 'requests'29 __version__ = '0.3.2'30 __build__ = 0x00030231 __author__ = 'Kenneth Reitz'6 unmodified lines38 'URLRequired', 'InvalidMethod', 'HTTPError'39 ]40 41 42-42 class _Request(urllib2.Request):43 """Hidden wrapper around the urllib2.Request object. Allows for manual44 setting of HTTP methods.45 """410 unmodified lines456 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.457 :param cookies: (optional) CookieJar object to send with the :class:`Request`.458 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.459 :param auth: (optional) AuthObject to enable Basic HTTP Auth.460+ :param timeout: (optional) Float describing the timeout of the request.461 """462 data = kwargs.pop('data', dict()) or kwargs.pop('params', dict())463-463+ 464 r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}),465 cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None),466 auth=kwargs.pop('auth', auth_manager.get_auth(url)),467- timeout=kwargs.pop('timeout', None))467+ timeout=kwargs.pop('timeout', requests.timeout))468 r.send()469 470 return r.response471 472 473-def get(url, params={}, headers={}, cookies=None, auth=None, timeout=None):473+def get(url, params={}, headers={}, cookies=None, auth=None, **kwargs):474 """Sends a GET request. Returns :class:`Response` object.475 476 :param url: URL for the new :class:`Request` object.477 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.478 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.479 :param cookies: (optional) CookieJar object to send with the :class:`Request`.480 :param auth: (optional) AuthObject to enable Basic HTTP Auth.481+ :param timeout: (optional) Float describing the timeout of the request.482 """483 483- return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout)484+ return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)485 486 486-def head(url, params={}, headers={}, cookies=None, auth=None, timeout=None):487+def head(url, params={}, headers={}, cookies=None, auth=None, **kwargs):488 """Sends a HEAD request. Returns :class:`Response` object.489 490 :param url: URL for the new :class:`Request` object.491 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.492 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.493 :param cookies: (optional) CookieJar object to send with the :class:`Request`.494 :param auth: (optional) AuthObject to enable Basic HTTP Auth.495+ :param timeout: (optional) Float describing the timeout of the request.496 """497 496- return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout)498+ return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)499 500 499-def post(url, data={}, headers={}, files=None, cookies=None, auth=None, timeout=None):501+def post(url, data={}, headers={}, files=None, cookies=None, auth=None, **kwargs):502 """Sends a POST request. Returns :class:`Response` object.503 504 :param url: URL for the new :class:`Request` object.505 :param data: (optional) Dictionary of POST data to send with the :class:`Request`.506 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.507 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.508 :param cookies: (optional) CookieJar object to send with the :class:`Request`.509 :param auth: (optional) AuthObject to enable Basic HTTP Auth.510+ :param timeout: (optional) Float describing the timeout of the request.511 """512 510- return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout)513+ return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs)514 515 513-def put(url, data='', headers={}, files={}, cookies=None, auth=None, timeout=None):516+def put(url, data='', headers={}, files={}, cookies=None, auth=None, **kwargs):517 """Sends a PUT request. Returns :class:`Response` object.518 519 :param url: URL for the new :class:`Request` object.520 :param params: (optional) Bytes of PUT Data to send with the :class:`Request`.521 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.522 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.523 :param cookies: (optional) CookieJar object to send with the :class:`Request`.524 :param auth: (optional) AuthObject to enable Basic HTTP Auth.525+ :param timeout: (optional) Float describing the timeout of the request.526 """527 524- return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout)528+ return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs)529 530 527-def delete(url, params={}, headers={}, cookies=None, auth=None, timeout=None):531+def delete(url, params={}, headers={}, cookies=None, auth=None, **kwargs):532 """Sends a DELETE request. Returns :class:`Response` object.533 534 :param url: URL for the new :class:`Request` object.535 :param params: (optional) Dictionary of DELETE Parameters to send with the :class:`Request`.536 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.537 :param cookies: (optional) CookieJar object to send with the :class:`Request`.538 :param auth: (optional) AuthObject to enable Basic HTTP Auth.539+ :param timeout: (optional) Float describing the timeout of the request.540 """541 537- return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout)542+ return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)543 544 545 546 class RequestException(Exception):11 unmodified lines