neurosnap
neurosnap

Reputation: 5798

Cycle.js Nested HTTP Requests

I'm trying to send multiple HTTP requests using the cycle-http driver. The first request in my example grabs a list of mailboxes and out of that response are multiple mailboxes I have to send HTTP requests for subsequent threads.

Get all mailbox IDs HTTP request -> Get all mail from each mailbox HTTP request.

I'm pretty stumped at this point, all the examples I have seen are simple single requests where the url is known prior to generating the observable. So not only am I confused about nesting HTTP requests, but I also am not sure how to "know" the URL for a filter after the request has been made since the URLs are generated dynamically.

Any help would be greatly appreciated.

Upvotes: 1

Views: 355

Answers (1)

WHITECOLOR
WHITECOLOR

Reputation: 26142

We can assume we already have a stream representing the results from the first "level" of HTTP requests. We can simply map over these results and transform them to new HTTP requests:

// this should be sent to HTTP driver
let mailboxDetailsRequests$ = mailboxes$.map(mailboxes => {
  // map all mailboxes to requests sequence
  return Observable.fromArray(mailboxes.map(mailbox => ({
    url: 'get/mailbox/url/' + mailbox.name,
    mailbox: mailbox.name
  }))
}).flatMap(_ => _)

Now we can access these HTTP responses by filtering on the mailbox property of the request (or you can use some sort of distinguishing feature that separates these requests from other types):

// and now you can get the responses by filtering the HTTP source
let mailboxResult$ = HTTP.filter(r$$ => r$$.request.mailbox).switch();

Upvotes: 3

Related Questions