use discoveryApi and update readme

This commit is contained in:
Samira Mokaram
2020-11-27 11:10:04 +01:00
parent ba63a66aa0
commit 60b290cd0b
2 changed files with 17 additions and 9 deletions
+4 -5
View File
@@ -47,18 +47,17 @@ import {
## Client configuration
The PagerDutyClient can be configured with the appropriate urls:
The client takes a config object which contains an instance of the `discoveryApi` which is used to look up the `baseUrl` of the `proxy`, and a `eventUrl` which is defaulted to: "https://events.pagerduty.com/v2".
In `apis.ts`:
```ts
createApiFactory({
api: pagerDutyApiRef,
deps: { configApi: configApiRef },
factory: ({ configApi }) =>
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) =>
new PagerDutyClientApi({
api_url: `https://api.pagerduty.com`,
events_url: 'https://events.pagerduty.com/v2',
discoveryApi: discoveryApi
}),
}),
```
+13 -4
View File
@@ -39,7 +39,9 @@ export class PagerDutyClientApi implements PagerDutyClient {
async getServiceByIntegrationKey(integrationKey: string): Promise<Service[]> {
const params = `include[]=integrations&include[]=escalation_policies&query=${integrationKey}`;
const url = `${this.config.api_url}/services?${params}`;
const url = `${await this.config.discoveryApi.getBaseUrl(
'proxy',
)}/pagerduty/services?${params}`;
const { services } = await this.getByUrl<ServicesResponse>(url);
return services;
@@ -47,7 +49,9 @@ export class PagerDutyClientApi implements PagerDutyClient {
async getIncidentsByServiceId(serviceId: string): Promise<Incident[]> {
const params = `service_ids[]=${serviceId}`;
const url = `${this.config.api_url}/incidents?${params}`;
const url = `${await this.config.discoveryApi.getBaseUrl(
'proxy',
)}/pagerduty/incidents?${params}`;
const { incidents } = await this.getByUrl<IncidentsResponse>(url);
return incidents;
@@ -55,7 +59,9 @@ export class PagerDutyClientApi implements PagerDutyClient {
async getOnCallByPolicyId(policyId: string): Promise<OnCall[]> {
const params = `include[]=users&escalation_policy_ids[]=${policyId}`;
const url = `${this.config.api_url}/oncalls?${params}`;
const url = `${await this.config.discoveryApi.getBaseUrl(
'proxy',
)}/pagerduty/oncalls?${params}`;
const { oncalls } = await this.getByUrl<OnCallsResponse>(url);
return oncalls;
@@ -92,7 +98,10 @@ export class PagerDutyClientApi implements PagerDutyClient {
body,
};
return this.request(`${this.config.events_url}/enqueue`, options);
return this.request(
`${this.config.eventsUrl ?? 'https://events.pagerduty.com/v2'}/enqueue`,
options,
);
}
private async getByUrl<T>(url: string): Promise<T> {