Add the json fetchers to scrape the API.
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-03-18 18:34:43 +02:00
parent 52ff683c2d
commit 57ead52ba2
4 changed files with 87 additions and 0 deletions
@@ -0,0 +1,13 @@
defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJson do
def get(url) do
case Req.get(url) do
{:ok, data} ->
if data.status == 200 do
{:ok, data.body}
else
{:error, data}
end
{:error, message} -> {:error, message}
end
end
end
@@ -0,0 +1,35 @@
defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsons do
alias DecentralisedBookIndex.Sync.ApiClients.FetchJson
def get(url) do
case FetchJson.get(url) do
{:ok, data} ->
[]
{:error, message} -> {:error, message}
end
query_results =
Stream.resource(
# Initial state:
fn -> url end,
fn
# Stop the stream if no more data
next when is_nil(next) ->
{:halt, nil}
next ->
case FetchJson.get(next) do
{:ok, page} ->
{page["data"], get_in(page, ["links", "next"])}
{:error, message} ->
Logger.error("FetchJsons error: #{inspect(message)}")
{[], nil}
end
end,
fn _ -> :ok end
)
|> Enum.to_list()
{:ok, query_results}
end
end