Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for preparing requests with base URL #317

Merged

Conversation

mbBRCM
Copy link
Contributor

@mbBRCM mbBRCM commented Oct 13, 2021

We now override the prepare_request method of the Session
to generate a complete URL from the session's base URL
and a partial resource name. The request is then prepared with
the complete URL.

from requests import Request
from requests_toolbelt import sessions
s = sessions.BaseUrlSession(
... base_url='https://example.com/resource/')
request = Request(method='GET', url='sub-resource/')
prepared_request = s.prepare_request(request)
r = s.send(prepared_request)
print(r.request.url)
https://example.com/resource/sub-resource

Closes #315.

Comment on lines 73 to 78
def request(self, method, url, *args, **kwargs):
"""Send the request after generating the complete URL."""
"""Construct and send the request after generating the
complete URL.
"""
url = self.create_url(url)
return super(BaseUrlSession, self).request(
method, url, *args, **kwargs
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can get rid of this because super().request will go through prepare_request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

We now override the `prepare_request` method of the Session
to generate a complete URL from the session's base URL
and a partial resource name. The request is then prepared with
the complete URL.

  >>> from requests import Request
  >>> from requests_toolbelt import sessions
  >>> s = sessions.BaseUrlSession(
  ...     base_url='https://example.com/resource/')
  >>> request = Request(method='GET', url='sub-resource/')
  >>> prepared_request = s.prepare_request(request)
  >>> r = s.send(prepared_request)
  >>> print(r.request.url)
  https://example.com/resource/sub-resource

Closes requests#315.
@mbBRCM mbBRCM force-pushed the 315-prepared-requests-with-base-url branch from beeab08 to d2810a2 Compare October 26, 2021 18:28
@sigmavirus24 sigmavirus24 merged commit 1112280 into requests:master Oct 28, 2021
@sigmavirus24
Copy link
Collaborator

Thanks @mbBRCM ! : 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[requests_toolbelt.session] Prepared requests do not have the complete url
2 participants