Use FetchApi wrapper instead of manually passing through the token

Signed-off-by: Robert Cen <robert.cen@safetyculture.io>
This commit is contained in:
Robert Cen
2022-08-12 10:01:16 +10:00
parent 459b179de4
commit 9a20067f44
4 changed files with 16 additions and 29 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
import { ApiRef } from '@backstage/core-plugin-api';
import { BackstagePlugin } from '@backstage/core-plugin-api';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { IdentityApi } from '@backstage/core-plugin-api';
import { FetchApi } from '@backstage/core-plugin-api';
import { RouteRef } from '@backstage/core-plugin-api';
// Warning: (ae-missing-release-tag) "CodeClimateApi" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
+10 -23
View File
@@ -22,7 +22,7 @@ import {
CodeClimateIssuesData,
} from './code-climate-data';
import { CodeClimateApi } from './code-climate-api';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { DiscoveryApi, FetchApi } from '@backstage/core-plugin-api';
import { Duration } from 'luxon';
import humanizeDuration from 'humanize-duration';
@@ -36,16 +36,16 @@ const otherIssuesQuery = `${basicIssuesOptions}&${categoriesFilter}=Bug%20Risk`;
type Options = {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
fetchApi: FetchApi;
};
export class ProductionCodeClimateApi implements CodeClimateApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
private readonly fetchApi: FetchApi;
constructor(options: Options) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
this.fetchApi = options.fetchApi;
}
async fetchAllData(options: {
@@ -55,11 +55,6 @@ export class ProductionCodeClimateApi implements CodeClimateApi {
testReportID: string;
}): Promise<any> {
const { apiUrl, repoID, snapshotID, testReportID } = options;
const { token } = await this.identityApi.getCredentials();
const headersWithAuth = {
headers: { Authorization: `Bearer ${token}` },
};
const [
maintainabilityResponse,
testCoverageResponse,
@@ -67,25 +62,20 @@ export class ProductionCodeClimateApi implements CodeClimateApi {
duplicationResponse,
otherIssuesResponse,
] = await Promise.all([
await fetch(
await this.fetchApi.fetch(
`${apiUrl}/repos/${repoID}/snapshots/${snapshotID}`,
headersWithAuth,
),
await fetch(
await this.fetchApi.fetch(
`${apiUrl}/repos/${repoID}/test_reports/${testReportID}`,
headersWithAuth,
),
await fetch(
await this.fetchApi.fetch(
`${apiUrl}/repos/${repoID}/snapshots/${snapshotID}/issues?${codeSmellsQuery}`,
headersWithAuth,
),
await fetch(
await this.fetchApi.fetch(
`${apiUrl}/repos/${repoID}/snapshots/${snapshotID}/issues?${duplicationQuery}`,
headersWithAuth,
),
await fetch(
await this.fetchApi.fetch(
`${apiUrl}/repos/${repoID}/snapshots/${snapshotID}/issues?${otherIssuesQuery}`,
headersWithAuth,
),
]);
@@ -132,11 +122,8 @@ export class ProductionCodeClimateApi implements CodeClimateApi {
const apiUrl = `${await this.discoveryApi.getBaseUrl(
'proxy',
)}/codeclimate/api`;
const { token } = await this.identityApi.getCredentials();
const repoResponse = await fetch(`${apiUrl}/repos/${repoID}`, {
headers: { Authorization: `Bearer ${token}` },
});
const repoResponse = await this.fetchApi.fetch(`${apiUrl}/repos/${repoID}`);
if (!repoResponse.ok) {
throw new Error('Failed fetching Code Climate info');
+4 -4
View File
@@ -20,7 +20,7 @@ import {
createPlugin,
createRouteRef,
discoveryApiRef,
identityApiRef,
fetchApiRef,
createComponentExtension,
} from '@backstage/core-plugin-api';
@@ -37,10 +37,10 @@ export const codeClimatePlugin = createPlugin({
api: codeClimateApiRef,
deps: {
discoveryApi: discoveryApiRef,
identityApi: identityApiRef,
fetchApi: fetchApiRef,
},
factory: ({ discoveryApi, identityApi }) =>
new ProductionCodeClimateApi({ discoveryApi, identityApi }),
factory: ({ discoveryApi, fetchApi }) =>
new ProductionCodeClimateApi({ discoveryApi, fetchApi }),
}),
],
routes: {