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

Add support to auth caching in all packages #578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions packages/account/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { BaseApiClient, ApiVersion } from '@openzeppelin/defender-base-client';

import { AccountUsageResponse } from './models/account';
import { ClientParams } from '@openzeppelin/defender-base-client';

// AccountClient class
export class AccountClient extends BaseApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
authConfig: { useCredentialsCaching: params.useCredentialsCaching ?? false, type: 'admin' },
});
}

protected getPoolId(): string {
return process.env.DEFENDER_ADMIN_POOL_ID ?? 'us-west-2_94f3puJWv';
}
Expand Down
12 changes: 11 additions & 1 deletion packages/admin/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseApiClient, ApiVersion } from '@openzeppelin/defender-base-client';
import { BaseApiClient, ApiVersion, ClientParams } from '@openzeppelin/defender-base-client';
import { capitalize, isArray, isEmpty } from 'lodash';
import { Interface } from 'ethers/lib/utils';

Expand Down Expand Up @@ -46,6 +46,16 @@ export interface ProposalResponseWithUrl extends ProposalResponse {
}

export class AdminClient extends BaseApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
authConfig: { useCredentialsCaching: params.useCredentialsCaching ?? false, type: 'admin' },
});
}

protected getPoolId(): string {
return process.env.DEFENDER_ADMIN_POOL_ID || 'us-west-2_94f3puJWv';
}
Expand Down
12 changes: 11 additions & 1 deletion packages/autotask-client/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash } from 'crypto';
import { BaseApiClient, ApiVersion } from '@openzeppelin/defender-base-client';
import { BaseApiClient, ApiVersion, ClientParams } from '@openzeppelin/defender-base-client';
import {
CreateAutotaskRequest,
UpdateAutotaskRequest,
Expand All @@ -22,6 +22,16 @@ type SourceFiles = {
};

export class AutotaskClient extends BaseApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
authConfig: { useCredentialsCaching: params.useCredentialsCaching ?? false, type: 'admin' },
});
}

protected getPoolId(): string {
return process.env.DEFENDER_AUTOTASK_POOL_ID || 'us-west-2_94f3puJWv';
}
Expand Down
16 changes: 15 additions & 1 deletion packages/base/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export type AuthConfig = {
type: AuthType;
};

export type BaseApiClientParams = {
apiKey: string;
apiSecret: string;
httpsAgent?: https.Agent;
authConfig?: AuthConfig;
};

export type ClientParams = {
apiKey: string;
apiSecret: string;
httpsAgent?: https.Agent;
useCredentialsCaching?: boolean;
};

export abstract class BaseApiClient {
private api: AxiosInstance | undefined;
private version: ApiVersion | undefined;
Expand All @@ -27,7 +41,7 @@ export abstract class BaseApiClient {
protected abstract getPoolClientId(): string;
protected abstract getApiUrl(v: ApiVersion, type?: AuthType): string;

public constructor(params: { apiKey: string; apiSecret: string; httpsAgent?: https.Agent; authConfig?: AuthConfig }) {
public constructor(params: BaseApiClientParams) {
if (!params.apiKey) throw new Error(`API key is required`);
if (!params.apiSecret) throw new Error(`API secret is required`);

Expand Down
2 changes: 1 addition & 1 deletion packages/base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { createApi, createAuthenticatedApi } from './api/api';
export { authenticate } from './api/auth';
export { BaseApiClient, ApiVersion, AuthConfig } from './api/client';
export { BaseApiClient, ApiVersion, AuthConfig, BaseApiClientParams, ClientParams } from './api/client';
export { AuthType } from './api/auth-v2';
export * from './utils/network';

Expand Down
11 changes: 11 additions & 0 deletions packages/deploy/src/api/block-explorer-api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ import {
RemoveResponse,
UpdateBlockExplorerApiKeyRequest,
} from '../models';
import { ClientParams } from '@openzeppelin/defender-base-client';

const PATH = '/block-explorer-api-key';

export class BlockExplorerApiKeyClient extends PlatformApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
useCredentialsCaching: params.useCredentialsCaching,
});
}

public async get(blockExplorerApiKeyId: string): Promise<BlockExplorerApiKeyResponse> {
return this.apiCall(async (api) => {
return api.get(`${PATH}/${blockExplorerApiKeyId}`);
Expand Down
11 changes: 11 additions & 0 deletions packages/deploy/src/api/deployment-config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { PlatformApiClient } from './platform';
import { DeploymentConfigCreateRequest, DeploymentConfigResponse, RemoveResponse } from '../models';
import { ClientParams } from '@openzeppelin/defender-base-client';

const PATH = '/deployment-config';

export class DeploymentConfigClient extends PlatformApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
useCredentialsCaching: params.useCredentialsCaching,
});
}

public async get(deploymentConfigId: string): Promise<DeploymentConfigResponse> {
return this.apiCall(async (api) => {
return api.get(`${PATH}/${deploymentConfigId}`);
Expand Down
12 changes: 11 additions & 1 deletion packages/deploy/src/api/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { isEmpty } from 'lodash';
import { Network } from '@openzeppelin/defender-base-client';
import { ClientParams, Network } from '@openzeppelin/defender-base-client';
import { PlatformApiClient } from './platform';
import { ApprovalProcessResponse, DeployContractRequest, DeploymentResponse } from '../models';

const PATH = '/deployments';

export class DeploymentClient extends PlatformApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
useCredentialsCaching: params.useCredentialsCaching,
});
}

public async getApprovalProcess(network: Network): Promise<ApprovalProcessResponse> {
return this.apiCall(async (api) => {
return api.get(`${PATH}/config/${network}`);
Expand Down
12 changes: 11 additions & 1 deletion packages/deploy/src/api/platform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { BaseApiClient, ApiVersion } from '@openzeppelin/defender-base-client';
import { BaseApiClient, ApiVersion, ClientParams } from '@openzeppelin/defender-base-client';

export class PlatformApiClient extends BaseApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
authConfig: { useCredentialsCaching: params.useCredentialsCaching ?? false, type: 'admin' },
});
}

protected getPoolId(): string {
return process.env.PLATFORM_POOL_ID || 'us-west-2_94f3puJWv';
}
Expand Down
12 changes: 11 additions & 1 deletion packages/deploy/src/api/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { Network } from '@openzeppelin/defender-base-client';
import { ClientParams, Network } from '@openzeppelin/defender-base-client';

import { PlatformApiClient } from './platform';
import { ApprovalProcessResponse, UpgradeContractRequest, UpgradeContractResponse } from '../models';

const PATH = '/upgrades';

export class UpgradeClient extends PlatformApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
useCredentialsCaching: params.useCredentialsCaching,
});
}

public async getApprovalProcess(network: Network): Promise<ApprovalProcessResponse> {
return this.apiCall(async (api) => {
return api.get(`${PATH}/config/${network}`);
Expand Down
11 changes: 11 additions & 0 deletions packages/sentinel/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ import {
UpdateNotificationCategoryRequest,
} from '../models/category';
import { ListNetworkRequestOptions } from '../models/networks';
import { ClientParams } from '@openzeppelin/defender-base-client';

export class SentinelClient extends BaseApiClient {

constructor(params: ClientParams) {
super({
apiKey: params.apiKey,
apiSecret: params.apiSecret,
httpsAgent: params.httpsAgent,
authConfig: { useCredentialsCaching: params.useCredentialsCaching ?? false, type: 'admin' },
});
}

protected getPoolId(): string {
return process.env.DEFENDER_SENTINEL_POOL_ID || 'us-west-2_94f3puJWv';
}
Expand Down
Loading