implement observers to get gateway and private api data
MODIFY
src/app/api-data.service.ts
src/app/api-data.service.ts
@@ -125,9 +125,24 @@ export interface Organization {
createdBy: string;
}
+export interface GatewayDetails {
+ name: string;
+ id: string;
+ type: 'REST';
+ description: string;
+}
+
+export interface GatewayEndpoint {
+ endpoint: string;
+}
+
@Injectable({
providedIn: 'root'
})
+
+/**
+ * A service which executes the REST calls to Apiman UI REST Interface
+ */
export class ApiDataService {
constructor(private http: HttpClient) { }
@@ -180,4 +195,14 @@ export class ApiDataService {
return this.http.get(url) as Observable<Array<ContractDetails>>;
}
+ public getGateways() {
+ const url = this.gatewayEndpoint + '/gateways';
+ return this.http.get(url) as Observable<Array<GatewayDetails>>;
+ }
+
+ public getGatewayEndpoint(gatewayId) {
+ const url = this.gatewayEndpoint + '/gateways/' + gatewayId + '/endpoint';
+ return this.http.get(url) as Observable<GatewayEndpoint>;
+ }
+
}

MODIFY
src/app/api-list/api-list.component.html
src/app/api-list/api-list.component.html
<table mat-table [dataSource]="apiData">
- <ng-container matColumnDef="id">
- <th mat-header-cell *matHeaderCellDef>API Id</th>
- <td mat-cell *matCellDef="let element"> {{element.id}} </td>
- </ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>API Name</th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
+ <ng-container matColumnDef="version">
+ <th mat-header-cell *matHeaderCellDef>API Version</th>
+ <td mat-cell *matCellDef="let element"> {{element.version}} </td>
+ </ng-container>
<ng-container matColumnDef="ispublic">
<th mat-header-cell *matHeaderCellDef>is public</th>
<td mat-cell *matCellDef="let element"> {{element.ispublic}} </td>

MODIFY
src/app/api-list/api-list.component.ts
src/app/api-list/api-list.component.ts
import { Component, OnInit } from '@angular/core';
-import { forkJoin } from 'rxjs';
-import {ApiDataService, ClientDetails, ClientVersionDetails} from '../api-data.service';
+import {forkJoin, Observable, from, pipe} from 'rxjs';
+import {map, mergeMap, toArray, mergeAll} from 'rxjs/operators';
+import {ApiDataService} from '../api-data.service';
+import {element} from 'protractor';
export interface ApiListView {
id: string;
name: string;
+ version: string;
endpoint: string;
ispublic: boolean;
}
@@ -19,31 +22,89 @@ export class ApiListComponent implements OnInit {
apiData: Array<ApiListView> = [];
- displayedColumns: string[] = ['id', 'ispublic', 'name', 'endpoint'];
+ displayedColumns: string[] = ['ispublic', 'name', 'version', 'endpoint'];
- constructor(private apiDataService: ApiDataService) { }
+ /**
+ * An observer to get all gateway details (gateway endpoint) from api data service
+ */
+ getGatewayDataObservable = this.apiDataService.getGateways()
+ .pipe(mergeAll())
+ .pipe(mergeMap(gateway => this.apiDataService.getGatewayEndpoint(gateway.id)
+ .pipe(map(gatewayEndpoint => ({id: gateway.id, endpoint: gatewayEndpoint.endpoint})))
+ ))
+ .pipe(toArray());
+ /**
+ * An observer to access all data of private apis
+ */
+ getPrivateApiData: Observable<ApiListView> = this.getGatewayDataObservable.pipe(mergeMap(gateways =>
+ this.apiDataService.getUserClients()
+ .pipe(mergeAll())
+ .pipe(mergeMap(userClient =>
+ this.apiDataService.getClientVersions(userClient.organizationId, userClient.id)
+ .pipe(mergeAll())
+ .pipe(mergeMap(clientVersion =>
+ this.apiDataService.getContracts(userClient.organizationId, userClient.id, clientVersion.version)
+ .pipe(mergeAll())
+ .pipe(mergeMap(contract =>
+ //fork data to receive api key and api details
+ forkJoin(
+ this.apiDataService.getApiKey(contract.clientOrganizationId, contract.clientId, contract.clientVersion)
+ //extract the api key
+ .pipe(map(source => source.apiKey)),
+ this.apiDataService.getApiVersionDetails(contract.clientOrganizationId, contract.apiId, contract.apiVersion)
+ //determine the first possible gateway endpoint and wether the api is public
+ .pipe(map(source => ({gatewayId: source.gateways[0].gatewayId, isPublic: source.publicAPI})))
+ //use the contract, gateway and forked api data to construct the view data
+ ).pipe(map((forkedApiData) => (this.buildViewData(contract, gateways, forkedApiData[0], forkedApiData[1]))))
+ ))
+ ))
+ ))
+ ));
+
+ constructor(private apiDataService: ApiDataService) {
+ }
+
+ /**
+ * ngOnInit is executed if the component is initialized
+ */
ngOnInit() {
- this.apiDataService.getUserClients().subscribe((data: Array<ClientDetails>) => {
- data.forEach((client) => {
- this.apiDataService.getClientVersions(client.organizationId, client.id).subscribe((clientData: Array<ClientVersionDetails>) => {
- clientData.forEach((clientDataDetail) => {
- this.apiDataService.getContracts(client.organizationId, client.id, clientDataDetail.version).subscribe((contractDetails) => {
- contractDetails.forEach((contractDetail) => {
- forkJoin(this.apiDataService.getApiEndpoint(contractDetail.apiOrganizationId, contractDetail.apiId, contractDetail.apiVersion), this.apiDataService.getApiKey(contractDetail.apiOrganizationId, contractDetail.clientId, contractDetail.clientVersion),
- this.apiDataService.getApiVersionDetails(contractDetail.clientOrganizationId, contractDetail.apiId, contractDetail.apiVersion)).subscribe((forkedData) => {
- this.apiData = this.apiData.concat({
- id: contractDetail.apiId,
- name: contractDetail.apiName,
- endpoint: forkedData[0].managedEndpoint + '?apikey=' + forkedData[1].apiKey,
- ispublic: forkedData[2].publicAPI
- });
- });
- });
- });
- });
- });
+ this.getGatewayDataObservable.subscribe( gwData => {
+ const test = gwData;
});
+
+ //subscribe for the private api data to fill the view
+ this.getPrivateApiData.subscribe(data => {
+ this.apiData = this.apiData.concat(data);
});
}
+
+ /**
+ * Builds the data view object
+ * @param contract: the contract between client and api
+ * @param gateways: the gateways which contains the gateway endpoints
+ * @param apiVersionDetails: the api version data containing the apikey and gateway id
+ */
+ private buildViewData(contract, gateways, apiKey, apiVersionDetails) {
+ return {
+ id: contract.apiId,
+ name: contract.apiName,
+ version: contract.apiVersion,
+ endpoint: this.buildApiEndpoint(gateways.find(g => g.id === apiVersionDetails.gatewayId).endpoint, contract.clientOrganizationId, contract.apiId, contract.apiVersion, apiKey),
+ ispublic: apiVersionDetails.isPublic
+ };
+ }
+
+ /**
+ * Builds endpoint for a private api
+ * @param gatewayEndpoint: the gateway endpoint
+ * @param organizationId: organization of api
+ * @param apiId: id of api
+ * @param apiVersion: version of api
+ * @param apiKey: apikey of api
+ */
+ buildApiEndpoint(gatewayEndpoint: string, organizationId: string, apiId: string, apiVersion: string, apiKey: string) {
+ return gatewayEndpoint + [organizationId, apiId, apiVersion].join('/') + '?apiKey=' + apiKey;
+ }
+
}

implement observers to get gateway and private api data