Skip to content

Commit

Permalink
#3 Created TokenInceport too add Headers too the request..
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Schneider authored and Peter Schneider committed Sep 11, 2020
1 parent acbbaca commit 6b4904c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
22 changes: 22 additions & 0 deletions src/app/TokenInterceptor.ts
Original file line number Diff line number Diff line change
@@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
'Ocp-Apim-Subscription-Key': '272101d76e92479c9762f658ecff0dc3'
}
});
return next.handle(request);
}
}
10 changes: 5 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
12 changes: 9 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';



Expand Down Expand Up @@ -46,7 +47,7 @@ const modules: any[] = [
];
@NgModule({
declarations: [
AppComponent
AppComponent,
],
imports: [
BrowserModule,
Expand All @@ -57,7 +58,12 @@ const modules: any[] = [
exports: [
modules
],
providers: [],
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
16 changes: 8 additions & 8 deletions src/app/http.service.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}
}

0 comments on commit 6b4904c

Please sign in to comment.