Skip to content

Middleware module for Tesla HTTP client library that encodes and decodes XML-RPC

License

Notifications You must be signed in to change notification settings

cogini/tesla_middleware_xmlrpc

Repository files navigation

test workflow Contributor Covenant

Tesla.Middleware.XMLRPC

Middleware for the Tesla HTTP client that encodes and decodes XML-RPC.

It uses xmlrpc to do the actual encoding.

Installation

Add tesla_middleware_xmlrpc to the list of dependencies in mix.exs:

def deps do
  [
    {:tesla_middleware_xmlrpc, "~> 0.1.0"}
  ]
end

Configuration

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.

About

Middleware module for Tesla HTTP client library that encodes and decodes XML-RPC

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published