Skip to content

Commit

Permalink
Local enpoint refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinGuayacan committed Dec 20, 2022
1 parent 5487801 commit 3cc3cbd
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 228 deletions.
13 changes: 6 additions & 7 deletions lib/chainweb/pact.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
defmodule Kadena.Chainweb.Pact do
@moduledoc """
Specifies the API for processing HTTP requests.
Exposes functions to interact with the Pact API endpoints.
"""

alias Kadena.Chainweb.Pact.Local
defdelegate local(request, network \\ :testnet04, chain_id \\ "0"), to: Local, as: :process
alias Kadena.Chainweb.Pact.{Local, Send}

# defdelegate send(request, network \\ :test, chain_id \\ "0"), to: Send, as: :process
# defdelegate poll(request, network \\ :test, chain_id \\ "0"), to: Poll, as: :process
# defdelegate listen(request, network \\ :test, chain_id \\ "0"), to: Listen, as: :process
# defdelegate spv(request, network \\ :test, chain_id \\ "0"), to: SPV, as: :process
@default_network_opts [network_id: :testnet04, chain_id: 0]

defdelegate send(cmds, network_opts \\ @default_network_opts), to: Send, as: :process
defdelegate local(cmds, network_opts \\ @default_network_opts), to: Local, as: :process
end
46 changes: 0 additions & 46 deletions lib/chainweb/pact/endpoints.ex

This file was deleted.

22 changes: 10 additions & 12 deletions lib/chainweb/pact/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,34 @@ defmodule Kadena.Chainweb.Pact.Local do
"""

alias Kadena.Chainweb.Request
alias Kadena.Chainweb.Pact.{Endpoints, JSONPayload, LocalResponse}
alias Kadena.Types.{Command, LocalRequestBody}
alias Kadena.Chainweb.Pact.{LocalRequestBody, LocalResponse, Spec}
alias Kadena.Types.Command

@type cmd :: Command.t()
@type json_request_body :: String.t()
@behaviour Endpoints
@type json :: String.t()
@behaviour Spec

@endpoint "local"
@headers [{"Content-Type", "application/json"}]

@impl true
def process(%Command{} = cmd, network, chain_id) do
request_body = get_json_request_body(cmd)
def process(%Command{} = cmd, network_id: network_id, chain_id: chain_id) do
request_body = json_request_body(cmd)

:post
|> Request.new(pact: [endpoint: @endpoint])
|> Request.set_chain_id(chain_id)
|> Request.set_network(network)
|> Request.set_network(network_id)
|> Request.add_body(request_body)
|> Request.add_headers(@headers)
|> Request.perform()
|> Request.results(as: LocalResponse)
end

def process(_cmds, _network, _chain_id), do: {:error, [command: :invalid_command]}

@spec get_json_request_body(cmd :: cmd()) :: json_request_body()
defp get_json_request_body(cmd) do
@spec json_request_body(cmd :: cmd()) :: json()
defp json_request_body(cmd) do
cmd
|> LocalRequestBody.new()
|> JSONPayload.parse()
|> LocalRequestBody.to_json!()
end
end
16 changes: 4 additions & 12 deletions lib/chainweb/pact/local_request_body.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBody do

@behaviour Kadena.Chainweb.Pact.Type

@type command :: String.t()
@type command :: Command.t()
@type hash :: PactTransactionHash.t()
@type sigs :: SignaturesList.t()
@type cmd :: String.t()
Expand All @@ -19,11 +19,8 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBody do
defstruct [:hash, :sigs, :cmd]

@impl true
def new(args) do
args
|> Command.new()
|> build_local_request_body()
end
def new(%Command{} = cmd), do: build_local_request_body(cmd)
def(new(_cmd), do: {:error, [arg: :not_a_command]})

@impl true
def to_json!(%__MODULE__{hash: hash, sigs: sigs, cmd: cmd}) do
Expand All @@ -33,17 +30,12 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBody do
end
end

@spec build_local_request_body(command :: command() | error()) :: t() | error()
@spec build_local_request_body(command :: command()) :: t()
defp build_local_request_body(%Command{} = command) do
attrs = Map.from_struct(command)
struct(%__MODULE__{}, attrs)
end

defp build_local_request_body({:error, [command: :not_a_list]}),
do: {:error, [local_request_body: :not_a_list]}

defp build_local_request_body({:error, reason}), do: {:error, reason}

@spec to_signature_list(signatures :: sigs()) :: {:ok, raw_sigs()}
defp to_signature_list(%SignaturesList{signatures: list}) do
sigs = Enum.map(list, fn sig -> Map.from_struct(sig) end)
Expand Down
29 changes: 29 additions & 0 deletions lib/chainweb/pact/spec.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Kadena.Chainweb.Pact.Spec do
@moduledoc """
Specifies the contracts to build the Pact's endpoints.
"""

alias Kadena.Chainweb.Pact.{
ListenResponse,
LocalResponse,
PollResponse,
SendResponse,
SPVResponse
}

alias Kadena.Chainweb.Error
alias Kadena.Types.Command

@type data :: list(Command.t()) | Command.t()
@type error :: {:error, Error.t()}
@type chain_id :: 0..19 | String.t()
@type network_opts :: [network_id: atom(), chain_id: chain_id()]
@type response ::
ListenResponse.t()
| LocalResponse.t()
| PollResponse.t()
| SendResponse.t()
| SPVResponse.t()

@callback process(data :: data(), network_opts :: network_opts()) :: {:ok, response()} | error()
end
54 changes: 0 additions & 54 deletions lib/types/local_request_body.ex

This file was deleted.

12 changes: 1 addition & 11 deletions test/chainweb/pact/local_request_body_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBodyTest do

alias Kadena.Chainweb.Pact.LocalRequestBody

<<<<<<< HEAD:test/chainweb/pact/local_request_body_test.exs
alias Kadena.Types.{PactTransactionHash, SignaturesList}
=======
alias Kadena.Types.{
Command,
LocalRequestBody,
PactTransactionHash,
Signature,
SignaturesList
}
>>>>>>> 4e12645 (Add local endpoitn and PACT API):test/types/local_request_body_test.exs

setup do
cmd =
Expand Down Expand Up @@ -93,15 +88,10 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBodyTest do
command: command,
json_result: json_result
} do
<<<<<<< HEAD:test/chainweb/pact/local_request_body_test.exs
^json_result =
[hash: hash, sigs: sigs, cmd: cmd]
command
|> LocalRequestBody.new()
|> LocalRequestBody.to_json!()
=======
local_request_body = LocalRequestBody.new(command)
^json_result = JSONPayload.parse(local_request_body)
>>>>>>> 4e12645 (Add local endpoitn and PACT API):test/types/local_request_body_test.exs
end
end
end
Loading

0 comments on commit 3cc3cbd

Please sign in to comment.