Use fetch

Signed-off-by: Alex Eftimie <alex.eftimie@getyourguide.com>
This commit is contained in:
Alex Eftimie
2024-05-23 10:39:51 +02:00
parent 5b794408e3
commit 206ce36aa2
8 changed files with 86 additions and 112 deletions
@@ -19,7 +19,7 @@ import { KubernetesBackendClient } from './KubernetesBackendClient';
import { rest } from 'msw';
import { UrlPatternDiscovery } from '@backstage/core-app-api';
import { setupServer } from 'msw/node';
import { setupRequestMockHandlers } from '@backstage/test-utils';
import { MockFetchApi, setupRequestMockHandlers } from '@backstage/test-utils';
import {
CustomObjectsByEntityRequest,
KubernetesRequestBody,
@@ -44,6 +44,7 @@ describe('KubernetesBackendClient', () => {
getBackstageIdentity: jest.fn(),
signOut: jest.fn(),
};
const fetchApi = new MockFetchApi({ injectIdentityAuth: { identityApi } });
beforeEach(() => {
jest.resetAllMocks();
@@ -51,7 +52,7 @@ describe('KubernetesBackendClient', () => {
discoveryApi: UrlPatternDiscovery.compile(
'http://localhost:1234/api/{{ pluginId }}',
),
identityApi,
fetchApi,
kubernetesAuthProvidersApi,
});
mockResponse = {
@@ -454,6 +455,7 @@ describe('KubernetesBackendClient', () => {
});
it('hits the /proxy API with serviceAccount as auth provider', async () => {
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
worker.use(
rest.get(
'http://localhost:1234/api/kubernetes/clusters',
@@ -21,7 +21,7 @@ import {
WorkloadsByEntityRequest,
CustomObjectsByEntityRequest,
} from '@backstage/plugin-kubernetes-common';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { DiscoveryApi, FetchApi } from '@backstage/core-plugin-api';
import { stringifyEntityRef } from '@backstage/catalog-model';
import { KubernetesAuthProvidersApi } from '../kubernetes-auth-provider';
import { NotFoundError } from '@backstage/errors';
@@ -29,16 +29,16 @@ import { NotFoundError } from '@backstage/errors';
/** @public */
export class KubernetesBackendClient implements KubernetesApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
private readonly fetchApi: FetchApi;
private readonly kubernetesAuthProvidersApi: KubernetesAuthProvidersApi;
constructor(options: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
fetchApi: FetchApi;
kubernetesAuthProvidersApi: KubernetesAuthProvidersApi;
}) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
this.fetchApi = options.fetchApi;
this.kubernetesAuthProvidersApi = options.kubernetesAuthProvidersApi;
}
@@ -62,12 +62,10 @@ export class KubernetesBackendClient implements KubernetesApi {
private async postRequired(path: string, requestBody: any): Promise<any> {
const url = `${await this.discoveryApi.getBaseUrl('kubernetes')}${path}`;
const { token: idToken } = await this.identityApi.getCredentials();
const response = await fetch(url, {
const response = await this.fetchApi.fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(idToken && { Authorization: `Bearer ${idToken}` }),
},
body: JSON.stringify(requestBody),
});
@@ -130,14 +128,8 @@ export class KubernetesBackendClient implements KubernetesApi {
}
async getClusters(): Promise<{ name: string; authProvider: string }[]> {
const { token: idToken } = await this.identityApi.getCredentials();
const url = `${await this.discoveryApi.getBaseUrl('kubernetes')}/clusters`;
const response = await fetch(url, {
method: 'GET',
headers: {
...(idToken && { Authorization: `Bearer ${idToken}` }),
},
});
const response = await this.fetchApi.fetch(url);
return (await this.handleResponse(response)).items;
}
@@ -157,15 +149,13 @@ export class KubernetesBackendClient implements KubernetesApi {
const url = `${await this.discoveryApi.getBaseUrl('kubernetes')}/proxy${
options.path
}`;
const identityResponse = await this.identityApi.getCredentials();
const headers = KubernetesBackendClient.getKubernetesHeaders(
options,
kubernetesCredentials?.token,
identityResponse,
authProvider,
oidcTokenProvider,
);
return await fetch(url, { ...options.init, headers });
return await this.fetchApi.fetch(url, { ...options.init, headers });
}
private static getKubernetesHeaders(
@@ -175,7 +165,6 @@ export class KubernetesBackendClient implements KubernetesApi {
init?: RequestInit;
},
k8sToken: string | undefined,
identityResponse: { token?: string },
authProvider: string,
oidcTokenProvider: string | undefined,
) {
@@ -190,9 +179,6 @@ export class KubernetesBackendClient implements KubernetesApi {
...(k8sToken && {
[kubernetesAuthHeader]: k8sToken,
}),
...(identityResponse.token && {
Authorization: `Bearer ${identityResponse.token}`,
}),
};
}