HttpBuilder v0.4.1 HttpBuilder View Source

HttpBuilder is a library that provides a DSL for composable HTTP Requests.

Each method provided builds and updates a HTTPBuilder.Request object, until passed into send, which calls the adapter to invoke the request.

Example

Here’s an example of a complete request chain:

HTTPBuilder.new()
|> with_adapter(HttpBuilder.Adapters.HTTPoison)
|> post("http://httparrot.com/post/1")
|> with_headers(%{"Authorization" => "Bearer token"})
|> with_json_parser(Jason)
|> with_json_body(%{"test" => "true"})
|> with_request_timeout(10_000)
|> with_receive_timeout(5_000)
|> send() # kicks off the request.

This can also be broken down into composable parts, allowing you to easily write declarative pipelines for your API calls.

defmodule MyAPIClient do
    alias HttpBuilder.Adapters

    @adapter Application.get_env(:my_api_client, :client, Adapters.HTTPoison)

    def client do
        HTTPBuilder.new() 
        |> with_adapter()
        |> with_host("http://httparrot.com/")
        |> with_headers(%{"Authorization" => "Bearer token"})
        |> with_request_timeout(10_000)
        |> with_receive_timeout(5_000)
    end

    def create(params) do
        client()
        |> post("/new")
        |> with_json_body(params)
        |> send()
    end

    def update(id, params) do
        client()
        |> put("/item/#{id}")
        |> with_json_body(params)
        |> send()
    end

    def list(limit, offset) do
        client()
        |> get("/items")
        |> with_query_params(%{"limit" => limit, "offset" => offset})
        |> send()
    end

    def delete(id) do
        client()
        |> delete("/item/#{id}")
        |> send()
    end

end

Sometimes, you don’t want to make a call against a service. By putting your adapter in your config, you can also easily switch to a test HTTP adapter. By pattern matching against the request object, you can handle exact request scenarios.

defmodule MyAPIClient.TestAdapter do

    @behaviour HttpBuilder.Adapter

    def send(%{method: :post, path: "/new"}), do: {:ok, new_placeholder_data }
    def send(%{method: :get, path: "/items"}), do: {:ok, items_placeholder_data }

    # ... other request options.
end

Link to this section Summary

Functions

Casts a map of predefined options to a HttpBuilder.Request struct

Sets the connect method on the request

Sets the delete method on the request

Sets the get method on the request

Sets the head method on the request

Creates a new request

Sets the options method on the request

Sets the patch method on the request

Sets the post method on the request

Sets the put method on the request

Executes a request, with the provided adapter

Sets the adapter for the request

Adds a body to the request, with no special notation of type

Marks a body as a file upload

Marks a body as a form-encoded upload

Sets either a list of two-item tuples, or map of headers on the request

Sets the host for the request

Marks a body to be sent as JSON

Sets the json parser for the request. Defaults to Poison if available

Sets additional options for the request that may not be handled by this DSL

Sets either a list of two-item tuples, or map of query params on the request

Sets the receive timeout of the request

Sets the request timeout of the request

Marks a body to be sent as a string

Link to this section Types

Link to this section Functions

Casts a map of predefined options to a HttpBuilder.Request struct.

Link to this function connect(request, path \\ "") View Source

Sets the connect method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function delete(request, path \\ "") View Source
delete(request(), path()) :: request()

Sets the delete method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function get(request, path \\ "") View Source
get(request(), path()) :: request()

Sets the get method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function head(request, path \\ "") View Source
head(request(), path()) :: request()

Sets the head method on the request.

Takes an optional path, to be added onto the host of the request.

Creates a new request.

Takes in a host, providing the base path for http requests, and an adapter module to eventually run the request.

Link to this function options(request, path \\ "") View Source
options(request(), path()) :: request()
options(request(), path()) :: request()

Sets the options method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function patch(request, path \\ "") View Source
patch(request(), path()) :: request()

Sets the patch method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function post(request, path \\ "") View Source
post(request(), path()) :: request()

Sets the post method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function put(request, path \\ "") View Source
put(request(), path()) :: request()

Sets the put method on the request.

Takes an optional path, to be added onto the host of the request.

Link to this function send(request) View Source
send(HttpBuilder.HttpRequest.t()) ::
  {:ok, term()} |
  {:error, String.t()}

Executes a request, with the provided adapter

Link to this function with_adapter(request, adapter) View Source
with_adapter(request(), atom()) :: request()

Sets the adapter for the request.

Takes an atom, representing a module that conforms to the HttpBuilder.Adapter behaviour.

Link to this function with_body(request, body) View Source
with_body(request(), term()) :: request()

Adds a body to the request, with no special notation of type.

This body should be used to handle requests not explicitly covered by HTTPBuilder, or the adapter.

Link to this function with_file_body(request, path) View Source
with_file_body(request(), path()) :: request()

Marks a body as a file upload.

Takes a filepath, and adds a tuple of {:file, filepath} to the body of the request.

Link to this function with_form_encoded_body(request, body) View Source
with_form_encoded_body(request(), [String.t()] | map()) :: request()

Marks a body as a form-encoded upload.

Takes either a list of two-item tuples, or a map of key-value pairs, and adds a tuple of {:form, [{"key", "value"} ...]} to the body of the request, and sets the Content-Type to “application/x-www-form-urlencoded”.

Link to this function with_headers(request, headers) View Source
with_headers(request(), [String.t()] | map()) :: request()

Sets either a list of two-item tuples, or map of headers on the request.

Link to this function with_host(request, host) View Source

Sets the host for the request.

Can be used for host/path composition to create a client library for an API.

Link to this function with_json_body(request, body) View Source
with_json_body(request(), list() | map()) :: request()
with_json_body(request(), module()) :: request()

Marks a body to be sent as JSON.

Takes the value passed in, and adds a tuple of {:json, body} to the request, and adds application/json as Content-Type headers

The adapter will be responsible for encoding the value.

Link to this function with_json_parser(request, parser) View Source

Sets the json parser for the request. Defaults to Poison if available.

Most JSON parsers can be used out of the box. See HttpBuilder.Adapter.JSONParser for adapter details.

Link to this function with_options(request, list) View Source
with_options(request(), list() | term()) :: request()

Sets additional options for the request that may not be handled by this DSL.

Link to this function with_query_params(request, query_params) View Source
with_query_params(request(), [String.t()] | map()) :: request()

Sets either a list of two-item tuples, or map of query params on the request.

Link to this function with_receive_timeout(request, timeout) View Source
with_receive_timeout(request(), integer()) :: request()

Sets the receive timeout of the request.

A receive timeout is how long until the request recieves a response. A request has a default value of 5000.

Link to this function with_request_timeout(request, timeout) View Source
with_request_timeout(request(), integer()) :: request()

Sets the request timeout of the request.

A request timeout is how long the overall request should take. A request has a default value of 8000.

Link to this function with_string_body(request, body) View Source
with_string_body(request(), list() | map()) :: request()

Marks a body to be sent as a string.

Takes a string, and adds a tuple of {:string, body} to the request.