From 0079ed67e5074922df568e23de6e75c41427c328 Mon Sep 17 00:00:00 2001 From: GermanVor Date: Wed, 30 Oct 2024 21:25:21 +0100 Subject: [PATCH] chore: cloudApi serviceClients --- src/session.ts | 19 +++++++++---------- src/token-service/iam-token-service.ts | 12 +++++++----- src/utils/operation/time-spent.ts | 5 +++-- src/utils/operation/wait-for.ts | 13 +++++-------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/session.ts b/src/session.ts index f530355c..fce60e21 100644 --- a/src/session.ts +++ b/src/session.ts @@ -13,8 +13,12 @@ import { import { IamTokenService } from './token-service/iam-token-service'; import { MetadataTokenService } from './token-service/metadata-token-service'; import { clientFactory } from './utils/client-factory'; -import { cloudApi, serviceClients } from '.'; + import { getServiceClientEndpoint } from './service-endpoints'; +import { + CreateIamTokenRequest, + IamTokenServiceClient, +} from './generated/yandex/cloud/iam/v1/iam_token_service'; const isOAuth = (config: SessionConfig): config is OAuthCredentialsConfig => 'oauthToken' in config; @@ -24,15 +28,10 @@ const isIamToken = (config: SessionConfig): config is IamTokenCredentialsConfig const isServiceAccount = (config: SessionConfig): config is ServiceAccountCredentialsConfig => 'serviceAccountJson' in config; -const createIamToken = async ( - iamEndpoint: string, - req: Partial, -) => { +const createIamToken = async (iamEndpoint: string, req: Partial) => { const channel = createChannel(iamEndpoint, credentials.createSsl()); - const client = clientFactory.create(serviceClients.IamTokenServiceClient.service, channel); - const resp = await client.create( - cloudApi.iam.iam_token_service.CreateIamTokenRequest.fromPartial(req), - ); + const client = clientFactory.create(IamTokenServiceClient.service, channel); + const resp = await client.create(CreateIamTokenRequest.fromPartial(req)); return resp.iamToken; }; @@ -40,7 +39,7 @@ const createIamToken = async ( const newTokenCreator = (config: SessionConfig): (() => Promise) => { if (isOAuth(config)) { return () => { - const iamEndpoint = getServiceClientEndpoint(serviceClients.IamTokenServiceClient); + const iamEndpoint = getServiceClientEndpoint(IamTokenServiceClient); return createIamToken(iamEndpoint, { yandexPassportOauthToken: config.oauthToken, diff --git a/src/token-service/iam-token-service.ts b/src/token-service/iam-token-service.ts index 8ccc6ddf..5147091a 100644 --- a/src/token-service/iam-token-service.ts +++ b/src/token-service/iam-token-service.ts @@ -2,12 +2,14 @@ import { credentials } from '@grpc/grpc-js'; import * as jwt from 'jsonwebtoken'; import { DateTime } from 'luxon'; import { createChannel } from 'nice-grpc'; -import { cloudApi, serviceClients } from '..'; import { getServiceClientEndpoint } from '../service-endpoints'; import { IIAmCredentials, ISslCredentials, TokenService } from '../types'; import { clientFactory } from '../utils/client-factory'; - -const { IamTokenServiceClient } = serviceClients; +import { + CreateIamTokenRequest, + CreateIamTokenResponse, + IamTokenServiceClient, +} from '../generated/yandex/cloud/iam/v1/iam_token_service'; export class IamTokenService implements TokenService { public readonly sslCredentials?: ISslCredentials; @@ -76,11 +78,11 @@ export class IamTokenService implements TokenService { } } - private async requestToken(): Promise { + private async requestToken(): Promise { const deadline = DateTime.now().plus({ millisecond: this.tokenRequestTimeout }).toJSDate(); return this.client().create( - cloudApi.iam.iam_token_service.CreateIamTokenRequest.fromPartial({ + CreateIamTokenRequest.fromPartial({ jwt: this.getJwtRequest(), }), { deadline }, diff --git a/src/utils/operation/time-spent.ts b/src/utils/operation/time-spent.ts index e17883dc..847b7850 100644 --- a/src/utils/operation/time-spent.ts +++ b/src/utils/operation/time-spent.ts @@ -1,4 +1,5 @@ import { Operation } from '../../generated/yandex/cloud/operation/operation'; -export const timeSpentOperation = (op: Operation): number => - Date.now() - (op.createdAt?.getTime() ?? 0); +export const timeSpentOperation = (op: Operation): number => { + return Date.now() - (op.createdAt?.getTime() ?? 0); +}; diff --git a/src/utils/operation/wait-for.ts b/src/utils/operation/wait-for.ts index e9708d0f..b0f47087 100644 --- a/src/utils/operation/wait-for.ts +++ b/src/utils/operation/wait-for.ts @@ -1,13 +1,10 @@ -import { cloudApi, serviceClients } from '../..'; import { Operation } from '../../generated/yandex/cloud/operation/operation'; +import { + GetOperationRequest, + OperationServiceClient, +} from '../../generated/yandex/cloud/operation/operation_service'; import { Session } from '../../session'; -const { - operation: { - operation_service: { GetOperationRequest }, - }, -} = cloudApi; - const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes export const waitForOperation = ( @@ -16,7 +13,7 @@ export const waitForOperation = ( timeoutMs: number = DEFAULT_TIMEOUT_MS, operationServiceEndpoint?: string, ): Promise => { - const client = session.client(serviceClients.OperationServiceClient, operationServiceEndpoint); + const client = session.client(OperationServiceClient, operationServiceEndpoint); const maxChecksCount = Math.ceil(timeoutMs / session.pollInterval); let checksCount = 0;