sessions.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import requests
  2. from ._compat import urljoin
  3. class BaseUrlSession(requests.Session):
  4. """A Session with a URL that all requests will use as a base.
  5. Let's start by looking at a few examples:
  6. .. code-block:: python
  7. >>> from requests_toolbelt import sessions
  8. >>> s = sessions.BaseUrlSession(
  9. ... base_url='https://example.com/resource/')
  10. >>> r = s.get('sub-resource/', params={'foo': 'bar'})
  11. >>> print(r.request.url)
  12. https://example.com/resource/sub-resource/?foo=bar
  13. Our call to the ``get`` method will make a request to the URL passed in
  14. when we created the Session and the partial resource name we provide.
  15. We implement this by overriding the ``request`` method of the Session.
  16. Likewise, we override the ``prepare_request`` method so you can construct
  17. a PreparedRequest in the same way:
  18. .. code-block:: python
  19. >>> from requests import Request
  20. >>> from requests_toolbelt import sessions
  21. >>> s = sessions.BaseUrlSession(
  22. ... base_url='https://example.com/resource/')
  23. >>> request = Request(method='GET', url='sub-resource/')
  24. >>> prepared_request = s.prepare_request(request)
  25. >>> r = s.send(prepared_request)
  26. >>> print(r.request.url)
  27. https://example.com/resource/sub-resource
  28. .. note::
  29. The base URL that you provide and the path you provide are **very**
  30. important.
  31. Let's look at another *similar* example
  32. .. code-block:: python
  33. >>> from requests_toolbelt import sessions
  34. >>> s = sessions.BaseUrlSession(
  35. ... base_url='https://example.com/resource/')
  36. >>> r = s.get('/sub-resource/', params={'foo': 'bar'})
  37. >>> print(r.request.url)
  38. https://example.com/sub-resource/?foo=bar
  39. The key difference here is that we called ``get`` with ``/sub-resource/``,
  40. i.e., there was a leading ``/``. This changes how we create the URL
  41. because we rely on :mod:`urllib.parse.urljoin`.
  42. To override how we generate the URL, sub-class this method and override the
  43. ``create_url`` method.
  44. Based on implementation from
  45. https://github.com/kennethreitz/requests/issues/2554#issuecomment-109341010
  46. """
  47. base_url = None
  48. def __init__(self, base_url=None):
  49. if base_url:
  50. self.base_url = base_url
  51. super(BaseUrlSession, self).__init__()
  52. def request(self, method, url, *args, **kwargs):
  53. """Send the request after generating the complete URL."""
  54. url = self.create_url(url)
  55. return super(BaseUrlSession, self).request(
  56. method, url, *args, **kwargs
  57. )
  58. def prepare_request(self, request, *args, **kwargs):
  59. """Prepare the request after generating the complete URL."""
  60. request.url = self.create_url(request.url)
  61. return super(BaseUrlSession, self).prepare_request(
  62. request, *args, **kwargs
  63. )
  64. def create_url(self, url):
  65. """Create the URL based off this partial path."""
  66. return urljoin(self.base_url, url)