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

Move verb+body validation into an isolated Channel #292

Closed
wants to merge 1 commit into from

Conversation

carterkozak
Copy link
Contributor

Fix: channel.execute must not throw, a future should be returned.
Follow-up: implement an outer channel to catch RuntimeExceptions
and Errors thrown and convert them into immediate failed futures.

Fix: channel.execute must not throw, a future should be returned.
Follow-up: implement an outer channel to catch RuntimeExceptions
and Errors thrown and convert them into immediate failed futures.
@carterkozak
Copy link
Contributor Author

WIP: Needs its own tests, AbstractChannelTest.*failsWhenBodyIsGiven should be removed as we don't need to implement this validation in every client.

@uschi2000
Copy link
Contributor

ACK on the future-vs-throw change.
I find the version with a verifying channel harder to read. Is this in preparation for another channel implementation?

@carterkozak
Copy link
Contributor Author

Is this in preparation for another channel implementation?

I started hacking on channel implementations for a couple other http clients for a direct comparison. This makes it easier to write a new client by reducing the required code duplication.

@carterkozak
Copy link
Contributor Author

#289 has a similar motivation since not all clients support content compression by default.

@@ -84,7 +84,6 @@ public static HttpChannel of(HttpClient client, URL baseUrl, Duration requestTim
// Fill request body and set HTTP method
switch (endpoint.httpMethod()) {
case GET:
Preconditions.checkArgument(!request.body().isPresent(), "GET endpoints must not have a request body");
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to rfink's suggestion (co-locating the GET related validation with the actual mention of GET in this switch seems nice), could we just pop a line in here saying:

if (request.body().isPresent()) {
  return Futures.immediateFailedFuture(
                    new SafeIllegalArgumentException("GET endpoint must not have a request body"));
}

I know java's control flow with async stuff is a bit crap (would love rust's ? operator here), but maybe the verbosity is fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This means every client must re-implement the same validation, I’d like to generalize as much as possible to avoid similar issues to our okhttp mess. happy to leave both in place here, but I think the generalized handler makes it easier to build additional client implementations with equivalent semantics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To rephrase; I don’t think this validation is a concern of client implementations, but a general dialogue concept.

Copy link
Contributor

Choose a reason for hiding this comment

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

As an alternative, could we consider stronger types that avoid optional fields where they don't make sense semantically?

Copy link
Contributor

Choose a reason for hiding this comment

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

as long as we don't have to include derive4j ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Always in favor of moving validation to the type system :-)
That would be a larger break than the proposed request change, is that something we’re amenable to?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's now or never.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack, before we change this I'd like to put some thought into the request body API. I put together quick netty client implementation (very, very naive at this point), but it exposes some api limitations that are worth considering before we lock things down.

@carterkozak
Copy link
Contributor Author

Closing, DELETE validation is unnecessary because we can send bodies.

@iamdanfox iamdanfox deleted the ckozak/body_validation branch May 12, 2020 21:06
@iamdanfox iamdanfox restored the ckozak/body_validation branch May 12, 2020 21:06
@iamdanfox iamdanfox deleted the ckozak/body_validation branch May 12, 2020 21:06
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.

3 participants