/
collect.ex
58 lines (53 loc) · 1.8 KB
/
collect.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
defmodule ExBankID.Collect do
alias ExBankID.Collect.Payload
def 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"),
doc:
"If no certificate path is specified, the publicly available test certificate will be used."
],
http_client: [
type: :atom,
default: Application.get_env(:ex_bank_id, :http_client, ExBankID.Http.Default),
doc:
"Specify a custom http client. Should be a module that implements ExBankID.Http.Client."
],
json_handler: [
type: :atom,
default: Application.get_env(:ex_bank_id, :json_handler, ExBankID.Json.Default),
doc:
"Specify a custom json handler. Should be a module that implements ExBankID.Json.Handler."
]
]
end
def collect(token, opts \\ [])
def collect(token, opts) when is_binary(token) do
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()),
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()),
payload = %Payload{} <-
Payload.new(token) do
ExBankID.HttpRequest.send_request(payload, opts)
end
end
end