项目作者: zhongwencool

项目描述 :
Maxwell is an HTTP client which support for middleware and multiple adapters.
高级语言: Elixir
项目地址: git://github.com/zhongwencool/maxwell.git
创建时间: 2016-03-22T13:53:05Z
项目社区:https://github.com/zhongwencool/maxwell

开源协议:MIT License

下载


Maxwell

Build Status
Inline docs
Coveralls Coverage
Module Version
Hex Docs
Total Download
License
Last Updated

Maxwell is an HTTP client that provides a common interface over :httpc, :ibrowse, :hackney.

Getting Started

The simplest way to use Maxwell is by creating a module which will be your API wrapper, using Maxwell.Builder:

  1. defmodule GitHubClient do
  2. # Generates `get/1`, `get!/1`, `patch/1`, `patch!/1` public functions
  3. # You can omit the list and functions for all HTTP methods will be generated
  4. use Maxwell.Builder, ~w(get patch)a
  5. # For a complete list of middlewares, see the docs
  6. middleware Maxwell.Middleware.BaseUrl, "https://api.github.com"
  7. middleware Maxwell.Middleware.Headers, %{"content-type" => "application/vnd.github.v3+json", "user-agent" => "zhongwenool"}
  8. middleware Maxwell.Middleware.Opts, connect_timeout: 3000
  9. middleware Maxwell.Middleware.Json
  10. middleware Maxwell.Middleware.Logger
  11. # adapter can be omitted, and the default will be used (currently :ibrowse)
  12. adapter Maxwell.Adapter.Hackney
  13. # List public repositories for the specified user.
  14. def user_repos(username) do
  15. "/users/#{username}/repos"
  16. |> new()
  17. |> get()
  18. end
  19. # Edit owner repositories
  20. def edit_repo_desc(owner, repo, name, desc) do
  21. "/repos/#{owner}/#{repo}"
  22. |> new()
  23. |> put_req_body(%{name: name, description: desc})
  24. |> patch()
  25. end
  26. end

Maxwell.Builder injects functions for all supported HTTP methods, in two flavors, the first (e.g. get/1) will
return {:ok, Maxwell.Conn.t} or {:error, term, Maxwell.Conn.t}. The second (e.g. get!/1) will return
Maxwell.Conn.t only if the request succeeds and returns a 2xx status code, otherwise it will raise Maxwell.Error.

The same functions are also exported by the Maxwell module, which you can use if you do not wish to define a wrapper
module for your API, as shown below:

  1. iex(1)> alias Maxwell.Conn
  2. iex(2)> Conn.new("http://httpbin.org/drip") |>
  3. Conn.put_query_string(%{numbytes: 25, duration: 1, delay: 1, code: 200}) |>
  4. Maxwell.get
  5. {:ok,
  6. %Maxwell.Conn{method: :get, opts: [], path: "/drip",
  7. query_string: %{code: 200, delay: 1, duration: 1, numbytes: 25},
  8. req_body: nil, req_headers: %{}, resp_body: '*************************',
  9. resp_headers: %{"access-control-allow-credentials" => "true",
  10. "access-control-allow-origin" => "*",
  11. "connection" => "keep-alive",
  12. "content-length" => "25",
  13. "content-type" => "application/octet-stream",
  14. "date" => "Sun, 18 Dec 2016 14:32:38 GMT",
  15. "server" => "nginx"}, state: :sent, status: 200,
  16. url: "http://httpbin.org"}}

There are numerous helper functions for the Maxwell.Conn struct. See it’s module docs
for a list of all functions, and detailed info about how they behave.

Installation

  1. Add maxwell to your list of dependencies in mix.exs:

    1. def deps do
    2. [{:maxwell, "~> 2.3"}]
    3. end
  2. Ensure maxwell has started before your application:

    1. def application do
    2. [applications: [:maxwell]] # also add your adapter(ibrowse, hackney)
    3. end

Adapters

Maxwell has support for different adapters that do the actual HTTP request processing.

httpc

Maxwell has built-in support for the httpc Erlang HTTP client.

To use it simply place adapter Maxwell.Adapter.Httpc in your API client definition.

ibrowse

Maxwell has built-in support for the ibrowse Erlang HTTP client.

To use it simply place adapter Maxwell.Adapter.Ibrowse in your API client definition.

NOTE: Remember to include :ibrowse in your applications list.

hackney

Maxwell has built-in support for the hackney Erlang HTTP client.

To use it simply place adapter Maxwell.Adapter.Hackney in your API client definition.

NOTE: Remember to include :hackney in your applications list.

Built-in Middleware

Maxwell.Middleware.BaseUrl

Sets the base url for all requests.

Maxwell.Middleware.Headers

Sets default headers for all requests.

Maxwell.Middleware.HeaderCase

Enforces that all header keys share a specific casing style, e.g. lower-case,
upper-case, or title-case.

Maxwell.Middleware.Opts

Sets adapter options for all requests.

Maxwell.Middleware.Rels

Decodes rel links in the response, and places them in the :rels key of the Maxwell.Conn struct.

Maxwell.Middleware.Logger

Logs information about all requests and responses. You can set :log_level to log the information at that level.

Maxwell.Middleware.Json

Encodes all requests as application/json and decodes all responses as application/json.

Maxwell.Middleware.EncodeJson

Encodes all requests as application/json.

Maxwell.Middleware.DecodeJson

Decodes all responses as application/json.

NOTE: The *Json middlewares require Poison as dependency, versions 2.x and 3.x are supported.
You may provide your own encoder/decoder by providing the following options:

  1. # For the EncodeJson module
  2. middleware Maxwell.Middleware.EncodeJson,
  3. encode_content_type: "text/javascript",
  4. encode_func: &other_json_lib.encode/1]
  5. # For the DecodeJson module
  6. middleware Maxwell.Middleware.DecodeJson,
  7. decode_content_types: ["yourowntype"],
  8. decode_func: &other_json_lib.decode/1]
  9. # Both sets of options can be provided to the Json module

Custom Middlewares

Take a look at the Maxwell.Middleware for more information
on the behaviour. For example implementations take a look at any of the middleware modules in the repository.

Contributing

Contributions are more than welcome!

Check the issues tracker for anything marked “help wanted”, and post a comment that you are planning to begin working on the issue. We can
then provide guidance on implementation if necessary.

License

See the LICENSE file for license rights and limitations (MIT).