Update:
I extend Http class, when I deleteDocument()
I want handle error then getTicket()
then retry ma request deleteDocument()
with new this.TICKET
:
@Injectable()
export class HttpService extends Http {
public SERVER_URL: string = 'http://10.0.0.183:8080/alfresco/s/'
public TICKET: string = ''
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.post(url,body, options));
}
intercept(observable: Observable<Response>): Observable<Response> {
return observable.catch(initialError => {
if (initialError.status === 401) {
return this.getTicket()
.do(res => {
// HERE I WANT RETRY MY FIRST REQUEST
this.TICKET = res.json().data.ticket
})
} else {
return Observable.throw(initialError);
}
})
}
getTicket() {
return this.post(this.SERVER_URL + 'api/login', JSON.stringify({username: 'admin', password: 'admin'}))
}
deleteDocument(idFile: number) {
return this.post(this.SERVER_URL + 'dms/file/delfile?idfile=' + idFile + '&alf_ticket=' + this.TICKET, {}).map((data: Response) => data.json())
}
}
After receive my new ticket, I want retry my first request with new url. Thanks
I don't know if there's any easier way but I'd use
concatMap()
operator and recursively calldeleteDocument()
with the the id returned from the previous call:This should simulate your situation:
See live demo: http://plnkr.co/edit/rUbLgEwtw7lSBueKJ5HN?p=preview
This prints to console:
I'm simulating the error with
willFail
argument. OperatorconcatMap
server here to distinguish between an error and a correct response. I'm also passing along the originalid
and to some processing with just for demonstration purposes.In your case, you'd append to
intercept()
also all parameters necessary to reconstruct the original HTTP request and instead ofthis.getTicket().do()
use:Operator
do()
is mostly useful to just debug what's going on in your operator chains, it doesn't modify the chain in any way.I don't know what your code actually does but I hope this makes sense to you.
Maybe a similar question to yours: Observable Continue calling API and changing parameters based on condition