How can I traverse through sequential API calls which is in a linked list structure? Right now I'm making a request each 5 seconds where I get OrdersList
data. This object contains a field results
with an array of Order
objects and it also contains a next
field with a request URL to access the next array of orders since they are not sent all at once.
My orders.component.ts
:
ngOnInit(): void {
this.subscription = timer(0, 5000).pipe(
switchMap(() => this.apiManager.fetchShopOrders())
).subscribe(orders => {
const loadedOrders: OrdersList = orders;
/* not working - try to get next set(s) of orders */
if(loadedOrders.next != null){
while(loadedOrders.next != null){
let response = this.apiManager.fetchNextSetOfOrders(loadedOrders.next).toPromise();
response.then((nextSetOrders) => {
const loadedNextSetOrders: OrdersList = nextSetOrders;
loadedOrders.next = loadedNextSetOrders.next;
loadedOrders.results = [...loadedOrders.results, ...loadedNextSetOrders.results];
})
}
}
...
/* do stuff with the orders */
...
});
}
My ApiManagerService
:
@Injectable({ providedIn: 'root' })
export class ApiManagerService {
private API_KEY = "...";
private API_SECRET = "...";
private token = "...";
private webzine_id: string = "...";
constructor(private http: HttpClient) {}
fetchShopOrders() {
const url = "https://commerce.ww-api.com/commerceapi/v1/order/" + this.webzine_id + "/order/?per_page=10";
return this.http
.get<OrdersList>(url, {
headers: new HttpHeaders({
"API-KEY": this.API_KEY,
"API-SECRET": this.API_SECRET
}),
});
}
fetchNextSetOfOrders(nextSetUrl: string) {
return this.http
.get<OrdersList>(nextSetUrl, {
headers: new HttpHeaders({
"API-KEY": this.API_KEY,
"API-SECRET": this.API_SECRET
}),
});
}
}
How can I make it possible to sequentially add the next set of orders to my OrdersList
object?
Thanks in advance.