Skip to content

Instantly share code, notes, and snippets.

@pedromlcosta
Last active May 15, 2020 16:54
Show Gist options
  • Save pedromlcosta/acd310d7725e129b613b6b040caf05ec to your computer and use it in GitHub Desktop.
Save pedromlcosta/acd310d7725e129b613b6b040caf05ec to your computer and use it in GitHub Desktop.
Cursor Pagination Calls With Streams in Elixir
defp get_all_results() do
initial_url = "www.somewebsite.com/api/somerequest/someitems?limit=50"
get_results(initial_url)
end
def get_results(initial_url) do
Stream.unfold(initial_url, fn
nil ->
nil
url ->
case some_api_call(url) do
{:ok, response} ->
next_url = extract_next_url_from_response(response)
results_from_page = response["results"]
{results_from_page, next_url}
{:error, error} = error ->
{error, nil}
end
end)
|> Enum.to_list()
|> List.flatten()
|> Enum.find(results, {:ok, results}, fn
{:error, _error} -> true
_ -> false
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment