TreasuryPrime.Resource (TreasuryPrime v1.0.0)

Copy Markdown View Source

Shared plumbing used internally by every resource module (TreasuryPrime.Account, TreasuryPrime.Ach, TreasuryPrime.Card, ...) to implement list/2, get/2, create/2, update/3, and delete/2 in terms of TreasuryPrime.HTTP.

This module is not meant to be called directly by consumers of the library — it exists purely so the ~45 resource modules don't each reimplement "make the request, cast the response into my struct, maybe wrap it in a Page" from scratch. Each resource module remains a normal, explicit, fully-documented module with its own struct and typespecs; only the repetitive HTTP/casting plumbing is shared.

Summary

Functions

Casts a raw, string-keyed JSON map (as decoded by Jason) into module's struct, using module.__fields__/0 as an allowlist of known atoms. This is deliberately not String.to_existing_atom/1 over arbitrary input — every atom involved is already known at compile time from the resource module's own field list, so there's no atom-exhaustion risk even though the input map's keys come from the network.

DELETE a resource by id. Returns {:ok, nil} on success (Treasury Prime returns an empty body).

GET a single resource by id.

GET a list/collection endpoint, returning a TreasuryPrime.Page.

PATCH to update a resource by id.

Functions

cast(module, map)

@spec cast(module(), map()) :: struct()

Casts a raw, string-keyed JSON map (as decoded by Jason) into module's struct, using module.__fields__/0 as an allowlist of known atoms. This is deliberately not String.to_existing_atom/1 over arbitrary input — every atom involved is already known at compile time from the resource module's own field list, so there's no atom-exhaustion risk even though the input map's keys come from the network.

Unknown keys returned by the API (Treasury Prime explicitly documents that it may add undocumented fields to responses over time) are silently ignored rather than raising — forward compatibility over strictness.

create(client, path, module, params, opts \\ [])

@spec create(TreasuryPrime.Client.t(), String.t(), module(), map(), keyword()) ::
  {:ok, struct()} | {:error, TreasuryPrime.Error.t()}

POST to create a resource.

create!(client, path, module, params, opts \\ [])

@spec create!(TreasuryPrime.Client.t(), String.t(), module(), map(), keyword()) ::
  struct()

delete(client, path, id, opts \\ [])

@spec delete(TreasuryPrime.Client.t(), String.t(), String.t(), keyword()) ::
  {:ok, nil} | {:error, TreasuryPrime.Error.t()}

DELETE a resource by id. Returns {:ok, nil} on success (Treasury Prime returns an empty body).

delete!(client, path, id, opts \\ [])

@spec delete!(TreasuryPrime.Client.t(), String.t(), String.t(), keyword()) :: nil

get(client, path, module, id, opts \\ [])

@spec get(TreasuryPrime.Client.t(), String.t(), module(), String.t(), keyword()) ::
  {:ok, struct()} | {:error, TreasuryPrime.Error.t()}

GET a single resource by id.

get!(client, path, module, id, opts \\ [])

@spec get!(TreasuryPrime.Client.t(), String.t(), module(), String.t(), keyword()) ::
  struct()

list(client, path, module, params \\ %{})

@spec list(TreasuryPrime.Client.t(), String.t(), module(), map()) ::
  {:ok, TreasuryPrime.Page.t()} | {:error, TreasuryPrime.Error.t()}

GET a list/collection endpoint, returning a TreasuryPrime.Page.

list!(client, path, module, params \\ %{})

update(client, path, module, id, params, opts \\ [])

@spec update(
  TreasuryPrime.Client.t(),
  String.t(),
  module(),
  String.t(),
  map(),
  keyword()
) ::
  {:ok, struct()} | {:error, TreasuryPrime.Error.t()}

PATCH to update a resource by id.

update!(client, path, module, id, params, opts \\ [])

@spec update!(
  TreasuryPrime.Client.t(),
  String.t(),
  module(),
  String.t(),
  map(),
  keyword()
) :: struct()