From 0e51e48473efb5fbd4eebeb1c78ee8a248927eaa Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 2 Feb 2016 02:12:38 -0500 Subject: [PATCH] PreparedRequest.send() --- 3.0-HISTORY.rst | 3 +++ requests/models.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/3.0-HISTORY.rst b/3.0-HISTORY.rst index 18194e0d..d71bc3bd 100644 --- a/3.0-HISTORY.rst +++ b/3.0-HISTORY.rst @@ -17,5 +17,8 @@ - Rename the ``resp`` argument from ``Session.resolve_redirects`` to ``response``. +- New ``PreparedRequest.send`` method. Now, you can +``Request().prepare().send()``. + .. _#2002: https://github.com/kennethreitz/requests/issues/2002 .. _#2631: https://github.com/kennethreitz/requests/issues/2631 diff --git a/requests/models.py b/requests/models.py index b1ec9ca7..e1c3ccd5 100644 --- a/requests/models.py +++ b/requests/models.py @@ -14,6 +14,7 @@ from io import BytesIO, UnsupportedOperation from .hooks import default_hooks from .structures import CaseInsensitiveDict +import requests from .auth import HTTPBasicAuth from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar from .packages.urllib3.fields import RequestField @@ -525,6 +526,14 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): for event in hooks: self.register_hook(event, hooks[event]) + def send(self, session=None, **send_kwargs): + """Sends the PreparedRequest to the given Session. + If none is provided, one is created for you.""" + session = requests.Session() if session is None else session + + with session: + return session.send(self, **send_kwargs) + class Response(object): """The :class:`Response ` object, which contains a