I am looking to send get requests to multiple endpoints simultaneously, but I want to collect all the responses at once. Currently, this is how a single endpoint request is handled:
public getTasks(): Observable<any> {
this.logger.info('TasksService: getTasks()');
const endpoint = `${this.processEngineUriPrefix}runtime/tasks`;
// https://www.flowable.org/docs/userguide/index.html#_request_parameters
const sort = 'createTime';
const order = 'asc'; // 'desc'
// const start = 0;
// const size = 16;
const params = new HttpParams().set('sort', sort).set('order', order);
return this.httpClient.get<TaskListModel>(endpoint, this.getHttpOptions(params)).pipe(
tap(() => {
this.logger.info('TasksService: getTasks() completed');
}),
catchError(error => {
this.logger.info('TasksService: getTasks() -> catchError()');
if (error === undefined) {
error = new Error(HTTP_SERVER_ERROR_CONNECTION_REFUSED);
throw error;
} else {
return this.handleError('Get tasks', []);
// return throwError(error);
}
})
);
}
To achieve the desired result, the proxy.conf configuration looks like this:
"/process-api-0": {
"target": "http://localhost:8084",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/process-api-0": "/process-api/"
},
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic Zmxvd2FibGUtcmVzdDp0ZXN0"
}
},
"/process-api-1": {
"target": "http://localhost:8088",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/process-api-1": "/process-api/"
},
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic Zmxvd2FibGUtcmVzdDp0ZXN0"
}
},
My initial approach was to loop through the endpoints, however, I can't seem to find a way to aggregate and return the results:
Note: The end goal is to be able to accommodate more endpoints later on.
const list = [0, 1];
for (const i in list) {
const endpoint = `${this.processEngineUriPrefix}` + i + `/runtime/tasks`;
this.httpClient.get<TaskListModel>(endpoint, this.getHttpOptions(params)).pipe(
tap(() => {
this.logger.info('TasksService: getTasks() completed');
}),
catchError(error => {
this.logger.info('TasksService: getTasks() -> catchError()');
if (error === undefined) {
error = new Error(HTTP_SERVER_ERROR_CONNECTION_REFUSED);
throw error;
} else {
return this.handleError('Get tasks', []);
// return throwError(error);
}
}))
};
return _________;
}
Although I came across this post, as a newcomer to this field, I'm unsure if it's applicable to my situation or how to adapt it: How to recursively perform an HTTP request in angular?