Initial commit
This commit is contained in:
8
lib/ex_data_gov_u_a/fetchers/base.ex
Normal file
8
lib/ex_data_gov_u_a/fetchers/base.ex
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule ExDataGovUA.Fetchers.Base do
|
||||
@type data :: map()
|
||||
@type status :: Atom.t()
|
||||
@type url :: String.t()
|
||||
@type method :: Atom.t()
|
||||
|
||||
@callback fetch(method, url) :: {status, data}
|
||||
end
|
||||
19
lib/ex_data_gov_u_a/fetchers/finch_fetcher.ex
Normal file
19
lib/ex_data_gov_u_a/fetchers/finch_fetcher.ex
Normal file
@@ -0,0 +1,19 @@
|
||||
defmodule ExDataGovUA.Fetchers.FinchFetcher do
|
||||
@behaviour ExDataGovUA.Fetchers.Base
|
||||
|
||||
@impl true
|
||||
def fetch(method, url) do
|
||||
start()
|
||||
with request <- Finch.build(method, url),
|
||||
{:ok, %Finch.Response{body: body}} <- Finch.request(request, MyFinchFetcher) do
|
||||
{:ok, body}
|
||||
end
|
||||
end
|
||||
|
||||
defp start() do
|
||||
case Finch.start_link(name: MyFinchFetcher) do
|
||||
{:ok, pid} -> pid
|
||||
{:error, {:already_started, pid}} -> pid
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user