Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search multiple api services

I am developing a search engine with angular 2. Therefore I use APIs from multiple platforms.

It works if I call the search function from every api service manually.

But is it possible to do the same foreach api service?

Every api service has the same function:

search (query: string): Observable<Array<SearchResult>> { ... }

In the UI I want to separate the results by tabs.

Therefore every api service has a title:

public title: string = "the title";

For storing the search results locally I have a class that is extended by every api service. This class has helper functions etc.

like image 392
Tobias Etter Avatar asked Feb 03 '26 02:02

Tobias Etter


1 Answers

Depending on the behaviour you need you can use merge, concat or forkJoin to merge multiple streams into one.

The code would look pretty much the same.

For example using merge in order to merge 2 streams into one.

If you have a list of apis you need to call for the search. Your code would 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.

like image 78
toskv Avatar answered Feb 04 '26 14:02

toskv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!