How to search multiple api services
I am developing a search engine with angular 2. So I am using the API from multiple platforms.
It works if I call the search function from each api manually.
But is it possible to do the same foreach api service?
Each api service has the same function:
search (query: string): Observable<Array<SearchResult>> { ... }
In the UI, I want to separate the results by tabs.
Therefore each api service has a header:
public title: string = "the title";
For storing search results locally, I have a class that is extended by each api service. This class has helper functions, etc.
source to share
Depending on your behavior, you can use merge , concat, or forkJoin to combine multiple threads into one.
The code will look something like the same.
For example, using merge to combine 2 threads into one.
Once you have a list of apis you need to call for a search. Your code will look like this.
let apis: string[] = [];
let observables = apis.map(api => search(api)); // get an array of observables
let merged = observables.reduce((previous, current) => previous.merge(current), new EmptyObservable()); // merge all obserbable in the list into one.
merged.subscribe(res => doSomething(res));
This article might be helpful .
source to share