Skip to content

Add some useful API methods #1295

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

Closed

Conversation

Spikhalskiy
Copy link
Contributor

Motivation and proposals:

  1. Method setHeaders(Map<String, Collection<String>> headers) is general, but in most cases applications use headers with only one value, so it's good to have a simple method to set them without converting Map<String, String> to Map<String, Collections.singletonList<String>>.
  2. Now if you already have Cookies in some entities from another API's it's a bit annoying to convert them to org.asynchttpclient.Cookie manually. Expose two helper factory methods set for Netty's and standard javax.servlet entities.

These methods were implemented as static utils for async-http-client in my project, I think it would be handy to have them out of the box.

… and set headers method for headers with single values
@slandelle
Copy link
Contributor

slandelle commented Oct 31, 2016

Hi,

There's too many different things in this PR. It would be better to isolate them.

I'm OK with the first one (once again, I'm so sad/disappointed by Java's type erasure...). See comments.

Regarding the second one, I'm not fond of it depending on the Servlet API.
It would introduce a required additional dependency to the Servlet API for very limited value.
I'm fine with the io.netty.handler.codec.http.cookie.Cookie bridge though, as it ships with Netty. There's also a chance that I'll drop AHC class for the Netty one.

I would be OK with isolated bridges that only require an optional dependency, though.

import org.slf4j.LoggerFactory;
import static org.asynchttpclient.util.HttpUtils.parseCharset;
import static org.asynchttpclient.util.HttpUtils.validateSupportedScheme;
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert imports ordering: unrelated.

import static org.asynchttpclient.cookie.CookieUtil.*;
import static org.asynchttpclient.cookie.CookieUtil.validateCookieAttribute;
import static org.asynchttpclient.cookie.CookieUtil.validateCookieName;
import static org.asynchttpclient.cookie.CookieUtil.validateCookieValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

please revert

}
return asDerivedType();
}

public T setSingleHeaders(Map<String, String> headers) {
this.headers.clear();
headers.forEach((name, value) -> this.headers.add(name, value));
Copy link
Contributor

Choose a reason for hiding this comment

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

missing null check, like in setHeaders

}
return asDerivedType();
}

public T setSingleHeaders(Map<String, String> headers) {
this.headers.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

add a clearHeaders method?

}
return asDerivedType();
}

public T setSingleHeaders(Map<String, String> headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

missing Javadoc

<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<optional>true</optional>
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure this dep is really optional? I feel like the Cookie class would fail to load and crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, just import doesn't mean anything. Cookie class would be loaded (and fail) if somebody actually would use these new methods.

@Spikhalskiy
Copy link
Contributor Author

Spikhalskiy commented Oct 31, 2016

@slandelle +1 for dropping cookie in favour of Netty's Cookie.

I would be OK with isolated bridges that only require an optional dependency, though.

Current code should be absolutely fine with optional dependency (so, if nobody will add javax.servlet and would not use new conversion methods - there would be no ClassDefNotFound), but I would double check.

@slandelle
Copy link
Contributor

Closing this.
Let's aim at dropping AHC's own Cookie impl for AHC 2.1. I just want to make sure I contributed everything upstream and we don't lose anything in the move.

@slandelle slandelle closed this Nov 1, 2016
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.

2 participants