Created
April 22, 2018 13:04
-
-
Save ShankarSumanth/54d5bb1f16c2e5f6f95ac843cb42e556 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from 'rxjs/Observable'; | |
import { KeycloakApiService } from '../keycloak/keycloak-api.service'; | |
import { HttpClient, HttpHeaders } from '@angular/common/http'; | |
import { Injectable } from '@angular/core'; | |
@Injectable() | |
export class CustomHttp { | |
private apiBaseUrl = `Your API Endpoint`; | |
constructor(public httpClient: HttpClient, public keycloakAPi: KeycloakApiService) { } | |
get<T>(url: string): Observable<T> { | |
return this.keycloakAPi.updateToken() | |
.map(token => this.createHeaders(token)) | |
.flatMap(headers => this.httpClient.get<T>(this.getUrl(url), { headers })); | |
} | |
put<T>(url: string, data: any): Observable<T> { | |
return this.keycloakAPi.updateToken() | |
.map(token => this.createHeaders(token)) | |
.flatMap(headers => this.httpClient.put<T>(this.getUrl(url), data, { headers })); | |
} | |
private getUrl(url: string) { | |
return `${this.apiBaseUrl}${url}`; | |
} | |
private createHeaders(token: string): HttpHeaders { | |
return new HttpHeaders({ | |
'Content-Type': 'application/json', | |
'Accept': 'application/json', | |
'Authorization': 'Bearer ' + token | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment