获取新票证,然后重试第一个请求 [英] get new ticket then retry first request
问题描述
我扩展了Http类,当我deleteDocument()
我想要处理错误然后getTicket()
时,然后使用新的this.TICKET
重试请求deleteDocument()
:
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 call deleteDocument()
with the the id returned from the previous call:
这应该可以模拟您的情况:
This should simulate your situation:
import {Observable} from 'rxjs';
function deleteDocument(willFail: bool, id) {
return Observable.of("I'm your response: " + id)
// process response and eventualy throw error
.concatMap(val => {
if (willFail) {
return Observable.throw({response: val, message: "It's broken"});
} else {
return Observable.of(val);
}
})
// recover from the error
.catch(initialError => {
return deleteDocument(false, initialError.response.length);
});
}
deleteDocument(true, 42).subscribe(result => console.log(result));
观看现场演示: http://plnkr.co/edit/rUbLgEwtw7lSBueKJ5HN?p=preview
这将打印到控制台:
I'm your response: 21
我正在使用willFail
参数模拟错误.此处的操作员concatMap
服务器在错误和正确响应之间进行区分.我还传递了原始的id
并进行了一些处理,仅用于演示目的.
I'm simulating the error with willFail
argument. Operator concatMap
server here to distinguish between an error and a correct response. I'm also passing along the original id
and to some processing with just for demonstration purposes.
在您的情况下,您还将在intercept()
后面附加重构原始HTTP请求所需的所有参数,而不是使用this.getTicket().do()
:
In your case, you'd append to intercept()
also all parameters necessary to reconstruct the original HTTP request and instead of this.getTicket().do()
use:
return this.getTicket().concatMap(response => {
var newBody = {id: response.ticketId};
return this.post(originalUrl, newBody, ...);
});
操作员do()
主要用于调试操作员链中正在发生的事情,它不会以任何方式修改链.
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.
也许与您的问题类似:可观察的继续调用API并根据条件更改参数
Maybe a similar question to yours: Observable Continue calling API and changing parameters based on condition
这篇关于获取新票证,然后重试第一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!