Skip to main content

Stream.resource is great for indeterminate streams, like API pages. Build a request, then handle the next page until it's finished
def stargazers(%CodeRepo{owner: owner, name: name}) do
  Stream.resource(
    fn -> 
      Finch.build(:get, @base_url <> "/repos/#{owner}/#{repo}/stargazers", @default_headers)
    end,
    fn
      nil ->
        {:halt, nil}

      request ->
        handle_request(:get, request)
    end,
    fn _request -> nil end
  )
end

defp handle_request(verb, request) do
  case Finch.request(request, HTTP) do
    {:ok, %{body: body, status: status} = response} when status in @success ->
      {[response], next_page_request(verb, response)}

    {:ok, %{body: body, status: 429} = response} ->
      handle_rate_limit(verb, response)

    {_, response} ->
      {[response], nil}
  end
end
21 upvotes

© 2021 Zest Creative, LLC