XMLRPC
Encode and Decode elixir terms to XML-RPC parameters. All XML-RPC parameter types are supported, including arrays, structs and Nil (optional).
This module handles the parsing and encoding of the datatypes, but can be used in conjunction with HTTPoison, Phoenix, etc to create fully featured XML-RPC clients and servers.
erlsom is used to decode the xml as xmerl creates atoms during decoding, which has the risk that a malicious client can exhaust out atom space and crash the vm. Additionally xml input (ie untrusted) is validated against an XML Schema, which should help enforce correctness of input.
Example
iex> request_body = %XMLRPC.MethodCall{method_name: "test.sumprod", params: [2,3]} |> XMLRPC.encode!
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>test.sumprod</methodName><params><param><value><int>2</int></value></param><param><value><int>3</int></value></param></params></methodCall>"
# Now use HTTPoison to call your RPC
response = HTTPoison.post!("http://www.advogato.org/XMLRPC", request_body).body
iex> response = "<?xml version=\"1.0\"?><methodResponse><params><param><value><array><data><value><int>5</int></value><value><int>6</int></value></data></array></value></param></params></methodResponse>" |> XMLRPC.decode
{:ok, %XMLRPC.MethodResponse{param: [5, 6]}}
Datatypes
XML-RPC only allows limited parameter types. We map these to Elixir as follows:
| XMLRPC | Elixir |
|---|---|
| Boolean - true/false | |
| Bitstring - “string” | |
| Integer - 17 | |
| Float - -12.3 | |
| List - [1, 2, 3] | |
| Map - %{key: “value”} | |
| %XMLRPC.DateTime | |
| %XMLRPC.Base64 | |
| nil |
Note that array and struct parameters can be composed of the fundamental types, and you can nest to arbitrary depths.
The encoding is performed through a protocol and so abstract datatypes can be encoded by implementing the XMLRPC.ValueEncoder protocol.
Summary↑
| decode!(value, options \\ []) | Decode XMLRPC call or response XML into an Elixir structure |
| decode(value, options \\ []) | Decode XMLRPC call or response XML into an Elixir structure |
| encode!(value, options \\ []) | Encode an XMLRPC call or response elixir structure into XML |
| encode(value, options \\ []) | Encode an XMLRPC call or response elixir structure into XML |
| encode_to_iodata!(value, options \\ []) | Encode an XMLRPC call or response elixir structure into XML as iodata |
| encode_to_iodata(value, options \\ []) | Encode an XMLRPC call or response elixir structure into XML as iodata |
Types ↑
Functions
Specs:
- decode(iodata, Keyword.t) :: {:ok, XMLRPC.Fault.t} | {:ok, XMLRPC.MethodCall.t} | {:ok, XMLRPC.MethodResponse.t} | {:error, String.t}
Decode XMLRPC call or response XML into an Elixir structure
iex> XMLRPC.decode("<?xml version=\"1.0\"?><methodResponse><params><param><value><array><data><value><int>5</int></value><value><int>6</int></value></data></array></value></param></params></methodResponse>")
{:ok, %XMLRPC.MethodResponse{param: [5, 6]}}
Specs:
- decode!(iodata, Keyword.t) :: XMLRPC.Fault.t | XMLRPC.MethodCall.t | XMLRPC.MethodResponse.t | no_return
Decode XMLRPC call or response XML into an Elixir structure
Raises an exception on error.
iex> XMLRPC.decode!("<?xml version=\"1.0\"?><methodResponse><params><param><value><array><data><value><int>5</int></value><value><int>6</int></value></data></array></value></param></params></methodResponse>")
%XMLRPC.MethodResponse{param: [5, 6]}
Specs:
Encode an XMLRPC call or response elixir structure into XML.
iex> %XMLRPC.MethodCall{method_name: "test.sumprod", params: [2,3]} |> XMLRPC.encode
{:ok, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>test.sumprod</methodName><params><param><value><int>2</int></value></param><param><value><int>3</int></value></param></params></methodCall>"}
Specs:
Encode an XMLRPC call or response elixir structure into XML.
Raises an exception on error.
iex> %XMLRPC.MethodCall{method_name: "test.sumprod", params: [2,3]} |> XMLRPC.encode!
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>test.sumprod</methodName><params><param><value><int>2</int></value></param><param><value><int>3</int></value></param></params></methodCall>"
Specs:
Encode an XMLRPC call or response elixir structure into XML as iodata
Specs:
Encode an XMLRPC call or response elixir structure into XML as iodata
Raises an exception on error.