appcues / mojito

An easy-to-use Elixir HTTP client, built on the low-level Mint library.
https://hexdocs.pm/mojito/Mojito.html
MIT License
349 stars 34 forks source link
elixir http http-client https pool

Mojito Build Status Docs Hex.pm Version License

Now Deprecated

We recommend that you use Finch which is also built on Mint. The creator of Finch has an excellent writeup here describing the problems with Mojito, and as a result we use Finch internally at Appcues now.

Original Description

Mojito is an easy-to-use, high-performance HTTP client built using the low-level Mint library.

Mojito is built for comfort and for speed. Behind a simple and predictable interface, there is a sophisticated connection pool manager that delivers maximum throughput with no intervention from the user.

Just want to make one request and bail? No problem. Mojito can make one-off requests as well, using the same process-less architecture as Mint.

Quickstart

{:ok, response} = Mojito.request(method: :get, url: "https://github.com")

Why Mojito?

Mojito addresses the following design goals:

Installation

Add mojito to your deps in mix.exs:

{:mojito, "~> 0.7.10"}

Configuration

The following config.exs config parameters are supported:

The following :pool_opts options are supported:

For example:

use Mix.Config

config :mojito,
  timeout: 2500,
  pool_opts: [
    size: 10,
    destinations: [
      "example.com:443": [
        size: 20,
        max_overflow: 20,
        pools: 10
      ]
    ]
  ]

Certain configs can be overridden with each request. See request/1.

Usage

Make requests with Mojito.request/1 or Mojito.request/5:

>>>> Mojito.request(:get, "https://jsonplaceholder.typicode.com/posts/1")
## or...
>>>> Mojito.request(%{method: :get, url: "https://jsonplaceholder.typicode.com/posts/1"})
## or...
>>>> Mojito.request(method: :get, url: "https://jsonplaceholder.typicode.com/posts/1")

{:ok,
 %Mojito.Response{
   body: "{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n  \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n}",
   headers: [
     {"content-type", "application/json; charset=utf-8"},
     {"content-length", "292"},
     {"connection", "keep-alive"},
     ...
   ],
   status_code: 200
 }}

By default, Mojito will use a connection pool for requests, automatically handling the creation and reuse of pools. If this is not desired, specify the pool: false option with a request to perform a one-off request. See the documentation for request/1 for more details.

Self-signed SSL/TLS certificates

To accept self-signed certificates in HTTPS connections, you can give the transport_opts: [verify: :verify_none] option to Mojito.request or Mojito.Pool.request:

>>>> Mojito.request(method: :get, url: "https://localhost:8443/")
{:error, {:tls_alert, 'bad certificate'}}

>>>> Mojito.request(method: :get, url: "https://localhost:8443/", opts: [transport_opts: [verify: :verify_none]])
{:ok, %Mojito.Response{ ... }}

Telemetry

Mojito integrates with the standard Telemetry library.

See the Mojito.Telemetry module for more information.

Changelog

See the CHANGELOG.md.

Contributing

Thanks for considering contributing to this project, and to the free software ecosystem at large!

Interested in contributing a bug report? Terrific! Please open a GitHub issue and include as much detail as you can. If you have a solution, even better -- please open a pull request with a clear description and tests.

Have a feature idea? Excellent! Please open a GitHub issue for discussion.

Want to implement an issue that's been discussed? Fantastic! Please open a GitHub pull request and write a clear description of the patch. We'll merge your PR a lot sooner if it is well-documented and fully tested.

Contributors and contributions are listed in the changelog. Heartfelt thanks to everyone who's helped make Mojito better.

Copyright and License

Copyright 2018-2021 Appcues, Inc.

This software is released under the MIT License.