Skip to content

Commit

Permalink
Merge branch 'master' into add-errors-to-aiar-struct
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbr authored Oct 10, 2024
2 parents 85effc0 + 8a21726 commit 1817067
Show file tree
Hide file tree
Showing 48 changed files with 4,254 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Visit our [API docs](https://documentation.ibanity.com/xs2a/1/api/elixir).
In the `mix.exs` file:
```elixir
def deps do
[{:ibanity, "~> 0.11.0}]
[{:ibanity, "~> 0.11.0"}]
end
```

Expand Down
222 changes: 222 additions & 0 deletions lib/ibanity/api/ponto_connect/account.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
defmodule Ibanity.PontoConnect.Account do
@moduledoc """
[Account API wrapper](https://documentation.ibanity.com/ponto-connect/2/api#account)
"""

use Ibanity.Resource

alias Ibanity.PontoConnect

@api_schema_path ["ponto-connect", "accounts"]

defstruct [
:id,
:description,
:deprecated,
:product,
:reference,
:currency,
:subtype,
:available_balance,
:available_balance_changed_at,
:available_balance_reference_date,
:current_balance,
:current_balance_changed_at,
:current_balance_reference_date,
:holder_name,
:reference_type,
:authorization_expiration_expected_at,
:authorized_at,
:available_balance_variation_observed_at,
:current_balance_variation_observed_at,
:internal_reference,
:availability,
:latest_synchronization,
:synchronized_at
]

@doc """
[List accounts](https://documentation.ibanity.com/ponto-connect/2/api#list-accounts)
Takes a `Ibanity.PontoConnect.Token`, or a `Ibanity.Request` with set `:token` as argument.
## Examples
iex> Ibanity.PontoConnect.Account.list(token)
{:ok, %Ibanity.Collection{
items: [%Ibanity.PontoConnect.Account{}]
}}
iex> token |> Ibanity.Request.token() |> Ibanity.PontoConnect.Accounts.list()
{:ok, %Ibanity.Collection{
items: [%Ibanity.PontoConnect.Account{}]
}}
iex> invalid_token |> Ibanity.PontoConnect.Account.list()
{:error,
[
%{
"code" => "invalidAccessToken",
"detail" => "Your access token is invalid.",
"meta" => %{"requestId" => "00077F000001D3A87F0000011F4066E43AFD1900051"}
}
]}
"""
def list(%Request{token: token} = request_or_token)
when not is_nil(token) do
request_or_token
|> Request.id(:id, "")
|> Client.execute(:get, @api_schema_path, __MODULE__)
end

def list(%PontoConnect.Token{} = request_or_token) do
request_or_token
|> Request.token()
|> list()
end

def list(other) do
raise ArgumentError,
message: PontoConnect.Exceptions.token_argument_error_msg("Accounts", other)
end

@doc """
[Find Account by id](https://documentation.ibanity.com/ponto-connect/2/api#get-account)
Takes a `Ibanity.PontoConnect.Token`, or a `Ibanity.Request` with set `:token` as first argument, and a Account
ID as second argument.
## Examples
With token
iex> Ibanity.PontoConnect.Account.find(token, "953934eb-229a-4fd2-8675-07794078cc7d")
{:ok, %Ibanity.PontoConnect.Account{id: "953934eb-229a-4fd2-8675-07794078cc7d"}}
With request
iex> token
...> |> Ibanity.Request.token()
...> |> Ibanity.Request.application(:my_application)
...> |> Ibanity.PontoConnect.Account.find("953934eb-229a-4fd2-8675-07794078cc7d")
{:ok, %Ibanity.PontoConnect.Account{id: "953934eb-229a-4fd2-8675-07794078cc7d"}}
Error
iex> Ibanity.PontoConnect.Account.find(token, "does-not-exist")
{:error,
[
%{
"code" => "resourceNotFound",
"detail" => "The requested resource was not found.",
"meta" => %{
"requestId" => "00077F00000184847F0000011F4066E44223327005A",
"resource" => "account"
}
}
]}
"""
def find(%Request{token: token} = request_or_token, id)
when not is_nil(token) do
request_or_token
|> Request.id(id)
|> Client.execute(:get, @api_schema_path, __MODULE__)
end

def find(%PontoConnect.Token{} = request_or_token, id) do
request_or_token
|> Request.token()
|> find(id)
end

def find(other, _id) do
raise ArgumentError,
message: PontoConnect.Exceptions.token_argument_error_msg("Account", other)
end

@doc """
[Revoke Account by id](https://documentation.ibanity.com/ponto-connect/2/api#revoke-account)
## Examples
With token
iex> Ibanity.PontoConnect.Account.delete(token, "953934eb-229a-4fd2-8675-07794078cc7d")
{:ok, %Ibanity.PontoConnect.Account{id: "953934eb-229a-4fd2-8675-07794078cc7d"}}
With request
iex> token
...> |> Ibanity.Request.token()
...> |> Ibanity.Request.application(:my_application)
...> |> Ibanity.PontoConnect.Account.delete("953934eb-229a-4fd2-8675-07794078cc7d")
{:ok, %Ibanity.PontoConnect.Account{id: "953934eb-229a-4fd2-8675-07794078cc7d"}}
Error
iex> Ibanity.PontoConnect.Account.delete(token, "does-not-exist")
{:error,
[
%{
"code" => "resourceNotFound",
"detail" => "The requested resource was not found.",
"meta" => %{
"requestId" => "00077F00000184847F0000011F4066E44223327005A",
"resource" => "account"
}
}
]}
"""
def delete(%Request{token: token} = request_or_token, id)
when not is_nil(token) do
formatted_ids = PontoConnect.RequestUtils.format_ids(%{id: id})

request_or_token
|> Request.ids(formatted_ids)
|> Client.execute(:delete, @api_schema_path, __MODULE__)
end

def delete(%PontoConnect.Token{} = request_or_token, id) do
request_or_token
|> Request.token()
|> delete(id)
end

def delete(other, _id) do
raise ArgumentError,
message: PontoConnect.Exceptions.token_argument_error_msg("Account", other)
end

@doc false
def key_mapping do
[
id: {~w(id), :string},
description: {~w(attributes description), :string},
deprecated: {~w(attributes deprecated), :boolean},
product: {~w(attributes product), :string},
reference: {~w(attributes reference), :string},
currency: {~w(attributes currency), :string},
subtype: {~w(attributes subtype), :string},
available_balance: {~w(attributes availableBalance), :number},
available_balance_changed_at: {~w(attributes availableBalanceChangedAt), :datetime},
available_balance_reference_date: {~w(attributes availableBalanceReferenceDate), :datetime},
current_balance: {~w(attributes currentBalance), :number},
current_balance_changed_at: {~w(attributes currentBalanceChangedAt), :datetime},
current_balance_reference_date: {~w(attributes currentBalanceReferenceDate), :datetime},
holder_name: {~w(attributes holderName), :string},
reference_type: {~w(attributes referenceType), :string},
authorization_expiration_expected_at:
{~w(attributes authorizationExpirationExpectedAt), :datetime},
authorized_at: {~w(attributes authorizedAt), :datetime},
available_balance_variation_observed_at:
{~w(attributes availableBalanceVariationObservedAt), :datetime},
current_balance_variation_observed_at:
{~w(attributes currentBalanceVariationObservedAt), :datetime},
internal_reference: {~w(attributes internalReference), :string},
avilability: {~w(meta availability), :string},
latest_synchronization: {~w(meta latestSynchronization), :map},
synchronized_at: {~w(meta synchronizedAt), :datetime}
]
end
end
Loading

0 comments on commit 1817067

Please sign in to comment.