Skip to content

Commit 1112280

Browse files
authored
Merge pull request #317 from mbBRCM/315-prepared-requests-with-base-url
Add support for preparing requests with base URL
2 parents 5ead53f + d2810a2 commit 1112280

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

requests_toolbelt/sessions.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class BaseUrlSession(requests.Session):
77
"""A Session with a URL that all requests will use as a base.
88
9-
Let's start by looking at an example:
9+
Let's start by looking at a few examples:
1010
1111
.. code-block:: python
1212
@@ -19,10 +19,22 @@ class BaseUrlSession(requests.Session):
1919
2020
Our call to the ``get`` method will make a request to the URL passed in
2121
when we created the Session and the partial resource name we provide.
22+
We implement this by overriding the ``request`` method of the Session.
2223
23-
We implement this by overriding the ``request`` method so most uses of a
24-
Session are covered. (This, however, precludes the use of PreparedRequest
25-
objects).
24+
Likewise, we override the ``prepare_request`` method so you can construct
25+
a PreparedRequest in the same way:
26+
27+
.. code-block:: python
28+
29+
>>> from requests import Request
30+
>>> from requests_toolbelt import sessions
31+
>>> s = sessions.BaseUrlSession(
32+
... base_url='https://example.com/resource/')
33+
>>> request = Request(method='GET', url='sub-resource/')
34+
>>> prepared_request = s.prepare_request(request)
35+
>>> r = s.send(prepared_request)
36+
>>> print(r.request.url)
37+
https://example.com/resource/sub-resource
2638
2739
.. note::
2840
@@ -65,6 +77,13 @@ def request(self, method, url, *args, **kwargs):
6577
method, url, *args, **kwargs
6678
)
6779

80+
def prepare_request(self, request, *args, **kwargs):
81+
"""Prepare the request after generating the complete URL."""
82+
request.url = self.create_url(request.url)
83+
return super(BaseUrlSession, self).prepare_request(
84+
request, *args, **kwargs
85+
)
86+
6887
def create_url(self, url):
6988
"""Create the URL based off this partial path."""
7089
return urljoin(self.base_url, url)

tests/test_sessions.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,53 @@
33
import pytest
44

55
from requests_toolbelt import sessions
6+
from requests import Request
67
from . import get_betamax
78

89

910
class TestBasedSession(unittest.TestCase):
10-
def test_with_base(self):
11+
def test_request_with_base(self):
1112
session = sessions.BaseUrlSession('https://httpbin.org/')
1213
recorder = get_betamax(session)
1314
with recorder.use_cassette('simple_get_request'):
1415
response = session.get('/get')
1516
response.raise_for_status()
1617

17-
def test_without_base(self):
18+
def test_request_without_base(self):
1819
session = sessions.BaseUrlSession()
1920
with pytest.raises(ValueError):
2021
session.get('/')
2122

22-
def test_override_base(self):
23+
def test_request_override_base(self):
2324
session = sessions.BaseUrlSession('https://www.google.com')
2425
recorder = get_betamax(session)
2526
with recorder.use_cassette('simple_get_request'):
2627
response = session.get('https://httpbin.org/get')
2728
response.raise_for_status()
2829
assert response.json()['headers']['Host'] == 'httpbin.org'
30+
31+
def test_prepared_request_with_base(self):
32+
session = sessions.BaseUrlSession('https://httpbin.org')
33+
request = Request(method="GET", url="/get")
34+
prepared_request = session.prepare_request(request)
35+
recorder = get_betamax(session)
36+
with recorder.use_cassette('simple_get_request'):
37+
response = session.send(prepared_request)
38+
response.raise_for_status()
39+
40+
def test_prepared_request_without_base(self):
41+
session = sessions.BaseUrlSession()
42+
request = Request(method="GET", url="/")
43+
with pytest.raises(ValueError):
44+
prepared_request = session.prepare_request(request)
45+
session.send(prepared_request)
46+
47+
def test_prepared_request_override_base(self):
48+
session = sessions.BaseUrlSession('https://www.google.com')
49+
request = Request(method="GET", url="https://httpbin.org/get")
50+
prepared_request = session.prepare_request(request)
51+
recorder = get_betamax(session)
52+
with recorder.use_cassette('simple_get_request'):
53+
response = session.send(prepared_request)
54+
response.raise_for_status()
55+
assert response.json()['headers']['Host'] == 'httpbin.org'

0 commit comments

Comments
 (0)