Current section
Files
Jump to
Current section
Files
lib/bingex/swap/data/get_contracts.ex
defmodule Bingex.Swap.Data.GetContracts do
@moduledoc """
Represents response content on `Bingex.Swap.get_contracts` request.
"""
alias Bingex.API.Reply
alias Bingex.Swap.Model.ContractInfo
defstruct contracts: []
@type t() :: %__MODULE__{
contracts: [ContractInfo.t()]
}
@spec embed_in_reply(Reply.t()) :: Reply.t(t())
def embed_in_reply(%Reply{payload: payload} = reply) do
Reply.put_data(reply, decode(payload))
end
@spec decode(any()) :: t()
def decode(%{"data" => data_list}) when is_list(data_list) do
contracts =
Enum.reduce(data_list, [], fn data, list ->
case ContractInfo.decode(data) do
{:ok, struct} -> [struct | list]
:error -> list
end
end)
%__MODULE__{contracts: contracts}
end
def decode(_data), do: %__MODULE__{}
end