Middleware for the Tesla HTTP client that encodes and decodes XML-RPC.
It uses xmlrpc to do the actual encoding.
Add tesla_middleware_xmlrpc
to the list of dependencies in mix.exs
:
def deps do
[
{:tesla_middleware_xmlrpc, "~> 0.1.0"}
]
end
Add this middleware as a plug in your client.
defmodule FooClient do
@api_url Application.compile_env(:foo_client, :api_url, "http://localhost:8080/")
use Tesla
plug Tesla.Middleware.BaseUrl, @api_url
plug Tesla.Middleware.XMLRPC
plug Tesla.Middleware.Logger
def call(method_name, params, opts \\ []) do
body = %XMLRPC.MethodCall{method_name: method_name, params: params}
case post("/RPC2", body, opts) do
{:ok, %{status: 200, body: %XMLRPC.MethodResponse{param: result}}} ->
{:ok, result}
{:ok, %{status: 200, body: %XMLRPC.Fault{fault_code: code, fault_string: reason}}} ->
{:error, {:fault, code, reason}}
{:ok, response} ->
{:error, {:unexpected_response, response}}
{:error, _reason} = error ->
error
end
end
end
Documentation is here: https://hexdocs.pm/tesla_middleware_xmlrpc
This project uses the Contributor Covenant version 2.1. Check CODE_OF_CONDUCT.md for more information.