Skip to content

Instantly share code, notes, and snippets.

@pedromlcosta
Created May 15, 2020 17:03
Show Gist options
  • Save pedromlcosta/9f3d3b0f532f82002ebf3f9cc2d04af0 to your computer and use it in GitHub Desktop.
Save pedromlcosta/9f3d3b0f532f82002ebf3f9cc2d04af0 to your computer and use it in GitHub Desktop.
defp get_all_results() do
initial_url = "www.somewebsite.com/api/somerequest/someitems?limit=50"
get_results(initial_url)
end
def get_results(nil, accumulated_results \\ []) do
# No next URL, return the accumulated results so far
{:ok, accumulated_results}
end
def get_results(url, accumulated_results \\ []) do
case some_api_call(url) do
{:ok, response} ->
next_url = extract_next_url_from_response(response)
results_from_page = response["results"]
get_results(next_url, accumulated_results ++ results_from_page)
{:error, error} ->
# *** You decide how to handle it here and what to return ***
{:error, error_reason}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment