Skip to content

Commit

Permalink
fix(java): use addAlgoliaAgent (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jun 8, 2022
1 parent bec6ee5 commit cbd81a2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 447 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());
refreshUserAgent();

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 refreshUserAgent() {
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);
refreshUserAgent();
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);
refreshUserAgent();
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
13 changes: 10 additions & 3 deletions scripts/ci/husky/pre-commit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
/* eslint-disable no-console */
/* eslint-disable import/no-commonjs */
/* eslint-disable @typescript-eslint/no-var-requires */
const chalk = require('chalk');
Expand Down Expand Up @@ -26,8 +27,15 @@ function getPatterns() {

async function preCommit() {
// when merging, we want to stage all the files
if ((await run('git merge HEAD')) !== 'Already up to date.') {
return;
try {
await run('git merge HEAD');
} catch (e) {
if (e.exitCode === 128) {
console.log(
'Skipping the pre-commit check because a merge is in progress'
);
return;
}
}

const stagedFiles = (
Expand All @@ -37,7 +45,6 @@ async function preCommit() {
const toUnstage = micromatch.match(stagedFiles, getPatterns());

for (const file of toUnstage) {
// eslint-disable-next-line no-console
console.log(
chalk.black.bgYellow('[INFO]'),
`Generated file found, unstaging: ${file}`
Expand Down
Loading

0 comments on commit cbd81a2

Please sign in to comment.