I'm implementing an Angular service which calls a server for data. In every call I need to pass a token which lasts about a minute and then I need to map the response to get an specific field. So if my main call fails I need to call again for a token, wait for the response and then retry my first call, is there an easy way of doing this? Here are my two approaches (neither of them work propertly):
return this.http.post(url,
firstCallText(this.token), {
responseType: 'text',
headers
})
.pipe(
map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
return asJson["soap:Envelope"]["soap:Body"]["Response"]["#text"];
}),
catchError(async err=>{
await this.http.post(url,
getToken(),
{
responseType: 'text',
headers
}).pipe(map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
this.token = asJson["soap:Envelope"]["soap:Body"]["Response"]["Token"]["#text"];
})).toPromise()
return EMPTY
}),
retry()
)
This first method fails because the retry() gets called before the new token is received. My second approach:
return this.http.post(url,
firstCallText(this.token), {
responseType: 'text',
headers
})
.pipe(
map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
return asJson["soap:Envelope"]["soap:Body"]["Response"]["#text"];
}),
retryWhen((errors) =>{
this.http.post(url,
getToken(),
{
responseType: 'text',
headers
}).pipe(map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
this.token = asJson["soap:Envelope"]["soap:Body"]["Response"]["Token"]["#text"];
})).toPromise()
return EMPTY
})
)
The second one doesn´t retry correctly, I don't want to set a delay because the token call might be shorter or longer.