Skip to content

Commit

Permalink
feat(templates): rename userAgent to algoliaAgent (#508)
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
  • Loading branch information
Haroenv and shortcuts authored May 18, 2022
1 parent e400a96 commit 0389b9d
Show file tree
Hide file tree
Showing 40 changed files with 141 additions and 132 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ jobs:
with:
type: minimal

- name: Download JavaScript clients
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
uses: ./.github/actions/restore-artifacts
with:
javascript: true
type: all

- name: Build 'algoliasearch' client
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
run: yarn cli build clients javascript algoliasearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.LinkedHashSet;
import java.util.Set;

public class UserAgent {
public class AlgoliaAgent {

private final Set<String> segments;

private String finalValue;

public UserAgent(String clientVersion) {
public AlgoliaAgent(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")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function algoliasearch(
write: 30,
},
requester: options?.requester ?? createXhrRequester(),
userAgents: [{ segment: 'Browser' }],
algoliaAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
responsesCache: createMemoryCache(),
requestsCache: createMemoryCache({ serializable: false }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function algoliasearch(
write: 30,
},
requester: options?.requester ?? createHttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
requestsCache: createNullCache(),
hostsCache: createMemoryCache(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export * from './src/createAuth';
export * from './src/createEchoRequester';
export * from './src/cache';
export * from './src/transporter';
export * from './src/createUserAgent';
export * from './src/getUserAgent';
export * from './src/createAlgoliaAgent';
export * from './src/getAlgoliaAgent';
export * from './src/types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { AlgoliaAgentOptions, AlgoliaAgent } from './types';

export function createAlgoliaAgent(version: string): AlgoliaAgent {
const algoliaAgent = {
value: `Algolia for JavaScript (${version})`,
add(options: AlgoliaAgentOptions): AlgoliaAgent {
const addedAlgoliaAgent = `; ${options.segment}${
options.version !== undefined ? ` (${options.version})` : ''
}`;

if (algoliaAgent.value.indexOf(addedAlgoliaAgent) === -1) {
algoliaAgent.value = `${algoliaAgent.value}${addedAlgoliaAgent}`;
}

return algoliaAgent;
},
};

return algoliaAgent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EchoResponse, EndRequest, Request, Response } from './types';

export type UrlParams = {
host: string;
userAgent: string;
algoliaAgent: string;
searchParams: EchoResponse['searchParams'];
};

Expand All @@ -20,7 +20,7 @@ export function createEchoRequester({
{ headers, url, connectTimeout, responseTimeout }: EndRequest,
{ data, ...originalRequest }: Request
): Promise<Response> {
const { host, searchParams, userAgent } = getUrlParams(url);
const { host, searchParams, algoliaAgent } = getUrlParams(url);
const originalData =
data && Object.entries(data).length > 0 ? data : undefined;

Expand All @@ -31,7 +31,7 @@ export function createEchoRequester({
headers,
connectTimeout,
responseTimeout,
userAgent: userAgent ? encodeURI(userAgent) : undefined,
algoliaAgent: algoliaAgent ? encodeURI(algoliaAgent) : undefined,
searchParams,
data: originalData,
}),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createAlgoliaAgent } from './createAlgoliaAgent';
import type { AlgoliaAgentOptions, AlgoliaAgent } from './types';

export type GetAlgoliaAgent = {
algoliaAgents: AlgoliaAgentOptions[];
client: string;
version: string;
};

export function getAlgoliaAgent({
algoliaAgents,
client,
version,
}: GetAlgoliaAgent): AlgoliaAgent {
const defaultAlgoliaAgent = createAlgoliaAgent(version).add({
segment: client,
version,
});

algoliaAgents.forEach((algoliaAgent) =>
defaultAlgoliaAgent.add(algoliaAgent)
);

return defaultAlgoliaAgent;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function createTransporter({
hostsCache,
baseHeaders,
baseQueryParameters,
userAgent,
algoliaAgent,
timeouts,
requester,
requestsCache,
Expand Down Expand Up @@ -107,7 +107,7 @@ export function createTransporter({
: {};

const queryParameters: QueryParameters = {
'x-algolia-agent': userAgent.value,
'x-algolia-agent': algoliaAgent.value,
...baseQueryParameters,
...dataQueryParameters,
};
Expand Down Expand Up @@ -355,7 +355,7 @@ export function createTransporter({
hostsCache,
requester,
timeouts,
userAgent,
algoliaAgent,
baseHeaders,
baseQueryParameters,
hosts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Host } from './Host';
import type { Requester } from './Requester';
import type {
Timeouts,
UserAgentOptions,
AlgoliaAgentOptions,
TransporterOptions,
} from './Transporter';

Expand All @@ -17,7 +17,7 @@ export type CreateClientOptions = Pick<
apiKey: string;
requester: Requester;
timeouts: Timeouts;
userAgents: UserAgentOptions[];
algoliaAgents: AlgoliaAgentOptions[];
hosts?: Host[];
authMode?: AuthMode;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export type EchoResponse = Request & {
headers: Headers;
responseTimeout: number;
searchParams?: Record<string, string>;
userAgent?: string;
algoliaAgent?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type StackFrame = {
triesLeft: number;
};

export type UserAgentOptions = {
export type AlgoliaAgentOptions = {
/**
* The segment. Usually the integration name.
*/
Expand All @@ -57,7 +57,7 @@ export type UserAgentOptions = {
version?: string;
};

export type UserAgent = {
export type AlgoliaAgent = {
/**
* The raw value of the user agent.
*/
Expand All @@ -66,7 +66,7 @@ export type UserAgent = {
/**
* Mutates the current user agent ading the given user agent options.
*/
add: (options: UserAgentOptions) => UserAgent;
add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
};

export type Timeouts = {
Expand Down Expand Up @@ -133,7 +133,7 @@ export type TransporterOptions = {
/**
* The user agent used. Sent on query parameters.
*/
userAgent: UserAgent;
algoliaAgent: AlgoliaAgent;
};

export type Transporter = {
Expand Down Expand Up @@ -175,7 +175,7 @@ export type Transporter = {
/**
* The user agent used. Sent on query parameters.
*/
userAgent: UserAgent;
algoliaAgent: AlgoliaAgent;

/**
* The headers used on each request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { UrlParams } from '@experimental-api-clients-automation/client-comm

function getUrlParams(url: string): UrlParams {
const { host, searchParams: urlSearchParams } = new URL(url);
const userAgent = urlSearchParams.get('x-algolia-agent') || '';
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
const searchParams = {};

for (const [k, v] of urlSearchParams) {
Expand All @@ -16,7 +16,7 @@ function getUrlParams(url: string): UrlParams {

return {
host,
userAgent,
algoliaAgent,
searchParams:
Object.entries(searchParams).length === 0 ? undefined : searchParams,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { UrlParams } from '@experimental-api-clients-automation/client-comm

function getUrlParams(url: string): UrlParams {
const { host, searchParams: urlSearchParams } = new URL(url);
const userAgent = urlSearchParams.get('x-algolia-agent') || '';
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
const searchParams = {};

for (const [k, v] of urlSearchParams) {
Expand All @@ -18,7 +18,7 @@ function getUrlParams(url: string): UrlParams {

return {
host,
userAgent,
algoliaAgent,
searchParams:
Object.entries(searchParams).length === 0 ? undefined : searchParams,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Algolia\AlgoliaSearch\RequestOptions;

use Algolia\AlgoliaSearch\Configuration\Configuration;
use Algolia\AlgoliaSearch\Support\UserAgent;
use Algolia\AlgoliaSearch\Support\AlgoliaAgent;

final class RequestOptionsFactory
{
Expand Down Expand Up @@ -50,9 +50,9 @@ private function normalize($options)
'headers' => [
'X-Algolia-Application-Id' => $this->config->getAppId(),
'X-Algolia-API-Key' => $this->config->getAlgoliaApiKey(),
'User-Agent' => $this->config->getUserAgent() !== null
? $this->config->getUserAgent()
: UserAgent::get(),
'User-Agent' => $this->config->getAlgoliaAgent() !== null
? $this->config->getAlgoliaAgent()
: AlgoliaAgent::get(),
'Content-Type' => 'application/json',
],
'queryParameters' => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Algolia\AlgoliaSearch\Algolia;

final class UserAgent
final class AlgoliaAgent
{
private static $value;

Expand All @@ -19,7 +19,7 @@ public static function get()
return self::$value;
}

public static function addCustomUserAgent($segment, $version)
public static function addCustomAlgoliaAgent($segment, $version)
{
self::$value = null;
self::$customSegments[trim($segment, ' ')] = trim($version, ' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void setDefaultGeneratorOptions() {

additionalProperties.put("apiName", apiName);
additionalProperties.put("capitalizedApiName", Utils.capitalize(apiName));
additionalProperties.put("userAgent", Utils.capitalize(CLIENT));
additionalProperties.put("algoliaAgent", Utils.capitalize(CLIENT));
additionalProperties.put("gitRepoId", "algoliasearch-client-javascript");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.algolia.exceptions.AlgoliaRuntimeException;
import com.algolia.model.insights.*;
import com.algolia.api.InsightsClient;
import com.algolia.utils.UserAgent;
import com.algolia.utils.AlgoliaAgent;
import io.github.cdimascio.dotenv.Dotenv;

public class Insights {
Expand All @@ -15,10 +15,10 @@ public static void main(String[] args) {
InsightsClient client = new InsightsClient(
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"),
new AlgoliaAgent.Segment[] {
new AlgoliaAgent.Segment("test", "8.0.0"),
new AlgoliaAgent.Segment("JVM", "11.0.14"),
new AlgoliaAgent.Segment("no version"),
}
);

Expand Down
Loading

0 comments on commit 0389b9d

Please sign in to comment.