From a7a96a53c8dcf38ba70d4c8b9172c31d1aec7866 Mon Sep 17 00:00:00 2001 From: Marcos Date: Thu, 23 May 2024 09:34:25 +0200 Subject: [PATCH] add support to caching in all packages --- packages/account/src/api.ts | 11 +++++++++++ packages/admin/src/api.ts | 12 +++++++++++- packages/autotask-client/src/api.ts | 12 +++++++++++- packages/base/src/api/client.ts | 16 +++++++++++++++- packages/base/src/index.ts | 2 +- .../deploy/src/api/block-explorer-api-key.ts | 11 +++++++++++ packages/deploy/src/api/deployment-config.ts | 11 +++++++++++ packages/deploy/src/api/deployment.ts | 12 +++++++++++- packages/deploy/src/api/platform.ts | 12 +++++++++++- packages/deploy/src/api/upgrade.ts | 12 +++++++++++- packages/sentinel/src/api/index.ts | 11 +++++++++++ 11 files changed, 115 insertions(+), 7 deletions(-) diff --git a/packages/account/src/api.ts b/packages/account/src/api.ts index 43e6d2fc..f638b769 100644 --- a/packages/account/src/api.ts +++ b/packages/account/src/api.ts @@ -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'; } diff --git a/packages/admin/src/api.ts b/packages/admin/src/api.ts index 74d5ab26..3fefb3ff 100644 --- a/packages/admin/src/api.ts +++ b/packages/admin/src/api.ts @@ -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'; @@ -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'; } diff --git a/packages/autotask-client/src/api.ts b/packages/autotask-client/src/api.ts index 18796117..4f451736 100644 --- a/packages/autotask-client/src/api.ts +++ b/packages/autotask-client/src/api.ts @@ -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, @@ -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'; } diff --git a/packages/base/src/api/client.ts b/packages/base/src/api/client.ts index 94f399bc..c8a3e295 100644 --- a/packages/base/src/api/client.ts +++ b/packages/base/src/api/client.ts @@ -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; @@ -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`); diff --git a/packages/base/src/index.ts b/packages/base/src/index.ts index 95cf7e4a..15848017 100644 --- a/packages/base/src/index.ts +++ b/packages/base/src/index.ts @@ -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'; diff --git a/packages/deploy/src/api/block-explorer-api-key.ts b/packages/deploy/src/api/block-explorer-api-key.ts index 8439d6f1..f1e21143 100644 --- a/packages/deploy/src/api/block-explorer-api-key.ts +++ b/packages/deploy/src/api/block-explorer-api-key.ts @@ -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 { return this.apiCall(async (api) => { return api.get(`${PATH}/${blockExplorerApiKeyId}`); diff --git a/packages/deploy/src/api/deployment-config.ts b/packages/deploy/src/api/deployment-config.ts index 9af0b008..7f94b03a 100644 --- a/packages/deploy/src/api/deployment-config.ts +++ b/packages/deploy/src/api/deployment-config.ts @@ -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 { return this.apiCall(async (api) => { return api.get(`${PATH}/${deploymentConfigId}`); diff --git a/packages/deploy/src/api/deployment.ts b/packages/deploy/src/api/deployment.ts index b43b2c9a..59f0ff4e 100644 --- a/packages/deploy/src/api/deployment.ts +++ b/packages/deploy/src/api/deployment.ts @@ -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 { return this.apiCall(async (api) => { return api.get(`${PATH}/config/${network}`); diff --git a/packages/deploy/src/api/platform.ts b/packages/deploy/src/api/platform.ts index 57ad4293..344b0eef 100644 --- a/packages/deploy/src/api/platform.ts +++ b/packages/deploy/src/api/platform.ts @@ -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'; } diff --git a/packages/deploy/src/api/upgrade.ts b/packages/deploy/src/api/upgrade.ts index 9b99c082..6c35990a 100644 --- a/packages/deploy/src/api/upgrade.ts +++ b/packages/deploy/src/api/upgrade.ts @@ -1,4 +1,4 @@ -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'; @@ -6,6 +6,16 @@ import { ApprovalProcessResponse, UpgradeContractRequest, UpgradeContractRespons 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 { return this.apiCall(async (api) => { return api.get(`${PATH}/config/${network}`); diff --git a/packages/sentinel/src/api/index.ts b/packages/sentinel/src/api/index.ts index 6a14e41c..fca8f5b9 100644 --- a/packages/sentinel/src/api/index.ts +++ b/packages/sentinel/src/api/index.ts @@ -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'; }