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

Add support for custom HTTP headers in API calls #35

Merged
merged 2 commits into from
Jun 16, 2017

Conversation

andereld
Copy link
Contributor

This commit adds a map of custom HTTP headers to UnleashConfig, and
includes these headers in calls to the Unleash server. This may be used
to, e.g., add an Authorization field if the server is behind a wrapper
or proxy which requires authentication.

See Unleash/unleash#222 on GitHub for background discussion.

This commit adds a map of custom HTTP headers to UnleashConfig, and
includes these headers in calls to the Unleash server. This may be used
to, e.g., add an `Authorization` field if the server is behind a wrapper
or proxy which requires authentication.

See Unleash/unleash#222 on GitHub for background discussion.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 95.335% when pulling 1b2c340 on andereld:feature/custom-http-headers into 3b71181 on Unleash:master.

@@ -88,8 +101,17 @@ public UnleashContextProvider getContextProvider() {
return contextProvider;
}

public static void setRequestProperties(HttpURLConnection connection, UnleashConfig config) {
connection.setRequestProperty(UNLEASH_APP_NAME_HEADER, config.getAppName());
connection.setRequestProperty(UNLEASH_INSTANCE_ID_HEADER, config.getInstanceId());
Copy link

Choose a reason for hiding this comment

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

Can this set User-Agent as well? Since this is getting a small refactor either way 🙂

Ref Unleash/unleash-client-node#39

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I didn't think to add it as there was no User-Agent in the code to begin with, but I'll add it now. Based on the linked commit, I take it the User-Agent should be the app name.

public static void setRequestProperties(HttpURLConnection connection, UnleashConfig config) {
connection.setRequestProperty(UNLEASH_APP_NAME_HEADER, config.getAppName());
connection.setRequestProperty(UNLEASH_INSTANCE_ID_HEADER, config.getInstanceId());
for (String name : config.getCustomHttpHeaders().keySet()) {
Copy link

Choose a reason for hiding this comment

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

Is this module java 8? If so, can use config.getCustomHttpHeaders().keySet().forEach. If not java 8, ignore!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maven-compiler-plugin in the pom.xml specifies a source and target of 1.8, so I guess that makes the module Java 8. I'll change it to forEach, that is nicer – thanks!


import no.finn.unleash.UnleashContextProvider;

public class UnleashConfig {
protected static final String UNLEASH_APP_NAME_HEADER = "UNLEASH-APPNAME";
Copy link

Choose a reason for hiding this comment

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

can be package local instead of protected, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'll change it, thanks.

Following code review, this commit

- adds a `User-Agent` field to the request properties set by
  UnleashConfig.setRequestProperties
- changes two variables from 'protected' to package-private scope
- replaces a for loop with a Java 8 forEach
@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 95.344% when pulling 35e3c60 on andereld:feature/custom-http-headers into 3b71181 on Unleash:master.

@ivarconr
Copy link
Member

Nice job, even got tests! Thanks =)

@ivarconr
Copy link
Member

Will release this next week (traveling this weekend, and do not have correct keys set up on my laptop).

@andereld andereld deleted the feature/custom-http-headers branch June 18, 2017 18:43
@ivarconr
Copy link
Member

Released as 2.1.2!

@andereld
Copy link
Contributor Author

Nice, thanks!

config.getCustomHttpHeaders().forEach(connection::setRequestProperty);

😄

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.

None yet

4 participants