1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
.. _Adapter:
=============
Adapter Usage
=============
Creating an Adapter
===================
The standard `requests`_ means of using a `transport adapter`_ is to `mount`_ it on a created session.
This is not the only way to load the adapter, however the same interactions will be used.
When mounting an adapter, keep in mind that:
* for a given URL, `requests`_ will use the adapter associated to the longest matching prefix;
* session are created with adapters for the :py:const:`http://` and :py:const:`https://` prefixes
(and thus adapters mounted on :py:const:`http` or :py:const:`https` will never be used);
* `requests`_ only prepares URLs for *http* schemes (start with http and only contains letters, numbers, and the + and - signs).
In particular :py:data:`params` won't work with the :py:const:`mock://` scheme, but will with :py:const:`http+mock://`.
If you are not familiar with adapters, prefer the mocker approach (see :ref:`Mocker`).
.. doctest::
>>> import requests
>>> import requests_mock
>>> session = requests.Session()
>>> adapter = requests_mock.Adapter()
>>> session.mount('mock://', adapter)
At this point any requests made by the session to a URI starting with `mock://` will be sent to our adapter.
.. _requests: https://requests.readthedocs.io
.. _transport adapter: https://requests.readthedocs.io/en/master/user/advanced/#transport-adapters
.. _mount: https://requests.readthedocs.io/en/master/api/#requests.Session.mount
|