I'm struggling to figure out how to execute a function from retryWhen and then call the parent function once retryWhen is done. Any ideas on how I can achieve this?
getStatuses(statusesType: string[]): Observable<IStatus[]> {
let body = JSON.stringify({ "StatusesType": statusesType});
let headers = new Headers({ 'Content-Type': 'application/json' });
headers.append('Authorization', 'Bearer ' + localStorage.getItem("access_token");
headers.append('Access-Control-Allow-Origin', '*');
let options = new RequestOptions({ headers: headers });
return this.http.post(this._baseUrl + '/statuses/statusesList', body, options)
.retryWhen(error => this.refreshToken())
.map((res: Response) => {
this.statusesrecieved = res.json();
return this.inspections;
});
}
I need to have getStatuses() function called again after executing refreshToken() in retryWhen. Any help would be greatly appreciated.
I also attempted using retry like this but it didn't work:
return this.http.post(this._baseUrl + '/statuses/statusesList', body, options)
.retryWhen(error => this.refreshToken())
.map((res: Response) => {
this.statusesrecieved = res.json();
return this.inspections;
}).retry(5);
}
Below is my refreshToken function
refreshToken(): Observable<any> {
console.log("refreshing token");
let body: string = 'grant_type=refresh_token&refresh_token=' + localStorage.getItem("refresh_token");
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
return Observable.create(
(observer: Observer<any>) => {
this.http.post('https://fedqa.test.com/as/token.oauth2', body, options)
.map(res => res.json()).subscribe(
(data) => {
localStorage.removeItem("access_token");
localStorage.removeItem("refresh_token");
localStorage.setItem("access_token", data.access_token);
localStorage.setItem("refresh_token", data.refresh_token);
localStorage.setItem("token_type", data.token_type);
localStorage.setItem("expires_in", data.expires_in);
},
(error) => {
Observable.throw(error);
}
);
});
}