Load API list depending on keycloak username
MODIFY
src/app/components/developer/api-list/api-list.component.ts
src/app/components/developer/api-list/api-list.component.ts
-import {Component, Input, OnChanges, SimpleChanges} from '@angular/core';
+import {Component, OnInit} from '@angular/core';
import {forkJoin, from, Observable, ObservedValueOf} from 'rxjs';
import {map, mergeAll, mergeMap, toArray} from 'rxjs/operators';
import {ApiDataService, ApiVersion, Client, Contract} from '../../../services/api-data.service';
@@ -7,6 +7,7 @@ import {Toast, ToasterService} from 'angular2-toaster';
import {Router} from '@angular/router';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {Sort} from '@angular/material/sort';
+import {KeycloakService} from 'keycloak-angular';
export interface ApiListElement {
id: string;
@@ -36,7 +37,7 @@ export interface ApiListElement {
]
})
-export class ApiListComponent implements OnChanges {
+export class ApiListComponent implements OnInit {
columnHeaders: string[] = ['api', 'apiVersion', 'tryApi', 'options'];
expandedElements: Array<ApiListElement> = [];
@@ -44,18 +45,21 @@ export class ApiListComponent implements OnChanges {
apiData: Array<ApiListElement> = [];
sortedApiData: Array<ApiListElement>;
+ developerId: string = this.keycloak.getKeycloakInstance().profile.username;
+
status = class ApiStatus {
static retired = 'Retired';
static active = 'Active';
static inactive = 'Inactive';
};
- @Input('developerId') developerId;
+ // @Input('developerId') developerId;
constructor(private router: Router,
private apiDataService: ApiDataService,
private toasterService: ToasterService,
- private loadingSpinnerService: SpinnerService) {
+ private loadingSpinnerService: SpinnerService,
+ private keycloak: KeycloakService) {
this.sortedApiData = this.apiData;
}
@@ -90,12 +94,12 @@ export class ApiListComponent implements OnChanges {
const gateway = gateways.find(g => g.id === apiVersion.gateways[0].gatewayId);
return this.buildViewData(contract, gateway, clientVersion, apiVersion);
}));
- }));
+ }))
/**
- * ngOnChanges is executed if the changes are applied to component
+ * We only need this to get called once because we don't have more than one developer
*/
- ngOnChanges(changes: SimpleChanges): void {
+ ngOnInit() {
this.loadingSpinnerService.startWaiting();
// subscribe for the private api data to fill the view
this.getDeveloperData(this.developerId).subscribe(data => {
@@ -103,9 +107,12 @@ export class ApiListComponent implements OnChanges {
this.sortedApiData = this.apiData;
this.loadingSpinnerService.stopWaiting();
}, error => {
+ // 404 is fine, we don't have any APIs to load
+ if (error.status !== 404) {
const errorMessage = 'Error loading api list';
console.error(errorMessage, error);
this.toasterService.pop('error', errorMessage, error.message);
+ }
this.loadingSpinnerService.stopWaiting();
});
}

MODIFY
src/app/components/developer/developer.component.html
src/app/components/developer/developer.component.html
-<div *ngFor="let developerId of developers">
- <h2 *ngIf="developers.length > 1">{{developerId}}</h2>
- <app-api-list [developerId]="developerId"></app-api-list>
-</div>
-
-<div id="noData" *ngIf="developers.length === 0">
- <p>No data available.</p>
+<div>
+ <h2>Your APIs</h2>
+ <app-api-list></app-api-list>
</div>

MODIFY
src/app/components/developer/developer.component.ts
src/app/components/developer/developer.component.ts
@@ -23,16 +23,6 @@ export class DeveloperComponent {
const keycloakInstance = keycloak.getKeycloakInstance();
// set token to token service
tokenService.setTokens(keycloakInstance.token, keycloakInstance.refreshToken);
- // get keycloak client roles of devportal
- // set ts-ignore here because our own property 'devportal' is not part of the type definition of the keycloak library
- // @ts-ignore
- const resourceAccess = keycloakInstance.tokenParsed.resource_access.devportal;
- console.log('read keycloak resourceAccess', resourceAccess);
- if (resourceAccess && resourceAccess.roles) {
- this.developers = resourceAccess.roles;
- } else {
- this.developers = [];
- }
}).catch(() => {
console.error('Failed to refresh token');
});

GW-335: Load API list depending on keycloak username