Created
March 8, 2022 15:35
-
-
Save larkintuckerllc/ec09df15a7b1ac6a273a7c30577f979f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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