Skip to content

Instantly share code, notes, and snippets.

@larkintuckerllc
Created March 8, 2022 15:35
Show Gist options
  • Save larkintuckerllc/ec09df15a7b1ac6a273a7c30577f979f to your computer and use it in GitHub Desktop.
Save larkintuckerllc/ec09df15a7b1ac6a273a7c30577f979f to your computer and use it in GitHub Desktop.
import { MyPluginApi } from './types';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
export class MyPluginBackendClient implements MyPluginApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
constructor(options: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
}) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
}
private async handleResponse(response: Response): Promise<any> {
if (!response.ok) {
throw new Error();
}
return await response.json();
}
async getHealth(): Promise<{ status: string; }> {
const url = `${await this.discoveryApi.getBaseUrl('my-plugin')}/health`;
const { token: idToken } = await this.identityApi.getCredentials();
const response = await fetch(url, {
method: 'GET',
headers: {
...(idToken && { Authorization: `Bearer ${idToken}` }),
},
});
return await this.handleResponse(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment