Skip to content

Commit 97c1aaf

Browse files
authored
feat(java): add algolia user agent APIC-338 (#347)
1 parent 6f17c84 commit 97c1aaf

File tree

6 files changed

+114
-17
lines changed

6 files changed

+114
-17
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.algolia.utils;
2+
3+
import java.util.LinkedHashSet;
4+
import java.util.Set;
5+
6+
public class UserAgent {
7+
8+
private final Set<String> segments;
9+
10+
private String finalValue;
11+
12+
public UserAgent(String clientVersion) {
13+
this.finalValue = String.format("Algolia for Java (%s)", clientVersion);
14+
this.segments = new LinkedHashSet<String>();
15+
this.addSegment(new Segment("JVM", System.getProperty("java.version")));
16+
}
17+
18+
public String addSegment(Segment seg) {
19+
String segment = seg.toString();
20+
if (segments.contains(segment)) {
21+
return finalValue;
22+
}
23+
segments.add(segment);
24+
finalValue += segment;
25+
return finalValue;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return finalValue;
31+
}
32+
33+
public static class Segment {
34+
35+
private final String value;
36+
private final String version;
37+
38+
public Segment(String value) {
39+
this(value, null);
40+
}
41+
42+
public Segment(String value, String version) {
43+
this.value = value;
44+
this.version = version;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
StringBuilder sb = new StringBuilder();
50+
sb.append("; ").append(value);
51+
if (version != null) {
52+
sb.append(" (").append(version).append(")");
53+
}
54+
return sb.toString();
55+
}
56+
}
57+
}

clients/algoliasearch-client-java-2/algoliasearch-core/com/algolia/utils/retry/RetryStrategy.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ public Response intercept(Chain chain) throws IOException {
5151
);
5252

5353
try {
54-
System.out.println(
55-
"MAKING REQUEST TO " +
56-
newUrl +
57-
" try: " +
58-
currentHost.getRetryCount()
59-
);
6054
Response response = chain.proceed(request);
6155
currentHost.setLastUse(Utils.nowUTC());
6256
// no timeout

playground/java/src/main/java/com/algolia/playground/App.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
import com.algolia.exceptions.AlgoliaRuntimeException;
66
import com.algolia.model.search.*;
77
import com.algolia.search.SearchApi;
8+
import com.algolia.utils.UserAgent;
89

910
import io.github.cdimascio.dotenv.Dotenv;
1011

1112
public class App {
1213
public static void main(String[] args) {
1314
Dotenv dotenv = Dotenv.configure().directory("../").load();
1415

15-
SearchApi client = new SearchApi(dotenv.get("ALGOLIA_APPLICATION_ID"), dotenv.get("ALGOLIA_SEARCH_KEY"));
16+
SearchApi client = new SearchApi(dotenv.get("ALGOLIA_APPLICATION_ID"), dotenv.get("ALGOLIA_SEARCH_KEY"),
17+
new UserAgent.Segment[] {
18+
new UserAgent.Segment("test", "8.0.0"),
19+
new UserAgent.Segment("JVM", "11.0.14"),
20+
new UserAgent.Segment("no version")
21+
});
1622

1723
String indexName = dotenv.get("SEARCH_INDEX");
1824
SearchParamsObject params = new SearchParamsObject();

templates/java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package {{invokerPackage}};
22

33
import com.algolia.utils.Requester;
44
import com.algolia.exceptions.*;
5+
import com.algolia.utils.UserAgent;
56

67
import okhttp3.*;
78
import okhttp3.internal.http.HttpMethod;
@@ -37,12 +38,19 @@ public class ApiClient {
3738
/*
3839
* Constructor for ApiClient with custom Requester
3940
*/
40-
public ApiClient(String appId, String apiKey, Requester requester) {
41-
setUserAgent("{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}/java{{/httpUserAgent}}");
41+
public ApiClient(String appId, String apiKey, Requester requester, String clientName, UserAgent.Segment[] segments) {
42+
UserAgent ua = new UserAgent("{{packageVersion}}");
43+
ua.addSegment(new UserAgent.Segment(clientName, "{{packageVersion}}"));
44+
if(segments != null) {
45+
for(UserAgent.Segment segment : segments) {
46+
ua.addSegment(segment);
47+
}
48+
}
49+
setUserAgent(ua.toString());
4250

43-
this.appId = appId;
44-
this.apiKey = apiKey;
45-
this.requester = requester;
51+
this.appId = appId;
52+
this.apiKey = apiKey;
53+
this.requester = requester;
4654
}
4755

4856
public DateFormat getDateFormat() {

templates/java/libraries/okhttp-gson/api.mustache

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,35 @@ public class {{classname}} extends ApiClient {
3535
{{#hasRegionalHost}}
3636
{{#fallbackToAliasHost}}
3737
public {{classname}}(String appId, String apiKey) {
38-
super(appId, apiKey, new HttpRequester(getDefaultHosts(".")));
38+
this(appId, apiKey, new HttpRequester(getDefaultHosts(".")), null);
3939
}
4040

4141
{{/fallbackToAliasHost}}
4242
public {{classname}}(String appId, String apiKey, String region) {
43-
super(appId, apiKey, new HttpRequester(getDefaultHosts(region)));
43+
this(appId, apiKey, new HttpRequester(getDefaultHosts(region)), null);
44+
}
45+
46+
public {{classname}}(String appId, String apiKey, String region, UserAgent.Segment[] userAgentSegments) {
47+
this(appId, apiKey, new HttpRequester(getDefaultHosts(region)), userAgentSegments);
4448
}
4549
{{/hasRegionalHost}}
4650

4751
{{^hasRegionalHost}}
4852
public {{classname}}(String appId, String apiKey) {
49-
super(appId, apiKey, new HttpRequester(getDefaultHosts(appId)));
53+
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), null);
54+
}
55+
56+
public {{classname}}(String appId, String apiKey, UserAgent.Segment[] userAgentSegments) {
57+
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), userAgentSegments);
5058
}
5159
{{/hasRegionalHost}}
52-
60+
5361
public {{classname}}(String appId, String apiKey, Requester requester) {
54-
super(appId, apiKey, requester);
62+
this(appId, apiKey, requester, null);
63+
}
64+
65+
public {{classname}}(String appId, String apiKey, Requester requester, UserAgent.Segment[] userAgentSegments) {
66+
super(appId, apiKey, requester, "{{{baseName}}}", userAgentSegments);
5567
}
5668

5769
{{^hasRegionalHost}}{{^experimentalHost}}

website/docs/addNewLanguage.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ Some Algolia clients (search and recommend) targets the default appId host (`${a
6868

6969
As the generator does not support reading `servers` in a spec file, hosts methods and variables are extracted with a custom script and create variables for you to use in the mustache templates, [read more here](/docs/addNewClient#generators).
7070

71+
### User Agent
72+
73+
The header 'User-Agent' must respect a strict pattern of a base, client, plus additional user defined segments:
74+
base: `Algolia for <language> (<apiVersion>)`
75+
client: `; <clientName> (<clientVersion>)`
76+
segment: `; <Description> ([version])`
77+
78+
The version is optional for segments.
79+
80+
The resulting User Agent is the concatenation of `base`, `client`, and all the `segments`.
81+
82+
For example, if we have:
83+
base: `Algolia for Java (5.0.0)`
84+
client: `; Search (5.0.0)`
85+
segment: `; JVM (11.0.14); experimental`
86+
87+
Then the resulting User Agent is `Algolia for Java (5.0.0); Search (5.0.0); JVM (11.0.14); experimental`.
88+
89+
You can take a look at the Java implementation [here](https://github.com/algolia/api-clients-automation/pull/347).
90+
7191
### Requesters
7292

7393
> TODO: informations

0 commit comments

Comments
 (0)