-
-
Save samrose/3e9cfae527f6e46db252cb138ae1b77b to your computer and use it in GitHub Desktop.
Streaming HTTP requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Integrations.Skubana do | |
@host "..." | |
@headers [ ... ] | |
# returns a stream of shipments | |
def get_shipments do | |
# start with page 1 | |
start = fn -> 1 end | |
# create a stream, it will make HTTP requests until the page returned is empty | |
Stream.resource(start, &get_page/1, &Function.identity/1) | |
end | |
defp get_page(page) do | |
# build url | |
url = "#{@host}/v1/shipments?page=#{page}" | |
# make http request | |
{:ok, response} = Mojito.get(url, @headers) | |
# decode JSON | |
case Jason.decode!(response.body) do | |
# if empty array, halt the stream | |
[] -> {:halt, nil} | |
# otherwise, return data and move on to next page | |
shipments -> | |
{shipments, page+1} | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# paging is completely hidden from the caller's perspective | |
MyApp.Integrations.Skubana.get_shipments() | |
|> Stream.each(&Fulfillment.create_shipment/1) | |
|> Stream.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment