I am currently attempting to achieve the following:
- Initiate a stop request using a stored ID in the URL.
- Send a request with a new ID in the URL and save this new ID.
- If the response code is 200, proceed as normal. If it is 204, repeat steps 1 and 2 with a new ID.
Ideally, I would like to implement this without using multiple subscribe() calls, but rather utilize RxJS's pipe for most of the operations.
The existing code structure resembles something like this:public doStuff(): Observable<string> {
if (this.id) {
return this.httpClient.get(`${STOP_URL}/${this.id}`).pipe(
tap(() => {
this.id = Date.now();
}),
mergeMap(() => this.httpClient.get(`${START_URL}/${this.id}`))
);
} else {/* similar logic without the stop */}
}
In the component where this function is employed:
this.service
.doStuff()
.pipe(
// Further implementation needed here to handle response code 204
mergeMap(result => this.httpClient.get(/* extract relevant data from the response */))
).subscribe(foo => bar())
I am seeking assistance on how to address the "TODO" section appropriately so that it can dynamically react to different response codes while also ensuring the generation of new IDs for subsequent requests. I understand that handling 204 responses poses challenges, as typical retry methods may not be directly applicable due to specific conditions regarding status codes.