Last active
May 15, 2020 16:54
-
-
Save pedromlcosta/acd310d7725e129b613b6b040caf05ec to your computer and use it in GitHub Desktop.
Cursor Pagination Calls With Streams in Elixir
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
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