Skip to content

Commit 96dff38

Browse files
committed
feat(java): add algolia user agent
1 parent 34ad2c1 commit 96dff38

File tree

6 files changed

+100
-18
lines changed

6 files changed

+100
-18
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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)) return finalValue;
21+
segments.add(segment);
22+
finalValue += segment;
23+
return finalValue;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return finalValue;
29+
}
30+
31+
public static class Segment {
32+
33+
private final String value;
34+
private final String version;
35+
36+
public Segment(String value) {
37+
this(value, null);
38+
}
39+
40+
public Segment(String value, String version) {
41+
this.value = value;
42+
this.version = version;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
StringBuilder sb = new StringBuilder();
48+
sb.append("; ").append(value);
49+
if (version != null) {
50+
sb.append(" (").append(version).append(")");
51+
}
52+
return sb.toString();
53+
}
54+
}
55+
}

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: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,31 @@ 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-
53-
public {{classname}}(String appId, String apiKey, Requester requester) {
54-
super(appId, apiKey, requester);
60+
61+
public {{classname}}(String appId, String apiKey, Requester requester, UserAgent.Segment[] userAgentSegments) {
62+
super(appId, apiKey, requester, "{{{baseName}}}", userAgentSegments);
5563
}
5664

5765
{{^hasRegionalHost}}{{^experimentalHost}}

website/docs/addNewLanguage.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ 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, 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+
7182
### Requesters
7283

7384
> TODO: informations

0 commit comments

Comments
 (0)