Giter Club home page Giter Club logo

httpact's Introduction

HTTPact

A contract for HTTP API Wrappers.

The idea is that API Wrapper libraries can depend on HTTPact instead directly on an HTTP Client library.

HTTPact describes how an HTTP Client must accept requests and return responses.

The Command and Entity HTTPact defines allow an API Wrapper a clear interface for converting to and from requests and responses.

Initially based on this neat gist from Michal Muskala.

Example Usage

The way I've trended towards building REST API Wrappers is with a struct for each command that can be executed against the API service. A valid command struct is a valid request against the service. The functions to create this command struct validate and cast parameters into the correct shape and return errors if necessary at the boundary. Valid commands can then be converted into a %Request{} which contains a client implementation that conforms to the HTTPact.Client behaviour. We can inject this Client at run-time on a per-request basis rather than compile, build, or application start time. A %Response{} is returned from the client implementation which can then be handled by the Wrapper library and converted into a domain entity. This domain entity can be converted back into a generic map where the consumer of the Wrapper library can do what it pleases.

HTTPact lets you implement protocols to control the transformation of a Request or Response into another type before returning to a consumer.

defmodule MyAPIWrapper do
  alias MyAPIWrapper.{CreateUser, User}

  def create_user(params) when is_map(params) do
    with {:ok, command} <- CreateUser.new(params) do
      HTTPact.execute(command, http_client())
    end
  end

  # fetch your http_client at runtime
  defp http_client() do
    Application.get_env(:my_api_wrapper, :http_client)
  end

  defmodule MyAPIWrapper.CreateUser do
    defstruct [:name, :email]

    def new(%{name: name, email: email}) do
      {:ok,
       %__MODULE__{
         name: name,
         email: email
       }}
    end

    def new(_), do: {:error, "invalid CreateUser params"}

    defimpl HTTPact.Command do
      def to_request(%__MODULE__{name: name, email: email}) do
        %HTTPact.Request{
          method: :post,
          path: "https://www.myapiservice.example/v1/users",
          headers: [
            {"Content-Type", "application/x-www-form-urlencoded"},
            {"Authorization", "Bearer #{MyAPIWrapper.Auth.fetch_token()}"}
          ],
          body:
            URI.encode_query(%{
              "name" => name,
              "email" => email
            })
        }
      end
    end
  end

  defmodule MyAPIWrapper.User do
    defstruct [:id, :name, :email]

    defimpl HTTPact.Entity do
      def from_response(%HTTPact.Response{status: 200, body: body}) do
        with {:ok,
              %{
                "id" => id,
                "email" => email,
                "name" => name
              }} <- Jason.decode(body) do
          %User{
            id: id,
            email: email,
            name: name
          }
        end
      end
    end
  end
end

Installation

If available in Hex, the package can be installed by adding httpact to your list of dependencies in mix.exs:

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

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/httpact.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.