use discoveryApi for baseUrl

Signed-off-by: Klara <klarabroman@live.se>

Co-authored-by: lykkeaxlin <lykkeaxlin@hotmail.com>
This commit is contained in:
Klara
2021-09-16 15:12:55 +02:00
parent 99c9fb9b9a
commit 7b4f506a22
7 changed files with 69 additions and 67 deletions
+53 -30
View File
@@ -19,7 +19,11 @@ import {
EntityRef,
stringifyEntityRef,
} from '@backstage/catalog-model';
import { createApiRef, IdentityApi } from '@backstage/core-plugin-api';
import {
createApiRef,
DiscoveryApi,
IdentityApi,
} from '@backstage/core-plugin-api';
import { BazaarProject, Status } from './types';
export const bazaarApiRef = createApiRef<BazaarApi>({
@@ -29,48 +33,52 @@ export const bazaarApiRef = createApiRef<BazaarApi>({
export interface BazaarApi {
updateMetadata(
baseUrl: string,
entity: Entity,
name: string,
announcement: string,
status: Status,
): Promise<any>;
getMetadata(baseUrl: string, entity: Entity): Promise<any>;
getMetadata(entity: Entity): Promise<any>;
getMemberCounts(
bazaarProjects: BazaarProject[],
baseUrl: string,
): Promise<Map<EntityRef, number>>;
getMembers(baseUrl: string, entity: Entity): Promise<any>;
getMembers(entity: Entity): Promise<any>;
deleteMember(baseUrl: string, entity: Entity): Promise<void>;
deleteMember(entity: Entity): Promise<void>;
deleteMembers(baseUrl: string, entity: Entity): Promise<void>;
deleteMembers(entity: Entity): Promise<void>;
addMember(baseUrl: string, entity: Entity): Promise<void>;
addMember(entity: Entity): Promise<void>;
getEntities(baseUrl: string): Promise<any>;
getEntities(): Promise<any>;
deleteEntity(baseUrl: string, entity: Entity): Promise<void>;
deleteEntity(entity: Entity): Promise<void>;
}
export class BazaarClient implements BazaarApi {
private readonly identityApi: IdentityApi;
private readonly discoveryApi: DiscoveryApi;
constructor(options: { identityApi: IdentityApi }) {
constructor(options: {
identityApi: IdentityApi;
discoveryApi: DiscoveryApi;
}) {
this.identityApi = options.identityApi;
this.discoveryApi = options.discoveryApi;
}
async updateMetadata(
baseUrl: string,
entity: Entity,
name: string,
announcement: string,
status: Status,
): Promise<any> {
return await fetch(`${baseUrl}/api/bazaar/metadata`, {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
return await fetch(`${baseUrl}/metadata`, {
method: 'PUT',
headers: {
entity_ref: stringifyEntityRef(entity),
@@ -85,8 +93,10 @@ export class BazaarClient implements BazaarApi {
}).then(resp => resp.json());
}
async getMetadata(baseUrl: string, entity: Entity): Promise<any> {
return await fetch(`${baseUrl}/api/bazaar/metadata`, {
async getMetadata(entity: Entity): Promise<any> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
return await fetch(`${baseUrl}/metadata`, {
method: 'GET',
headers: {
entity_ref: stringifyEntityRef(entity),
@@ -96,11 +106,12 @@ export class BazaarClient implements BazaarApi {
async getMemberCounts(
bazaarProjects: BazaarProject[],
baseUrl: string,
): Promise<Map<EntityRef, number>> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
const members = new Map<EntityRef, number>();
for (const project of bazaarProjects) {
const response = await fetch(`${baseUrl}/api/bazaar/members`, {
const response = await fetch(`${baseUrl}/members`, {
method: 'GET',
headers: {
entity_ref: project.entityRef as string,
@@ -114,8 +125,10 @@ export class BazaarClient implements BazaarApi {
return members;
}
async getMembers(baseUrl: string, entity: Entity): Promise<any> {
return await fetch(`${baseUrl}/api/bazaar/members`, {
async getMembers(entity: Entity): Promise<any> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
return await fetch(`${baseUrl}/members`, {
method: 'GET',
headers: {
entity_ref: stringifyEntityRef(entity),
@@ -123,8 +136,10 @@ export class BazaarClient implements BazaarApi {
}).then(resp => resp.json());
}
async addMember(baseUrl: string, entity: Entity): Promise<void> {
await fetch(`${baseUrl}/api/bazaar/member`, {
async addMember(entity: Entity): Promise<void> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
await fetch(`${baseUrl}/member`, {
method: 'PUT',
headers: {
user_id: this.identityApi.getUserId(),
@@ -133,8 +148,10 @@ export class BazaarClient implements BazaarApi {
});
}
async deleteMember(baseUrl: string, entity: Entity): Promise<void> {
await fetch(`${baseUrl}/api/bazaar/member`, {
async deleteMember(entity: Entity): Promise<void> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
await fetch(`${baseUrl}/member`, {
method: 'DELETE',
headers: {
user_id: this.identityApi.getUserId(),
@@ -143,8 +160,10 @@ export class BazaarClient implements BazaarApi {
});
}
async deleteMembers(baseUrl: string, entity: Entity): Promise<void> {
await fetch(`${baseUrl}/api/bazaar/members`, {
async deleteMembers(entity: Entity): Promise<void> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
await fetch(`${baseUrl}/members`, {
method: 'DELETE',
headers: {
entity_ref: stringifyEntityRef(entity),
@@ -152,21 +171,25 @@ export class BazaarClient implements BazaarApi {
});
}
async getEntities(baseUrl: string): Promise<any> {
return await fetch(`${baseUrl}/api/bazaar/entities`, {
async getEntities(): Promise<any> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
return await fetch(`${baseUrl}/entities`, {
method: 'GET',
}).then(resp => resp.json());
}
async deleteEntity(baseUrl: string, entity: Entity): Promise<void> {
await fetch(`${baseUrl}/api/bazaar/metadata`, {
async deleteEntity(entity: Entity): Promise<void> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
await fetch(`${baseUrl}/metadata`, {
method: 'DELETE',
headers: {
entity_ref: stringifyEntityRef(entity),
},
});
await fetch(`${baseUrl}/api/bazaar/members`, {
await fetch(`${baseUrl}/members`, {
method: 'DELETE',
headers: {
user_id: this.identityApi.getUserId(),
@@ -17,7 +17,7 @@
import React, { useState, useEffect, Dispatch, SetStateAction } from 'react';
import { Entity, stringifyEntityRef } from '@backstage/catalog-model';
import { SubmitHandler } from 'react-hook-form';
import { useApi, configApiRef } from '@backstage/core-plugin-api';
import { useApi } from '@backstage/core-plugin-api';
import { ProjectDialog } from '../ProjectDialog';
import { ProjectSelector } from '../ProjectSelector';
import { BazaarProject, FormValues, Status } from '../../types';
@@ -39,9 +39,6 @@ export const AddProjectDialog = ({
setCatalogEntities,
}: Props) => {
const bazaarApi = useApi(bazaarApiRef);
const baseUrl = useApi(configApiRef)
.getConfig('backend')
.getString('baseUrl');
const [selectedEntity, setSelectedEntity] = useState(
catalogEntities ? catalogEntities[0] : null,
);
@@ -83,7 +80,6 @@ export const AddProjectDialog = ({
});
await bazaarApi.updateMetadata(
baseUrl,
selectedEntity!,
selectedEntity!.metadata.name,
formValues.announcement,
@@ -30,7 +30,7 @@ import IconButton from '@material-ui/core/IconButton';
import CloseIcon from '@material-ui/icons/Close';
import Typography from '@material-ui/core/Typography';
import { Entity } from '@backstage/catalog-model';
import { useApi, configApiRef } from '@backstage/core-plugin-api';
import { useApi } from '@backstage/core-plugin-api';
import { bazaarApiRef } from '../../api';
const styles = (theme: Theme) =>
@@ -101,10 +101,6 @@ export const DeleteProjectDialog = ({
handleClose,
setIsBazaar,
}: Props) => {
const baseUrl = useApi(configApiRef)
.getConfig('backend')
.getString('baseUrl');
const handleCloseAndClear = () => {
handleClose();
};
@@ -112,7 +108,7 @@ export const DeleteProjectDialog = ({
const bazaarApi = useApi(bazaarApiRef);
const handleSubmit = async () => {
await bazaarApi.deleteEntity(baseUrl, entity);
await bazaarApi.deleteEntity(entity);
setIsBazaar(false);
handleCloseAndClear();
};
@@ -16,7 +16,7 @@
import React, { useState, useEffect, Dispatch, SetStateAction } from 'react';
import { Entity } from '@backstage/catalog-model';
import { useApi, configApiRef } from '@backstage/core-plugin-api';
import { useApi } from '@backstage/core-plugin-api';
import { ProjectDialog } from '../ProjectDialog';
import { BazaarProject, FormValues } from '../../types';
import { bazaarApiRef } from '../../api';
@@ -37,10 +37,6 @@ export const EditProjectDialog = ({
open,
handleClose,
}: Props) => {
const baseUrl = useApi(configApiRef)
.getConfig('backend')
.getString('baseUrl');
const [defaultValues, setDefaultValues] = useState<FormValues>({
announcement: bazaarProject.announcement,
status: bazaarProject.status,
@@ -59,7 +55,6 @@ export const EditProjectDialog = ({
const formValues = getValues();
const updateResponse = await bazaarApi.updateMetadata(
baseUrl,
entity!,
entity.metadata.name,
formValues.announcement,
@@ -47,11 +47,7 @@ import DeleteIcon from '@material-ui/icons/Delete';
import { EditProjectDialog } from '../EditProjectDialog';
import { DeleteProjectDialog } from '../DeleteProjectDialog';
import ExitToAppIcon from '@material-ui/icons/ExitToApp';
import {
useApi,
identityApiRef,
configApiRef,
} from '@backstage/core-plugin-api';
import { useApi, identityApiRef } from '@backstage/core-plugin-api';
import { useAsync } from 'react-use';
import { Member, BazaarProject } from '../../types';
import { bazaarApiRef } from '../../api';
@@ -101,12 +97,8 @@ export const EntityBazaarInfoCard = () => {
});
const [isBazaar, setIsBazaar] = useState(false);
const baseUrl = useApi(configApiRef)
.getConfig('backend')
.getString('baseUrl');
const getInitMemberStatus = async () => {
const response = await bazaarApi.getMembers(baseUrl, entity);
const response = await bazaarApi.getMembers(entity);
const dbMembers = response.data.map((obj: any) => {
const member: Member = {
userId: obj.user_id,
@@ -128,7 +120,7 @@ export const EntityBazaarInfoCard = () => {
};
const getMetadata = async () => {
const response = await bazaarApi.getMetadata(baseUrl, entity);
const response = await bazaarApi.getMetadata(entity);
if (response.status !== 404) {
setIsBazaar(true);
@@ -181,14 +173,14 @@ export const EntityBazaarInfoCard = () => {
newMembers.sort(sortMembers);
return newMembers;
});
await bazaarApi.addMember(baseUrl, entity);
await bazaarApi.addMember(entity);
} else {
setMembers(
members.filter(
(member: Member) => member.userId !== identity.getUserId(),
),
);
await bazaarApi.deleteMember(baseUrl, entity);
await bazaarApi.deleteMember(entity);
}
};
@@ -27,7 +27,7 @@ import { ProjectPreview } from '../ProjectPreview/ProjectPreview';
import { Button, makeStyles, Link } from '@material-ui/core';
import { useAsync } from 'react-use';
import { Entity, EntityRef } from '@backstage/catalog-model';
import { useApi, configApiRef } from '@backstage/core-plugin-api';
import { useApi } from '@backstage/core-plugin-api';
import {
catalogApiRef,
CATALOG_FILTER_EXISTS,
@@ -52,9 +52,6 @@ export const SortView = () => {
const [bazaarProjects, setBazaarProjects] = useState<BazaarProject[]>([]);
const bazaarApi = useApi(bazaarApiRef);
const catalogApi = useApi(catalogApiRef);
const baseUrl = useApi(configApiRef)
.getConfig('backend')
.getString('baseUrl');
const compareProjectsByDate = (
a: BazaarProject,
@@ -78,7 +75,7 @@ export const SortView = () => {
fields: ['apiVersion', 'kind', 'metadata', 'spec'],
});
const response = await bazaarApi.getEntities(baseUrl);
const response = await bazaarApi.getEntities();
const dbProjects: BazaarProject[] = [];
const bazaarProjectRefs: string[] = [];
@@ -94,7 +91,7 @@ export const SortView = () => {
bazaarProjectRefs.push(project.entity_ref);
});
setBazaarMembers(await bazaarApi.getMemberCounts(dbProjects, baseUrl));
setBazaarMembers(await bazaarApi.getMemberCounts(dbProjects));
setBazaarProjects(dbProjects);
setCatalogEntities(
entities.items.filter((entity: Entity) => {
+4 -1
View File
@@ -20,6 +20,7 @@ import {
createPlugin,
createRoutableExtension,
identityApiRef,
discoveryApiRef,
} from '@backstage/core-plugin-api';
import { bazaarApiRef, BazaarClient } from './api';
@@ -33,8 +34,10 @@ export const bazaarPlugin = createPlugin({
api: bazaarApiRef,
deps: {
identityApi: identityApiRef,
discoveryApi: discoveryApiRef,
},
factory: ({ identityApi }) => new BazaarClient({ identityApi }),
factory: ({ identityApi, discoveryApi }) =>
new BazaarClient({ identityApi, discoveryApi }),
}),
],
});