Current section
Files
Jump to
Current section
Files
lib/gusex/client.ex
defmodule Gusex.Client do
@moduledoc """
Client module for Gusex, providing actual implementation of functions
required to interact with the BIR service. While it is of course possible
to use these functions directly, it is recommended to use convenience delegates
provided by the main `Gusex` module.
"""
alias Gusex.{Soap, Parse}
alias Gusex.Types.{ParametryWyszukiwania, Session}
@spec zaloguj(String.t() | nil, :test | :production | nil) :: {:ok, Session.t()} | {:error, any()}
def zaloguj(user_key \\ nil, environment \\ nil) do
api_key = user_key || Application.get_env(:gusex, :api_key)
env = environment || Application.get_env(:gusex, :environment, :test)
endpoint_url = endpoint_url(env)
if is_nil(api_key) do
{:error, :api_key_missing}
else
Soap.zaloguj(api_key, endpoint_url)
|> send_request(build_headers(), endpoint_url(env))
|> Parse.zaloguj_response()
|> case do
{:ok, session_id} ->
{:ok, %Session{id: session_id, environment: env, endpoint_url: endpoint_url}}
{:error, :session_id_not_found} ->
{:error, :login_failure}
{:error, reason} ->
{:error, reason}
end
end
end
@spec dane_szukaj_podmioty(ParametryWyszukiwania.t() | map(), Session.t()) :: {:ok, list()} | {:error, any()}
def dane_szukaj_podmioty(%ParametryWyszukiwania{} = search_params, %Session{} = session) do
Soap.dane_szukaj_podmioty(search_params, session.endpoint_url)
|> send_request(session)
|> Parse.dane_response("SzukajPodmioty")
|> case do
{:ok, data} -> {:ok, type_encapsulate(data, "SzukajPodmioty")}
{:error, error_data} -> {:error, error_data}
end
end
def dane_szukaj_podmioty(%{} = search_params, %Session{} = session) do
try do
dane_szukaj_podmioty(struct!(ParametryWyszukiwania, search_params), session)
rescue
error in KeyError -> {:error, {:invalid_search_params, Exception.message(error)}}
end
end
@spec dane_pobierz_pelny_raport(String.t(), String.t(), Session.t()) :: {:ok, list()} | {:error, any()}
def dane_pobierz_pelny_raport(regon, report_name, %Session{} = session) do
Soap.dane_pobierz_pelny_raport(regon, report_name, session.endpoint_url)
|> send_request(session)
|> Parse.dane_response("PobierzPelnyRaport")
# |> case do
# {:ok, data} -> {:ok, type_encapsulate(data, report_name)}
# {:error, error_data} -> {:error, error_data}
# end
end
@spec dane_pobierz_raport_zbiorczy(String.t(), String.t(), Session.t()) :: {:ok, list()} | {:error, any()}
def dane_pobierz_raport_zbiorczy(report_date, report_name, %Session{} = session) do
Soap.dane_pobierz_raport_zbiorczy(report_date, report_name, session.endpoint_url)
|> send_request(session)
|> Parse.dane_response("PobierzRaportZbiorczy")
|> case do
{:ok, data} -> {:ok, type_encapsulate(data, report_name)}
{:error, error_data} -> {:error, error_data}
end
end
@spec get_value(String.t(), Session.t() | nil) :: {:ok, String.t()} | {:error, any()}
def get_value("KomunikatUslugi" = param_name, nil) do
endpoint_url = endpoint_url(Application.get_env(:gusex, :environment, :test))
Soap.get_value(param_name, endpoint_url)
|> send_request(build_headers(), endpoint_url)
|> Parse.get_value_response()
end
def get_value(param_name, %Session{} = session) do
Soap.get_value(param_name, session.endpoint_url)
|> send_request(session)
|> Parse.get_value_response()
end
@spec wyloguj(Session.t()) :: {:ok, boolean()} | {:error, any()}
def wyloguj(%Session{} = session) do
Soap.wyloguj(session.id, session.endpoint_url)
|> send_request(session)
|> Parse.wyloguj_response()
end
def wyloguj(_), do: {:error, :invalid_session}
defp send_request(body, %Session{} = session), do: send_request(body, build_headers(session.id), session.endpoint_url)
defp send_request(body, headers, url) do
case HTTPoison.post(url, body, headers, recv_timeout: 30_000) do
{:ok, %HTTPoison.Response{status_code: 200, body: response_body}} ->
{:ok, response_body}
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} ->
{:error, {:http_error, status_code, body}}
{:error, %HTTPoison.Error{reason: reason}} ->
{:error, {:http_client_error, reason}}
end
end
defp build_headers(session_id \\ nil) do
headers = [
{"Content-Type", "application/soap+xml; charset=utf-8"},
# {"Accept", "application/soap+xml"},
{"Accept", "application/xop+xml"},
]
if session_id do
[{"sid", session_id} | headers]
else
headers
end
end
@method_types %{
"BIR11AktualizowaneJednostkiLokalne" => Gusex.Types.AktualizowaneJednostkiLokalne,
"BIR11AktualizowanePodmiotyPrawneOrazDzialalnosciOsFizycznych" => Gusex.Types.AktualizowanePodmiotyPrawneOrazDzialalnosciOsFizycznych,
"BIR11NoweJednostkiLokalne" => Gusex.Types.NoweJednostkiLokalne,
"BIR11NowePodmiotyPrawneOrazDzialalnosciOsFizycznych" => Gusex.Types.NowePodmiotyPrawneOrazDzialalnosciOsFizycznych,
"BIR11SkresloneJednostkiLokalne" => Gusex.Types.SkresloneJednostkiLokalne,
"BIR11SkreslonePodmiotyPrawneOrazDzialalnosciOsFizycznych" => Gusex.Types.SkreslonePodmiotyPrawneOrazDzialalnosciOsFizycznych,
"BIR12JednLokalnaOsFizycznej" => Gusex.Types.JednLokalnaOsFizycznej,
"BIR12JednLokalnaOsFizycznejPkd" => Gusex.Types.JednLokalnaOsFizycznejPkd,
"BIR12JednLokalnaOsPrawnej" => Gusex.Types.JednLokalnaOsPrawnej,
"BIR12JednLokalnaOsPrawnejPkd" => Gusex.Types.JednLokalnaOsPrawnejPkd,
"BIR12OsFizycznaDaneOgolne" => Gusex.Types.OsFizycznaDaneOgolne,
"BIR12OsFizycznaDzialalnoscCeidg" => Gusex.Types.OsFizycznaDzialalnoscCeidg,
"BIR12OsFizycznaDzialalnoscPozostala" => Gusex.Types.OsFizycznaDzialalnoscPozostala,
"BIR12OsFizycznaDzialalnoscRolnicza" => Gusex.Types.OsFizycznaDzialalnoscRolnicza,
"BIR12OsFizycznaDzialalnoscSkreslonaDo20141108" => Gusex.Types.OsFizycznaDzialalnoscSkreslonaDo20141108,
"BIR12OsFizycznaListaJednLokalnych" => Gusex.Types.OsFizycznaListaJednLokalnych,
"BIR12OsFizycznaPkd" => Gusex.Types.OsFizycznaPkd,
"BIR12OsPrawna" => Gusex.Types.OsPrawna,
"BIR12OsPrawnaListaJednLokalnych" => Gusex.Types.OsPrawnaListaJednLokalnych,
"BIR12OsPrawnaPkd" => Gusex.Types.OsPrawnaPkd,
"BIR12OsPrawnaSpCywilnaWspolnicy" => Gusex.Types.OsPrawnaSpCywilnaWspolnicy,
"SzukajPodmioty" => Gusex.Types.Podmiot,
}
defp type_encapsulate(data, method) when is_list(data) do
case @method_types[method] do
nil -> data
struct_module -> Enum.map(data, &struct(struct_module, &1))
end
end
defp endpoint_url(:test), do: "https://wyszukiwarkaregontest.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc"
defp endpoint_url(:production), do: "https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc"
defp endpoint_url(_), do: (raise ArgumentError, "Invalid environment. Use :test or :production.")
end