Skip to content

feat(java): add algolia user agent APIC-338 #347

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

Merged
merged 6 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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;
Copy link
Member

Choose a reason for hiding this comment

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

non blocking: I did not saw other one liners in this codebase, we might want to stick to curly brackets for consistency

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
11 changes: 11 additions & 0 deletions website/docs/addNewLanguage.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ 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, 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`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe an example of output could help

Copy link
Member

Choose a reason for hiding this comment

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

and the link to this PR


### Requesters

> TODO: informations
Expand Down