Skip to content

feat(spec): add Predict client #129

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 30 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7289b7d
Fetch user profile endpoint
Jan 31, 2022
8bf2435
Fix errors
Feb 1, 2022
8eb49db
Add Javascript client
Feb 1, 2022
b5f01ca
Update configuration
Feb 1, 2022
5cb4425
Add JavaScript API client
Feb 1, 2022
4f779f1
Fetch user profile endpoint
Jan 31, 2022
0a65382
Fix errors
Feb 1, 2022
e082c8b
Add Javascript client
Feb 1, 2022
ba67c5b
Add JavaScript API client
Feb 1, 2022
04f2b3c
Merge branch 'feat/predict-pilot' of github.com:algolia/api-clients-a…
Feb 8, 2022
217c0c1
Fix configuration
Feb 8, 2022
68f60b3
Remove old files
Feb 8, 2022
2e667ab
Remove long descriptions
Feb 8, 2022
3bbf2a0
Clean up old files, regenerate client
Feb 8, 2022
c3c4e5f
Disable enum validation
Feb 8, 2022
be5f35e
Update request body to remove InlineObject name
Feb 9, 2022
5e9b9f8
Remove old file
Feb 9, 2022
f4b2533
Update requestBody name
Feb 9, 2022
f498f0a
Add 400 error
Feb 9, 2022
a1dfb13
Add example
Feb 9, 2022
36e5a1c
Update example and config files
Feb 9, 2022
8214c19
Merge branch 'main' of github.com:algolia/api-clients-automation into…
Feb 10, 2022
0be4c32
Remove duplicate error spec, formatting
Feb 10, 2022
2b5dc90
Remove git push file
Feb 10, 2022
c0a5cb5
Fix error base path
Feb 10, 2022
3278c85
Remove extra empty lines
Feb 10, 2022
ba8af71
Merge branch 'main' into feat/predict-pilot
shortcuts Feb 11, 2022
9ccd6d0
regenerate and fix host edge case
shortcuts Feb 11, 2022
1861535
Add response titles, regenerate client
Feb 11, 2022
9a12538
update clients/README.md
shortcuts Feb 11, 2022
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
7 changes: 7 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ runs:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/packages/client-sources/dist
key: ${{ runner.os }}-1-js-client-sources-${{ hashFiles('clients/algoliasearch-client-javascript/packages/client-sources/**') }}-${{ hashFiles('specs/dist/sources.yml') }}

- name: Restore built JavaScript predict client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
with:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/packages/client-predict/dist
key: ${{ runner.os }}-1-js-client-predict-${{ hashFiles('clients/algoliasearch-client-javascript/packages/client-predict/**') }}-${{ hashFiles('specs/dist/predict.yml') }}

- name: Restore built Java client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
Expand Down
1 change: 1 addition & 0 deletions .redocly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ apiDefinitions:
recommend: specs/recommend/spec.yml
search: specs/search/spec.yml
sources: specs/sources/spec.yml
predict: specs/predict/spec.yml

lint:
extends:
Expand Down
1 change: 1 addition & 0 deletions clients/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This folder hosts the generated clients.
- [@algolia/client-search](./algoliasearch-client-javascript/packages/client-search/): The Algolia search client.
- [@algolia/recommend](./algoliasearch-client-javascript/packages/recommend/): The Algolia recommend client.
- [@algolia/sources](./algoliasearch-client-javascript/packages/client-sources/): The Algolia sources client.
- [@algolia/predict](./algoliasearch-client-javascript/packages/client-predict/): The Algolia predict client.
- [@algolia/client-common](./algoliasearch-client-javascript/packages/client-common/): The JavaScript clients common files.
- [@algolia/requester-browser-xhr](./algoliasearch-client-javascript/packages/requester-browser-xhr/): Browser XHR requester for the Algolia JavaScript clients.
- [@algolia/requester-node-http](./algoliasearch-client-javascript/packages/requester-node-http/): Node.js HTTP requester for the Algolia JavaScript clients.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

git_push.sh
model/models.ts
.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Host, Requester } from '@algolia/client-common';
import { XhrRequester } from '@algolia/requester-browser-xhr';

import { createPredictApi } from './src/predictApi';
import type { PredictApi } from './src/predictApi';

export * from './src/predictApi';

export function predictApi(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
): PredictApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

return createPredictApi({
appId,
apiKey,

timeouts: {
connect: 1,
read: 2,
write: 30,
},
requester: options?.requester ?? new XhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
...options,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Affinities = {
name?: string;
value?: string;
probability?: number;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Error.
*/
export type ErrorBase = {
message?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Predictions } from './predictions';
import type { Properties } from './properties';
import type { Segments } from './segments';

export type FetchUserProfileResponse = {
user: string;
predictions?: Predictions;
properties?: Properties;
segments?: Segments;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type FunnelStage = {
name?: string;
probability?: number;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Object with models and types to retrieve.
*/
export type Params = {
/**
* List with model types for which to retrieve predictions.
*/
modelsToRetrieve?: ParamsModelsToRetrieve[];
/**
* List with types to be retrieved.
*/
typesToRetrieve?: ParamsTypesToRetrieve[];
};

export type ParamsModelsToRetrieve =
| 'affinities'
| 'funnel_stage'
| 'order_value';

export type ParamsTypesToRetrieve = 'properties' | 'segments';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PredictionsAffinities } from './predictionsAffinities';
import type { PredictionsFunnelStage } from './predictionsFunnelStage';
import type { PredictionsOrderValue } from './predictionsOrderValue';

export type Predictions = {
funnel_stage?: PredictionsFunnelStage;
order_value?: PredictionsOrderValue;
affinities?: PredictionsAffinities;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Affinities } from './affinities';

/**
* Prediction for the **affinities** model.
*/
export type PredictionsAffinities = {
value?: Affinities[];
lastUpdatedAt?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { FunnelStage } from './funnelStage';

/**
* Prediction for the **funnel_stage** model.
*/
export type PredictionsFunnelStage = {
value?: FunnelStage[];
lastUpdatedAt?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Prediction for the **order_value** model.
*/
export type PredictionsOrderValue = {
value?: number;
lastUpdatedAt?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Properties for the user profile.
*/
export type Properties = {
/**
* Raw user properties (key-value pairs).
*/
raw?: Record<string, any>;
/**
* Computed user properties (key-value pairs).
*/
computed?: Record<string, any>;
/**
* Custom user properties (key-value pairs).
*/
custom?: Record<string, any>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Segments that the user belongs to.
*/
export type Segments = {
/**
* List of computed segments IDs.
*/
computed?: string[];
/**
* List of custom segments IDs.
*/
custom?: string[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Host, Requester } from '@algolia/client-common';
import { HttpRequester } from '@algolia/requester-node-http';

import { createPredictApi } from './src/predictApi';
import type { PredictApi } from './src/predictApi';

export * from './src/predictApi';

export function predictApi(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
): PredictApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

return createPredictApi({
appId,
apiKey,

timeouts: {
connect: 2,
read: 5,
write: 30,
},
requester: options?.requester ?? new HttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
...options,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@algolia/client-predict",
"version": "0.0.1",
"description": "JavaScript client for @algolia/client-predict",
"repository": "algolia/algoliasearch-client-javascript",
"author": "Algolia",
"private": true,
"license": "MIT",
"main": "./dist/node.js",
"types": "./dist/node.d.ts",
"jsdelivr": "./dist/browser.js",
"unpkg": "./dist/browser.js",
"browser": {
"./index.js": "./dist/browser.js"
},
"scripts": {
"build": "tsc",
"clean": "rm -rf dist/"
},
"engines": {
"node": "^14.0.0"
},
"dependencies": {
"@algolia/client-common": "5.0.0",
"@algolia/requester-browser-xhr": "5.0.0",
"@algolia/requester-node-http": "5.0.0"
},
"devDependencies": {
"@types/node": "16.11.11",
"typescript": "4.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Transporter, createAuth, getUserAgent } from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Host,
Request,
} from '@algolia/client-common';

import type { FetchUserProfileResponse } from '../model/fetchUserProfileResponse';
import type { Params } from '../model/params';

export const version = '0.0.1';

function getDefaultHosts(): Host[] {
return [
{
url: 'predict-api-oslcbws3zq-ew.a.run.app',
accept: 'readWrite',
protocol: 'https',
},
];
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const createPredictApi = (options: CreateClientOptions) => {
const auth = createAuth(options.appId, options.apiKey, options.authMode);
const transporter = new Transporter({
hosts: options?.hosts ?? getDefaultHosts(),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
...auth.headers(),
},
baseQueryParameters: auth.queryParameters(),
userAgent: getUserAgent({
userAgents: options.userAgents,
client: 'Predict',
version,
}),
timeouts: options.timeouts,
requester: options.requester,
});

/**
* Get predictions, properties (raw, computed or custom) and segments (computed or custom) for a user profile.
*
* @summary Get user profile.
* @param fetchUserProfile - The fetchUserProfile object.
* @param fetchUserProfile.userID - User ID for authenticated users or cookie ID for non-authenticated repeated users (visitors).
* @param fetchUserProfile.params - The params object.
*/
function fetchUserProfile({
userID,
params,
}: FetchUserProfileProps): Promise<FetchUserProfileResponse> {
const path = '/1/users/{userID}/fetch'.replace(
'{userID}',
encodeURIComponent(String(userID))
);
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (!userID) {
throw new Error(
'Parameter `userID` is required when calling `fetchUserProfile`.'
);
}

if (!params) {
throw new Error(
'Parameter `params` is required when calling `fetchUserProfile`.'
);
}

const request: Request = {
method: 'POST',
path,
data: params,
};

return transporter.request(request, {
queryParameters,
headers,
});
}

return { fetchUserProfile };
};

export type PredictApi = ReturnType<typeof createPredictApi>;

export type FetchUserProfileProps = {
/**
* User ID for authenticated users or cookie ID for non-authenticated repeated users (visitors).
*/
userID: string;
params: Params;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
},
"include": ["src", "model", "node.ts", "browser.ts"],
"exclude": ["dist", "node_modules"]
}
Loading