Skip to content
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

feat: Add clients for actor env vars #202

Merged
merged 5 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: Add clients for actor env vars
  • Loading branch information
jirimoravcik committed Aug 24, 2021
commit 632090712e51e5dfe8088195baf036f71e9d212e
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.1.0 / TBD
===================
- Added `ActorEnvVarCollectionClient` and `ActorEnvVarClient`

Copy link
Member

Choose a reason for hiding this comment

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

2.0.0 is not out yet, so move this to that version

2.0.0 / TBD
===================
- Laid out base for migration to TypeScript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apify-client",
"version": "2.0.0",
"version": "2.1.0",
Copy link
Member

Choose a reason for hiding this comment

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

no need for version bump as 2.0.0 is not yet out

Copy link
Member

Choose a reason for hiding this comment

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

2.0.0 is out on NPM as far as I know.

Copy link
Member

Choose a reason for hiding this comment

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

The beta is
image

Copy link
Member

Choose a reason for hiding this comment

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

It's not tagged as it was published by accident, but it's there
image

"description": "Apify API client for JavaScript",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
38 changes: 38 additions & 0 deletions src/resource_clients/actor_env_var.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ow from 'ow';
import { ApiClientSubResourceOptions } from '../base/api_client';
import { ResourceClient } from '../base/resource_client';
import { ActorEnvironmentVariable } from './actor_version';

/**
* @hideconstructor
*/
export class ActorEnvVarClient extends ResourceClient {
constructor(options: ApiClientSubResourceOptions) {
super({
resourcePath: 'env-vars',
...options,
});
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-object/get-env-var
*/
async get(): Promise<ActorEnvironmentVariable | undefined> {
return this._get();
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-object/update-env-var
*/
async update(actorEnvVar: ActorEnvironmentVariable): Promise<ActorEnvironmentVariable> {
ow(actorEnvVar, ow.object);
return this._update(actorEnvVar);
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-object/delete-env-var
*/
async delete(): Promise<void> {
return this._delete();
}
}
45 changes: 45 additions & 0 deletions src/resource_clients/actor_env_var_collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import ow from 'ow';
import { ApiClientSubResourceOptions } from '../base/api_client';
import { ResourceCollectionClient } from '../base/resource_collection_client';
import { PaginatedList } from '../utils';
import { ActorEnvironmentVariable } from './actor_version';

/**
* @hideconstructor
*/
export class ActorEnvVarCollectionClient extends ResourceCollectionClient {
constructor(options: ApiClientSubResourceOptions) {
super({
resourcePath: 'env-vars',
...options,
});
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-collection/get-list-of-env-vars
*/
async list(options: ActorEnvVarCollectionListOptions = {}): Promise<ActorEnvVarListResult> {
ow(options, ow.object.exactShape({
limit: ow.optional.number,
offset: ow.optional.number,
desc: ow.optional.boolean,
}));
return this._list(options);
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-collection/create-env-var
*/
async create(actorEnvVar: ActorEnvironmentVariable): Promise<ActorEnvironmentVariable> {
ow(actorEnvVar, ow.optional.object);
return this._create(actorEnvVar);
}
}

export interface ActorEnvVarCollectionListOptions {
limit?: number;
offset?: number;
desc?: boolean;
}

export type ActorEnvVarListResult = Pick<PaginatedList<ActorEnvironmentVariable>, 'total' | 'items'>
20 changes: 20 additions & 0 deletions src/resource_clients/actor_version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ow from 'ow';
import { ApiClientSubResourceOptions } from '../base/api_client';
import { ResourceClient } from '../base/resource_client';
import { ActorEnvVarClient } from './actor_env_var';
import { ActorEnvVarCollectionClient } from './actor_env_var_collection';

/**
* @hideconstructor
Expand Down Expand Up @@ -34,6 +36,24 @@ export class ActorVersionClient extends ResourceClient {
async delete(): Promise<void> {
return this._delete();
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-object
*/
envVar(envVarName: string): ActorEnvVarClient {
ow(envVarName, ow.string);
return new ActorEnvVarClient(this._subResourceOptions({
id: envVarName,
}));
}

/**
* TODO: https://docs.apify.com/api/v2#/reference/actors/env-var-collection
* @return {ActorVersionCollectionClient}
*/
envVars(): ActorEnvVarCollectionClient {
return new ActorEnvVarCollectionClient(this._subResourceOptions());
}
}

export interface BaseActorVersion<SourceType extends ActorSourceType> {
Expand Down
104 changes: 104 additions & 0 deletions test/actors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,110 @@ describe('Actor methods', () => {
});
});

describe('envVars()', () => {
test('list() works', async () => {
const actorId = 'some-id';
const versionNumber = '0.1';

const res = await client.actor(actorId).version(versionNumber).envVars().list();
expect(res.id).toEqual('list-actor-env-vars');
validateRequest({}, { actorId, versionNumber });

const browserRes = await page.evaluate((aId, vn) => client.actor(aId).version(vn).envVars().list(), actorId, versionNumber);
expect(browserRes).toEqual(res);
validateRequest({}, { actorId, versionNumber });
});

test('create() works', async () => {
const actorId = 'some-id';
const versionNumber = '0.1';
const actorEnvVar = {
name: 'TEST_ENV_VAR',
value: 123,
};

const res = await client.actor(actorId).version(versionNumber).envVars().create(actorEnvVar);
expect(res.id).toEqual('create-actor-env-var');
validateRequest({}, { actorId, versionNumber }, actorEnvVar);

const browserRes = await page.evaluate(
(aId, vn, ev) => client.actor(aId).version(vn).envVars().create(ev), actorId, versionNumber, actorEnvVar,
);
expect(browserRes).toEqual(res);
validateRequest({}, { actorId, versionNumber }, actorEnvVar);
});
});

describe('envVar()', () => {
test('get() works', async () => {
const actorId = 'some-id';
const versionNumber = '0.0';
const envVarName = 'TEST_ENV_VAR';

const res = await client.actor(actorId).version(versionNumber).envVar(envVarName).get();
expect(res.id).toEqual('get-actor-env-var');
validateRequest({}, { actorId, versionNumber, envVarName });

const browserRes = await page.evaluate(
(aId, vn, evn) => client.actor(aId).version(vn).envVar(evn).get(), actorId, versionNumber, envVarName,
);
expect(browserRes).toEqual(res);
validateRequest({}, { actorId, versionNumber, envVarName });
});

test('get() works if envVar did not exist', async () => {
const actorId = '404'; // this will make the mock API return 404. Must be the first param.
const versionNumber = '0.0';
const envVarName = 'TEST_ENV_VAR';

const res = await client.actor(actorId).version(versionNumber).envVar(envVarName).get();
expect(res).toBeUndefined();
validateRequest({}, { actorId, versionNumber, envVarName });

const browserRes = await page.evaluate(
(aId, vn, evn) => client.actor(aId).version(vn).envVar(evn).get(), actorId, versionNumber, envVarName,
);
expect(browserRes).toEqual(res);
validateRequest({}, { actorId, versionNumber, envVarName });
});

test('update() works', async () => {
const actorId = 'some-user/some-id';
const versionNumber = '0.0';
const envVarName = 'TEST_UPDATE_ENV_VAR';
const envVar = {
name: 'TEST_UPDATE_ENV_VAR',
value: 123,
};

const res = await client.actor(actorId).version(versionNumber).envVar(envVarName).update(envVar);
expect(res.id).toEqual('update-actor-env-var');
validateRequest({}, { actorId: 'some-user~some-id', versionNumber, envVarName }, envVar);

const browserRes = await page.evaluate(
(id, vn, evn, uev) => client.actor(id).version(vn).envVar(evn).update(uev), actorId, versionNumber, envVarName, envVar,
);
expect(browserRes).toEqual(res);
validateRequest({}, { actorId: 'some-user~some-id', versionNumber, envVarName }, envVar);
});

test('delete() works', async () => {
const actorId = '204';
const versionNumber = '0.0';
const envVarName = 'TEST_ENV_VAR_TO_DELETE';

const res = await client.actor(actorId).version(versionNumber).envVar(envVarName).delete();
expect(res).toBeUndefined();
validateRequest({}, { actorId, versionNumber, envVarName });

const browserRes = await page.evaluate(
(id, vn, evn) => client.actor(id).version(vn).envVar(evn).delete(), actorId, versionNumber, envVarName,
);
expect(browserRes).toEqual(res);
validateRequest({}, { actorId, versionNumber, envVarName });
});
});

test('webhooks().list() works', async () => {
const actorId = 'some-act-id';
const query = {
Expand Down
5 changes: 5 additions & 0 deletions test/mock_server/routes/actors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const ROUTES = [
{ id: 'get-actor-version', method: 'GET', path: '/:actorId/versions/:versionNumber' },
{ id: 'update-actor-version', method: 'PUT', path: '/:actorId/versions/:versionNumber' },
{ id: 'delete-actor-version', method: 'DELETE', path: '/:actorId/versions/:versionNumber' },
{ id: 'list-actor-env-vars', method: 'GET', path: '/:actorId/versions/:versionNumber/env-vars' },
{ id: 'create-actor-env-var', method: 'POST', path: '/:actorId/versions/:versionNumber/env-vars' },
{ id: 'get-actor-env-var', method: 'GET', path: '/:actorId/versions/:versionNumber/env-vars/:envVarName' },
{ id: 'update-actor-env-var', method: 'PUT', path: '/:actorId/versions/:versionNumber/env-vars/:envVarName' },
{ id: 'delete-actor-env-var', method: 'DELETE', path: '/:actorId/versions/:versionNumber/env-vars/:envVarName' },
{ id: 'list-webhooks', method: 'GET', path: '/:actorId/webhooks' },
];

Expand Down