Repository navigation
Replies: 1 comment
|
If repeating import httpx
import yarl
class YarlClient(httpx.Client):
def build_request(self, method, url, **kwargs):
if isinstance(url, yarl.URL):
url = str(url)
return super().build_request(method, url, **kwargs)
def handler(request):
return httpx.Response(200, json={
"method": request.method,
"target": request.url.raw_path.decode("ascii"),
"marker": request.headers["x-example"],
})
with YarlClient(
base_url="https://example.com/api/",
headers={"X-Example": "preserved"},
transport=httpx.MockTransport(handler),
) as client:
absolute = yarl.URL("https://example.com/a%2Fb?x=a%2Bb", encoded=True)
assert client.get(absolute).json()["target"] == "/a%2Fb?x=a%2Bb"
result = client.post(yarl.URL("child"), json={"value": 1})
assert result.json() == {"method": "POST", "target": "/api/child", "marker": "preserved"}The inherited high-level request methods use A This is an application adapter rather than native yarl/furl support. Converting at the boundary also makes the handoff between the libraries' URL normalization rules explicit; test encoded-path cases important to your application. References: HTTPX client request construction, yarl URL string representation. AI-assisted answer using OpenAI Codex. Tested with Python 3.11.9, HTTPX 0.28.1, and yarl 1.25.1: encoded slash/plus preservation, absolute and relative URLs, GET/POST, and default header preservation using MockTransport. |
Uh oh!
There was an error while loading. Please reload this page.
I like to use yarl for storing and manipulating URLs. The
requestslibrary used to let me use these directly, buthttpxdoes not:I'm moving a largish codebase over from
requeststohttpx, and having to wrap my URLs instr(url)in many places is painful, and I'm not sure it should be necessary.(I used to use furl rather than
yarl- the same issue exists with that.)All reactions