Skip to content

Commit

Permalink
Resolve config at runtime (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
anfly0 committed Aug 15, 2020
1 parent b7eddfb commit d3c18cd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 75 deletions.
39 changes: 20 additions & 19 deletions lib/ex_bank_id/auth.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
defmodule ExBankID.Auth do
@options [
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
personal_number: [
type: :string
# TODO: Add validator
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
defp options() do
[
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
personal_number: [
type: :string
# TODO: Add validator
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
]
]
]
@doc "Supported options:\n#{NimbleOptions.docs(@options)}"
end

def auth(ip_address, opts) when is_binary(ip_address) and is_list(opts) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %ExBankID.Auth.Payload{} <- ExBankID.Auth.Payload.new(ip_address, opts) do
ExBankID.HttpRequest.send_request(payload, opts)
end
Expand Down
35 changes: 18 additions & 17 deletions lib/ex_bank_id/cancel.ex
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
defmodule ExBankID.Cancel do
alias ExBankID.Cancel.Payload

@options [
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
defp options() do
[
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
]
]
]
@doc "Supported options:\n#{NimbleOptions.docs(@options)}"
end

def cancel(token, opts \\ [])

def cancel(token, opts) when is_binary(token) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
end
end

def cancel(token = %ExBankID.Auth.Response{}, opts) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
end
end

def cancel(token = %ExBankID.Sign.Response{}, opts) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
Expand Down
35 changes: 18 additions & 17 deletions lib/ex_bank_id/collect.ex
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
defmodule ExBankID.Collect do
alias ExBankID.Collect.Payload

@options [
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
defp options() do
[
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
]
]
]
@doc "Supported options:\n#{NimbleOptions.docs(@options)}"
end

def collect(token, opts \\ [])

def collect(token, opts) when is_binary(token) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
end
end

def collect(token = %ExBankID.Auth.Response{}, opts) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
end
end

def collect(token = %ExBankID.Sign.Response{}, opts) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
Expand Down
45 changes: 23 additions & 22 deletions lib/ex_bank_id/sign.ex
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
defmodule ExBankID.Sign do
alias ExBankID.Sign

@options [
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
personal_number: [
type: :string
# TODO: Add validator
],
user_non_visible_data: [
type: :string
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
defp options() do
[
url: [
type: :string,
default: Application.get_env(:ex_bank_id, :url, "https://appapi2.test.bankid.com/rp/v5.1/")
],
cert_file: [
type: :string,
default: Application.get_env(:ex_bank_id, :cert_file, __DIR__ <> "/../../assets/test.pem")
],
personal_number: [
type: :string
# TODO: Add validator
],
user_non_visible_data: [
type: :string
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default)
]
]
]
@doc "Supported options:\n#{NimbleOptions.docs(@options)}"
end

def sign(ip_address, user_visible_data, opts \\ [])
when is_binary(ip_address) and is_binary(user_visible_data) and is_list(opts) do
with {:ok, opts} <- NimbleOptions.validate(opts, @options),
with {:ok, opts} <- NimbleOptions.validate(opts, options()),
payload = %Sign.Payload{} <- Sign.Payload.new(ip_address, user_visible_data, opts) do
ExBankID.HttpRequest.send_request(payload, opts)
end
Expand Down

0 comments on commit d3c18cd

Please sign in to comment.