Skip to content
Myr

Shell of Transport Adapter documentation.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Cory Benfield · committed by Cory Benfield · parent c73f653 · Browse files · 1 file+44 added−8 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+44 added−8 removed

  • other:+44 added−8 removed

1 file+44 added−8 removed

modified:docs/user/advanced.rst+44 added-8 removed
78 unmodified lines
7980Prepared Requests81-----------------8283Whenever you receive a :class:`Response <requests.models.Response>` object 84from an API call or a Session call, the ``request`` attribute is actually the 85``PreparedRequest`` that was used. In some cases you may wish to do some extra 86work to the body or headers (or anything else really) before sending a 83Whenever you receive a :class:`Response <requests.models.Response>` object84from an API call or a Session call, the ``request`` attribute is actually the85``PreparedRequest`` that was used. In some cases you may wish to do some extra86work to the body or headers (or anything else really) before sending a87request. The simple recipe for this is the following::8889 from requests import Request, Session90
15 unmodified lines
106 # etc.107 )108 print(resp.status_code)109110Since you are not doing anything special with the ``Request`` object, you 111prepare it immediately and modified the ``PreparedRequest`` object. You then 112send that with the other parameters you would have sent to ``requests.*`` or 113``Sesssion.*``. 110Since you are not doing anything special with the ``Request`` object, you111prepare it immediately and modified the ``PreparedRequest`` object. You then112send that with the other parameters you would have sent to ``requests.*`` or113``Sesssion.*``.114115SSL Cert Verification116---------------------117
418 unmodified lines
536537 >>> r.links["last"]538 {'url': 'https://api.github.com/users/kennethreitz/repos?page=7&per_page=10', 'rel': 'last'}539540Transport Adapters541------------------542543As of v1.0.0, Requests has moved to a modular internal design. Part of the544reason this was done was to implement Transport Adapters, originally545`described here`_. Transport Adapters provide a mechanism to define interaction546methods for an HTTP service. In particular, they allow you to apply per-service547configuration.548549Requests ships with a single Transport Adapter, the550:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. This adapter provides the551default Requests interaction with HTTP and HTTPS using the powerful `urllib3`_552library. Whenever a Requests :class:`Session <Session>` is initialized, one of553these is attached to the :class:`Session <Session>` object for HTTP, and one554for HTTPS.555556Requests enables users to create and use their own Transport Adapters that557provide specific functionality. Once created, a Transport Adapter can be558mounted to a Session object, along with an indication of which web services559it should apply to.560561::562563 >>> s = requests.Session()564 >>> s.mount('http://www.github.com', MyAdapter())565566The mount call registers a specific instance of a Transport Adapter to a567prefix. Once mounted, any HTTP request made using that session whose URL starts568with the given prefix will use the given Transport Adapter.569570Implementing a Transport Adapter is beyond the scope of this documentation, but571a good start would be to subclass the ``requests.adapters.BaseAdapter`` class.572573.. _`described here`: http://kennethreitz.org/exposures/the-future-of-python-http574.. _`urllib3`: https://github.com/shazow/urllib3575