Raxx now has a simple client for HTTP/1.1
Examples
synchronous
request = Raxx.request(:GET, "http://example.com")
|> Raxx.set_header("accept", "text/html")
{:ok, response} = Raxx.SimpleClient.send_sync(request, 2000)
asynchronous
request = Raxx.request(:GET, "http://example.com")
|> Raxx.set_header("accept", "text/html")
channel = Raxx.SimpleClient.send_async(request)
{:ok, response} = Raxx.SimpleClient.yield(channel, 2000)
Simple Client
Client is simple because it makes very few assumptions about how to deal with requests and responses.
For example.
- Cookies are not managed, each request is handled in isolation
- Connections are not limited or reused, each request gets a new connection
These omissions of functionality make the client much simpler to work with and reason about.
They are also not limitations in many cases. An API client probably doesn’t use cookies.
Composable requests
Because a Raxx.Request
is just a data structure,
using this client separates the logic of building the request from the side effect of sending it.
This makes it very easy add custom functionality for your own clients.
import Raxx
def set_request_id(request, request_id) do
request
|> set_header("x-request-id", request_id)
end
def set_json_payload(request, json) do
request
|> set_header("content-type", "application/json")
|> set_body(Poison.encode!(json))
end
# later
request = Raxx.request(:POST, "http://api.com/create_user")
|> set_request_id("12345")
|> set_json_payload(%{"username" => "alice"})
The value of this separation for me in my own projects has been with testing.
It’s now very easy to create invalid requests. just forget to set a request_id and then send it to the API.