Skip to content

Commit

Permalink
fix(java): use addAlgoliaAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jun 8, 2022
1 parent 078ea29 commit 0496511
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ public abstract class ApiClient {

private boolean debugging = false;
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private AlgoliaAgent algoliaAgent;

private String contentType;

private Requester requester;

/*
* Constructor for ApiClient with custom Requester
*/
public ApiClient(String appId, String apiKey, String clientName, ClientOptions options) {
public ApiClient(String appId, String apiKey, String clientName, String version, ClientOptions options) {
if (appId == null || appId.length() == 0) {
throw new AlgoliaRuntimeException("`appId` is missing.");
}
Expand All @@ -37,34 +35,51 @@ public ApiClient(String appId, String apiKey, String clientName, ClientOptions o

this.contentType = "application/json";

AlgoliaAgent ua = new AlgoliaAgent("4.0.0-SNAPSHOT");
ua.addSegment(new AlgoliaAgent.Segment(clientName, "4.0.0-SNAPSHOT"));
this.algoliaAgent = new AlgoliaAgent(version);
this.algoliaAgent.addSegment(new AlgoliaAgent.Segment(clientName, version));
if (options.getAlgoliaAgentSegments() != null) {
for (AlgoliaAgent.Segment segment : options.getAlgoliaAgentSegments()) {
ua.addSegment(segment);
this.algoliaAgent.addSegment(segment);
}
}
setAlgoliaAgent(ua.toString());
resetUserAgent();

defaultHeaderMap.put("X-Algolia-Application-Id", appId);
defaultHeaderMap.put("X-Algolia-API-Key", apiKey);
defaultHeaderMap.put("Accept", this.contentType);
defaultHeaderMap.put("Content-Type", this.contentType);
addDefaultHeader("X-Algolia-Application-Id", appId);
addDefaultHeader("X-Algolia-API-Key", apiKey);
addDefaultHeader("Accept", this.contentType);
addDefaultHeader("Content-Type", this.contentType);

this.requester = options.getRequester();
if (this.requester == null) {
this.requester = new HttpRequester();
}
}

private void resetUserAgent() {
addDefaultHeader("User-Agent", this.algoliaAgent.toString());
}

/**
* Add a custom user agent segment
*
* @param segment Algolia Agent Segment
* @return ApiClient
*/
public ApiClient addAlgoliaAgent(AlgoliaAgent.Segment segment) {
algoliaAgent.addSegment(segment);
resetUserAgent();
return this;
}

/**
* Set the User-Agent header's value (by adding to the default header map).
* Remove a user agent segment
*
* @param algoliaAgent HTTP request's user agent
* @param segment Algolia Agent Segment
* @return ApiClient
*/
public ApiClient setAlgoliaAgent(String algoliaAgent) {
addDefaultHeader("User-Agent", algoliaAgent);
public ApiClient removeAlgoliaAgent(AlgoliaAgent.Segment segment) {
algoliaAgent.removeSegment(segment);
resetUserAgent();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public String addSegment(Segment seg) {
return finalValue;
}

public boolean removeSegment(Segment seg) {
return segments.remove(seg.toString());
}

@Override
public String toString() {
return finalValue;
Expand Down
1 change: 0 additions & 1 deletion config/generation.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = {
// Java
'!clients/algoliasearch-client-java-2/**',
'clients/algoliasearch-client-java-2/gradle.properties',
'clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/ApiClient.java',
'clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/api/**',
'clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/model/**',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void processOpts() {
file.getTemplateFile().equals("build.gradle.mustache") ||
file.getTemplateFile().equals("settings.gradle.mustache") ||
file.getTemplateFile().equals("gitignore.mustache") ||
file.getTemplateFile().equals("ApiClient.mustache") ||
file.getTemplateFile().equals("ApiCallback.mustache") ||
file.getTemplateFile().equals("ApiResponse.mustache") ||
file.getTemplateFile().equals("JSON.mustache") ||
Expand Down
Loading

0 comments on commit 0496511

Please sign in to comment.