Skip to content

Commit

Permalink
feat(java): add algolia user agent APIC-338 (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Apr 8, 2022
1 parent 6f17c84 commit 97c1aaf
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.algolia.utils;

import java.util.LinkedHashSet;
import java.util.Set;

public class UserAgent {

private final Set<String> segments;

private String finalValue;

public UserAgent(String clientVersion) {
this.finalValue = String.format("Algolia for Java (%s)", clientVersion);
this.segments = new LinkedHashSet<String>();
this.addSegment(new Segment("JVM", System.getProperty("java.version")));
}

public String addSegment(Segment seg) {
String segment = seg.toString();
if (segments.contains(segment)) {
return finalValue;
}
segments.add(segment);
finalValue += segment;
return finalValue;
}

@Override
public String toString() {
return finalValue;
}

public static class Segment {

private final String value;
private final String version;

public Segment(String value) {
this(value, null);
}

public Segment(String value, String version) {
this.value = value;
this.version = version;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("; ").append(value);
if (version != null) {
sb.append(" (").append(version).append(")");
}
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ public Response intercept(Chain chain) throws IOException {
);

try {
System.out.println(
"MAKING REQUEST TO " +
newUrl +
" try: " +
currentHost.getRetryCount()
);
Response response = chain.proceed(request);
currentHost.setLastUse(Utils.nowUTC());
// no timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
import com.algolia.exceptions.AlgoliaRuntimeException;
import com.algolia.model.search.*;
import com.algolia.search.SearchApi;
import com.algolia.utils.UserAgent;

import io.github.cdimascio.dotenv.Dotenv;

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

SearchApi client = new SearchApi(dotenv.get("ALGOLIA_APPLICATION_ID"), dotenv.get("ALGOLIA_SEARCH_KEY"));
SearchApi client = new SearchApi(dotenv.get("ALGOLIA_APPLICATION_ID"), dotenv.get("ALGOLIA_SEARCH_KEY"),
new UserAgent.Segment[] {
new UserAgent.Segment("test", "8.0.0"),
new UserAgent.Segment("JVM", "11.0.14"),
new UserAgent.Segment("no version")
});

String indexName = dotenv.get("SEARCH_INDEX");
SearchParamsObject params = new SearchParamsObject();
Expand Down
18 changes: 13 additions & 5 deletions templates/java/libraries/okhttp-gson/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package {{invokerPackage}};

import com.algolia.utils.Requester;
import com.algolia.exceptions.*;
import com.algolia.utils.UserAgent;

import okhttp3.*;
import okhttp3.internal.http.HttpMethod;
Expand Down Expand Up @@ -37,12 +38,19 @@ public class ApiClient {
/*
* Constructor for ApiClient with custom Requester
*/
public ApiClient(String appId, String apiKey, Requester requester) {
setUserAgent("{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}/java{{/httpUserAgent}}");
public ApiClient(String appId, String apiKey, Requester requester, String clientName, UserAgent.Segment[] segments) {
UserAgent ua = new UserAgent("{{packageVersion}}");
ua.addSegment(new UserAgent.Segment(clientName, "{{packageVersion}}"));
if(segments != null) {
for(UserAgent.Segment segment : segments) {
ua.addSegment(segment);
}
}
setUserAgent(ua.toString());

this.appId = appId;
this.apiKey = apiKey;
this.requester = requester;
this.appId = appId;
this.apiKey = apiKey;
this.requester = requester;
}

public DateFormat getDateFormat() {
Expand Down
22 changes: 17 additions & 5 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,35 @@ public class {{classname}} extends ApiClient {
{{#hasRegionalHost}}
{{#fallbackToAliasHost}}
public {{classname}}(String appId, String apiKey) {
super(appId, apiKey, new HttpRequester(getDefaultHosts(".")));
this(appId, apiKey, new HttpRequester(getDefaultHosts(".")), null);
}

{{/fallbackToAliasHost}}
public {{classname}}(String appId, String apiKey, String region) {
super(appId, apiKey, new HttpRequester(getDefaultHosts(region)));
this(appId, apiKey, new HttpRequester(getDefaultHosts(region)), null);
}

public {{classname}}(String appId, String apiKey, String region, UserAgent.Segment[] userAgentSegments) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(region)), userAgentSegments);
}
{{/hasRegionalHost}}

{{^hasRegionalHost}}
public {{classname}}(String appId, String apiKey) {
super(appId, apiKey, new HttpRequester(getDefaultHosts(appId)));
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), null);
}

public {{classname}}(String appId, String apiKey, UserAgent.Segment[] userAgentSegments) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), userAgentSegments);
}
{{/hasRegionalHost}}

public {{classname}}(String appId, String apiKey, Requester requester) {
super(appId, apiKey, requester);
this(appId, apiKey, requester, null);
}

public {{classname}}(String appId, String apiKey, Requester requester, UserAgent.Segment[] userAgentSegments) {
super(appId, apiKey, requester, "{{{baseName}}}", userAgentSegments);
}

{{^hasRegionalHost}}{{^experimentalHost}}
Expand Down
20 changes: 20 additions & 0 deletions website/docs/addNewLanguage.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ Some Algolia clients (search and recommend) targets the default appId host (`${a

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).

### User Agent

The header 'User-Agent' must respect a strict pattern of a base, client, plus additional user defined segments:
base: `Algolia for <language> (<apiVersion>)`
client: `; <clientName> (<clientVersion>)`
segment: `; <Description> ([version])`

The version is optional for segments.

The resulting User Agent is the concatenation of `base`, `client`, and all the `segments`.

For example, if we have:
base: `Algolia for Java (5.0.0)`
client: `; Search (5.0.0)`
segment: `; JVM (11.0.14); experimental`

Then the resulting User Agent is `Algolia for Java (5.0.0); Search (5.0.0); JVM (11.0.14); experimental`.

You can take a look at the Java implementation [here](https://github.com/algolia/api-clients-automation/pull/347).

### Requesters

> TODO: informations
Expand Down

0 comments on commit 97c1aaf

Please sign in to comment.