From 6b4904cdc0f322339d6f9e1bfea2a7016a4a165f Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Fri, 11 Sep 2020 13:01:23 +0200 Subject: [PATCH] #3 Created TokenInceport too add Headers too the request.. --- src/app/TokenInterceptor.ts | 22 ++++++++++++++++++++++ src/app/app.component.ts | 10 +++++----- src/app/app.module.ts | 12 +++++++++--- src/app/http.service.ts | 16 ++++++++-------- 4 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 src/app/TokenInterceptor.ts diff --git a/src/app/TokenInterceptor.ts b/src/app/TokenInterceptor.ts new file mode 100644 index 0000000..e1cc1e1 --- /dev/null +++ b/src/app/TokenInterceptor.ts @@ -0,0 +1,22 @@ +import { Injectable } from '@angular/core'; +import { + HttpRequest, + HttpHandler, + HttpEvent, + HttpInterceptor +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +@Injectable() +export class TokenInterceptor implements HttpInterceptor { + + constructor() {} + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + request = request.clone({ + setHeaders: { + 'Ocp-Apim-Subscription-Key': '272101d76e92479c9762f658ecff0dc3' + } + }); + return next.handle(request); + } +} \ No newline at end of file diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 15df814..03abbcb 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -65,13 +65,13 @@ export class AppComponent { displayData: string[]; dataSource = this.displayData; - constructor(private service: HttpService) { } + constructor(private service: HttpService) { } - ngOnInit() { - this.elements = this.service.getCharacters(); + async ngOnInit() { + this.elements = await this.service.getCharacters(); console.log(this.elements); - this.jsonStringArray = Object.keys(this.elements).map(e => this.elements[e]); - this.displayedColumns = ['position', 'name', 'weight', 'symbol']; + // this.jsonStringArray = Object.keys(this.elements).map(e => this.elements[e]); + // this.displayedColumns = ['position', 'name', 'weight', 'symbol']; console.log(this.jsonStringArray); // for (let index = 0; index < this.jsonStringArray.length; index++) { // const val = Object.keys(this.jsonStringArray).map(index => this.jsonStringArray[index]); diff --git a/src/app/app.module.ts b/src/app/app.module.ts index cf5eb80..b1a6c33 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -18,7 +18,8 @@ import { MatSliderModule } from '@angular/material/slider'; import { MatSlideToggleModule } from '@angular/material/slide-toggle'; import { MatNativeDateModule, MatIconModule, MatToolbarModule, MatTableModule } from '@angular/material'; -import { HttpClientModule } from '@angular/common/http'; +import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; +import {TokenInterceptor} from './TokenInterceptor'; @@ -46,7 +47,7 @@ const modules: any[] = [ ]; @NgModule({ declarations: [ - AppComponent + AppComponent, ], imports: [ BrowserModule, @@ -57,7 +58,12 @@ const modules: any[] = [ exports: [ modules ], - providers: [], + providers: [{ + provide: HTTP_INTERCEPTORS, + useClass: TokenInterceptor, + multi: true + } + ], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/http.service.ts b/src/app/http.service.ts index 3587742..af9aed0 100644 --- a/src/app/http.service.ts +++ b/src/app/http.service.ts @@ -1,22 +1,22 @@ import { Injectable } from '@angular/core'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; - - -const headers = new HttpHeaders() - .set('Ocp-Apim-Subscription-Key', '272101d76e92479c9762f658ecff0dc3'); +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) + export class HttpService { constructor(private http: HttpClient) { } + url = 'https://preview-demo-mm.azure-api.net/private/api/management/apis?'; - getCharacters() { - return this + async getCharacters() { + await this .http - .get(`${this.url}`, { headers }); + .get(this.url).subscribe(data => { + return data; + }); } }