- Notifications
You must be signed in to change notification settings - Fork 275
Expose user agent and pass header through in requests #32
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
Closed
Changes from 3 commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions 95 java-client/src/main/java/co/elastic/clients/base/UserAgent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
| ||
package co.elastic.clients.base; | ||
| ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
| ||
/** | ||
* Models a user agent, consisting of a name, version, | ||
* and optional key-value metadata. | ||
*/ | ||
public class UserAgent { | ||
| ||
static final String DEFAULT_NAME = "elasticsearch-java"; | ||
| ||
// The client version is loaded from the 'version.properties' file | ||
static final String DEFAULT_VERSION; | ||
static { | ||
InputStream in = UserAgent.class.getResourceAsStream("/co.elastic.clients.elasticsearch/version.properties"); | ||
if (in != null) { | ||
Properties prop = new Properties(); | ||
String version; | ||
try { | ||
prop.load(in); | ||
version = prop.getProperty("version", "?"); | ||
} catch (IOException e) { | ||
// Unable to read properties file | ||
version = "?"; | ||
} | ||
DEFAULT_VERSION = version; | ||
} | ||
else { | ||
// Unable to locate properties file | ||
DEFAULT_VERSION = "?"; | ||
} | ||
// TODO: log error if DEFAULT_VERSION now equals "?" | ||
} | ||
| ||
// Default user agent, constructed from default repo name and version | ||
public static final UserAgent DEFAULT = new UserAgent(DEFAULT_NAME, DEFAULT_VERSION); | ||
| ||
private final String name; | ||
private final String version; | ||
private final Map<String, String> metadata; | ||
| ||
public UserAgent(String name, String version, Map<String, String> metadata) { | ||
this.name = name; | ||
this.version = version; | ||
this.metadata = metadata; | ||
} | ||
| ||
public UserAgent(String repoName, String version) { | ||
this(repoName, version, Collections.emptyMap()); | ||
} | ||
| ||
@Override | ||
public String toString() { | ||
if (metadata.isEmpty()) { | ||
return String.format("%s/%s", name, version); | ||
} | ||
else { | ||
StringBuilder metadataString = new StringBuilder(); | ||
for (Map.Entry<String, String> entry : metadata.entrySet()) { | ||
if (metadataString.length() > 0) { | ||
metadataString.append("; "); | ||
} | ||
metadataString.append(entry.getKey()); | ||
metadataString.append(' '); | ||
metadataString.append(entry.getValue()); | ||
} | ||
return String.format("%s/%s (%s)", name, version, metadataString); | ||
} | ||
} | ||
| ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions 77 java-client/src/test/java/co/elastic/clients/base/UserAgentTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
| ||
package co.elastic.clients.base; | ||
| ||
import co.elastic.clients.base.rest_client.RestClientTransport; | ||
import org.apache.http.HttpHost; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestClient; | ||
import org.junit.Test; | ||
| ||
import static co.elastic.clients.base.UserAgent.DEFAULT_NAME; | ||
import static co.elastic.clients.base.UserAgent.DEFAULT_VERSION; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
| ||
public class UserAgentTest { | ||
| ||
private static final String DEFAULT_USER_AGENT = String.format("%s/%s", DEFAULT_NAME, DEFAULT_VERSION); | ||
| ||
private static final String CUSTOM_NAME = "ÜberClient"; | ||
private static final String CUSTOM_VERSION = "1.0.13"; | ||
private static final String CUSTOM_USER_AGENT = String.format("%s/%s", CUSTOM_NAME, CUSTOM_VERSION); | ||
| ||
private static final String CUSTOM_NAME_2 = "MegaClient"; | ||
private static final String CUSTOM_VERSION_2 = "6.7.8"; | ||
| ||
@Test | ||
public void testDefaultUserAgent() throws Exception { | ||
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); | ||
Transport transport = new RestClientTransport(restClient, null); | ||
assertEquals(DEFAULT_USER_AGENT, transport.userAgent()); | ||
} | ||
| ||
@Test | ||
public void testCustomUserAgent() throws Exception { | ||
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); | ||
Transport transport = new RestClientTransport(restClient, null, null, | ||
new UserAgent(CUSTOM_NAME, CUSTOM_VERSION)); | ||
assertEquals(CUSTOM_USER_AGENT, transport.userAgent()); | ||
} | ||
| ||
@Test | ||
public void testManualUserAgent() throws Exception { | ||
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); | ||
Transport transport = new RestClientTransport(restClient, null, | ||
RequestOptions.DEFAULT.toBuilder().addHeader("User-Agent", CUSTOM_USER_AGENT).build()); | ||
assertEquals(CUSTOM_USER_AGENT, transport.userAgent()); | ||
} | ||
| ||
@Test | ||
public void testMultipleUserAgentsThrowsException() throws Exception { | ||
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> { | ||
Transport transport = new RestClientTransport(restClient, null, | ||
RequestOptions.DEFAULT.toBuilder().addHeader("User-Agent", CUSTOM_USER_AGENT).build(), | ||
new UserAgent(CUSTOM_NAME_2, CUSTOM_VERSION_2)); | ||
}); | ||
} | ||
| ||
} |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive by nit (hi 👋): IIRC the rest client starts a threadpool with every invocation and is
Closeable
. Maybe start the client only once for the whole test and make sure it is closed after?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi back :)
Yes, that seems like good advice. Given this is my first foray into this code base, I was just about to ping @swallez for his thoughts; I'll add this to the list for discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spinscale WDYT of the latest commit?
There was a problem hiding this comment.
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 mockito as a dependency ;-) - I also opened #33 as a discussion to prevent such issues in the future, but that depends if we want to use junit jupiter in the future I guess