Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pedromlcosta/c76813342c345ba2dcf0a7364d0b7945 to your computer and use it in GitHub Desktop.
Save pedromlcosta/c76813342c345ba2dcf0a7364d0b7945 to your computer and use it in GitHub Desktop.
def get_shopify_products_cursor_based(project_id, api_token, store_name) do
base_url = "www.derp.com/some_listing?limit=50"
results =
Stream.unfold(base_url, fn
nil ->
nil
acc ->
response = HTTPoison.get(acc, [], [])
result =
with {:ok, %HTTPoison.Response{status_code: 200, body: body, headers: headers}} <-
response,
{:ok, product_list} <- Poison.decode(body) do
{:ok, %{products: product_list["products"], next: next_link}}
else
{:ok, %HTTPoison.Response{status_code: 404, body: _body}} ->
{:error, :invalid_shopify_store}
_response ->
{:error, :unexpected}
end
case result do
{:ok, %{products: product_list, next: value}} ->
{product_list, value}
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