reqwest::RequestBuilder is probably a misnomer #2377
JohnScience
started this conversation in
Ideas
Replies: 2 comments
-
Original issue: #2375. Other problems caused by this pattern: |
Beta Was this translation helpful? Give feedback.
0 replies
-
As a workaround, one can introduce a new abstraction, e.g. type RequestBuilderProvider = Box<dyn FnOnce(reqwest::Client) -> reqwest::RequestBuilder>; Now any Request-producing function can return a However this approach doesn't look viable as a long-term solution because it creates an unnecessary abstraction. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
TL;DR
reqwest::RequestBuilder
shouldn't depend on the client.Full text:
Let's say we have a function that wants to prepare "requests" for execution.
When I say "requests", I don't meanreqwest::Request
because it'd require a breaking change, which is undesirable.These "requests" can be either regularreqwest::Request
orreqwest::Request
with extra data, such as JSON body, that needs to be sent along with thereqwest::Request
.These "requests" can be either regular
reqwest::Request
created withreqwest::Request::new
(and not modified further) orreqwest::Request
with extra data, such as JSON body.Currently, the only way to do something like this is to usereqwest::RequestBuilder
.While it is also possible to manually add necessary data to
reqwest::Request
, it is usually not as convenient as with API provided byreqwest::RequestBuilder
.On the surface, everything seems fine. However, you can notice that something is fishy when you realize that there's no way to create
reqwest::RequestBuilder
without supplyingreqwest::Client
in one way or another.Indeed, for some reason, reqwest::RequestBuilder stores owned
reqwest::Client
.While
reqwest::Client
is presumably cheaply cloneable, several questions naturally arise.Let's try answering them one by one.
reqwest::Client
is non-essential for the primary thing that people would want from a type calledRequestBuilder
(building requests). Moreover, the next answer will highlight the conflict of the non-essential and essential aspects of this type.reqwest::RequestBuilder
to containreqwest::Client
now creates an abstraction leak where the user code now is required to also supplyreqwest::Client
- either as an argument or as a local variable. Other than the implications of the design ofreqwest::RequestBuilder
, there are no reasons for the function preparing a request to know which client will execute its request.reqwest::RequestBuilder
is already taken. In the short term, such a type can be calledClientlessRequestBuilder
. And it can have.send_via(client)
method instead of.send()
. Oncereqwest
is ready to make breaking changes,reqwest::RequestBuilder
can be redefined toClientlessRequestBuilder
and its documentation can provide a note on the legacy misnomer. Alternatively, convenience methods can be added toreqwest::Request
.Beta Was this translation helpful? Give feedback.
All reactions