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

Expand URL interface #1601

Merged
merged 2 commits into from
Apr 27, 2021
Merged

Expand URL interface #1601

merged 2 commits into from
Apr 27, 2021

Conversation

tomchristie
Copy link
Member

Neaten out the URL interface as follows:

  • Support httpx.URL(**kwargs)
  • Support url.copy_with(params=...)
  • Support url.params
  • Support url.copy_set_param(), url.copy_add_param(), url.copy_remove_param(), url.copy_merge_params()

For instance:

u = httpx.URL("https://www.example.com/", username="tom@gmail.com", password="123 456")
assert u == "https://tom%40gmail.com:123%20456@www.example.com/"

u = httpx.URL(scheme="https", host="www.example.com", path="/')
assert u == "https://www.example.com/"

u = httpx.URL("https://www.example.com/")
u = u.copy_with(params={"search": "is green a colour?"}
assert u == "https://www.example.com/?search=is+green+a+colour%3F"

u = httpx.URL("https://www.example.com/?a=123")
assert u.params == httpx.QueryParams({"a": "123"})

u = httpx.URL("https://www.example.com/?a=123")
u = u.copy_set_param("a", "456")
assert u == "https://www.example.com/?a=456"

u = httpx.URL("https://www.example.com/?a=123")
u = u.copy_add_param("a", "456")
assert u == "https://www.example.com/?a=123&a=456"

u = httpx.URL("https://www.example.com/?a=123")
u = u.copy_remove_param("a")
assert u == "https://www.example.com/"

u = httpx.URL("https://www.example.com/?a=123")
u = u.copy_merge_params({"b": "456"})
assert u == "https://www.example.com/?a=123&b=456"

There's one edge case bit of necessary straightening out here, which is the behaviour of:

u = httpx.URL("https://www.example.com/?a=123", params={"b": "456"})

Which now replaces the params, rather than merging them. Because that's the consistent thing to do if params is just one of many possible keyword arguments there, and should get consistent behaviour.

Note however that I've continued to ensure that #652 remains resolved. So:

httpx.get("https://www.example.com/?a=123", params={"b": "456"})  # Outgoing request uses "?a=123&b=456" here.

# Is equivalent to...
url = httpx.URL("https://www.example.com/?a=123")
url = url.copy_merge_params({"b": "456"}
httpx.get(url)

@tomchristie tomchristie added the enhancement New feature or request label Apr 26, 2021
@tomchristie tomchristie added this to the v0.18 milestone Apr 26, 2021
@tomchristie tomchristie mentioned this pull request Apr 26, 2021
@tomchristie
Copy link
Member Author

Righty, this is the last set of stuff I wanted to get into 0.18. Planning to merge this tomorrow and then release.

Full release notes at #1576

@tomchristie tomchristie merged commit e67b0dd into master Apr 27, 2021
@tomchristie tomchristie deleted the expand-url-interface branch April 27, 2021 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant