Coder Social home page Coder Social logo

sabiwara / mint Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elixir-mint/mint

0.0 1.0 0.0 989 KB

Functional HTTP client for Elixir with support for HTTP/1 and HTTP/2 🌱

License: Apache License 2.0

Erlang 1.91% Elixir 97.94% Dockerfile 0.14%

mint's Introduction

Mint 🌱

Build Status Docs Hex.pm Version Coverage Status

Functional HTTP client for Elixir with support for HTTP/1 and HTTP/2.

Installation

To install Mint, add it to your mix.exs file. Unless you're using your own SSL certificate store, also add the CAStore library to your dependencies.

defp deps do
  [
    {:castore, "~> 0.1.0"},
    {:mint, "~> 1.0"}
  ]
end

Then, run $ mix deps.get.

Usage

Mint is different from most Erlang and Elixir HTTP clients because it provides a process-less architecture. Instead, Mint is based on a functional and immutable data structure that represents an HTTP connection. This data structure wraps a TCP or SSL socket. This allows for more fine-tailored architectures where the developer is responsible for wrapping the connection struct, such as having one process handle multiple connections or having different kinds of processes handle connections.

Below is an example of a basic interaction with Mint. First, we start a connection through Mint.HTTP.connect/3:

iex> {:ok, conn} = Mint.HTTP.connect(:http, "httpbin.org", 80)

This transparently chooses between HTTP/1 and HTTP/2. Requests are sent with:

iex> {:ok, conn, request_ref} = Mint.HTTP.request(conn, "GET", "/", [], "")

The connection socket runs in active mode (with active: :once), which means that the user of the library needs to handle TCP messages and SSL messages:

iex> flush()
{:tcp, #Port<0.8>,
 "HTTP/1.1 200 OK\r\n" <> _}

To handle such messages, Mint provides a stream/2 function that turns messages into HTTP responses. Responses are streamed back to the user in parts through response parts :status, :headers, :data, and finally :done.

iex> {:ok, conn} = Mint.HTTP.connect(:http, "httpbin.org", 80)
iex> {:ok, conn, request_ref} = Mint.HTTP.request(conn, "GET", "/", [], "")
iex> receive do
...>   message ->
...>     {:ok, conn, responses} = Mint.HTTP.stream(conn, message)
...>     IO.inspect responses
...> end
[
  {:status, #Reference<...>, 200},
  {:headers, #Reference<...>, [{"connection", "keep-alive"}, ...},
  {:data, #Reference<...>, "<!DOCTYPE html>..."},
  {:done, #Reference<...>}
]

The connection API is stateless, this means that you need to make sure to always save the returned conn:

# Wrong
{:ok, _conn, ref} = Mint.HTTP.request(conn, "GET", "/foo", [], "")
{:ok, conn, ref} = Mint.HTTP.request(conn, "GET", "/bar", [], "")

# Correct
{:ok, conn, ref} = Mint.HTTP.request(conn, "GET", "/foo", [], "")
{:ok, conn, ref} = Mint.HTTP.request(conn, "GET", "/bar", [], "")

For more information, see the documentation.

SSL certificates

When using SSL, you can pass in your own CA certificate store or use one provided by Mint. Mint doesn't ship with the certificate store itself, but it has an optional dependency on CAStore, which provides an up-to-date certificate store. If you don't want to use your own certificate store, just add :castore to your dependencies.

def deps do
  [
    {:castore, "~> 0.1.0"},
    {:mint, "~> 0.4.0"}
  ]
end

WebSocket Support

Mint itself does not support the WebSocket protocol, but it can be used as the foundation to build a WebSocket client on top of. If you need WebSocket support, you can use mint_web_socket.

Connection Management and Pooling

Mint is a low-level client. If you need higher-level features such as connection management, pooling, metrics, and more, check out Finch, a project built on top of Mint that provides those things.

Contributing

If you wish to contribute check out the issue list and let us know what you want to work on so we can discuss it and reduce duplicate work.

Tests are organized with tags. Integration tests that hit real websites over the internet are tagged with :requires_internet_connection. Proxy tests are tagged with :proxy and require that you run docker-compose up from the Mint root directory in order to run (they are excluded by default when you run $ mix test). A few examples of running tests:

  • $ mix test to run the test suite without caring about Docker and docker-compose up.

  • $ mix test --exclude integration to only run local tests (for example, you don't have an internet connection available).

  • $ mix test --include proxy to run all tests, including proxy tests.

License

Copyright 2018 Eric Meadows-Jönsson and Andrea Leopardi

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

mint's People

Contributors

ahamez avatar amatalai avatar aquageek avatar bismark avatar dsdshcym avatar dunyakokoschka avatar ericmj avatar exit9 avatar gamache avatar howleysv avatar jayjun avatar josevalim avatar kianmeng avatar liamwhite avatar lucaspiller avatar lukebakken avatar mezz avatar myobie avatar paulswartz avatar qcam avatar scohen avatar sergiodottori avatar slashmili avatar sneako avatar tcrossland avatar the-mikedavis avatar voltone avatar vrcca avatar whatyouhide avatar wojtekmach avatar

Watchers

 avatar

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.