Code Monkey home page Code Monkey logo

erqwest's Introduction

erqwest

Hex pm

An erlang wrapper for reqwest using rustler. Map-based interface inspired by katipo.

Features

  • HTTP/1.1 and HTTP/2 with connection keepalive/reuse
  • Configurable SSL support, uses system root certificates by default
  • Sync and async interfaces
  • Proxy support
  • Optional cookies support
  • Optional gzip support

Prerequisites

  • Erlang/OTP
  • Rust
  • OpenSSL (not required on mac)

Or use the provided shell.nix if you have nix installed.

Usage

Start a client

ok = application:ensure_started(erqwest),
ok = erqwest:start_client(default).

This registers a client under the name default. The client maintains an internal connection pool.

Synchronous interface

No streaming

{ok, #{status := 200, body := Body}} = 
    erqwest:get(default, <<"https://httpbin.org/get">>),

{ok, #{status := 200, body := Body1}} =
    erqwest:post(default, <<"https://httpbin.org/post">>,
                 #{body => <<"data">>}).

Stream request body

{handle, H} = erqwest:post(default, <<"https://httpbin.org/post">>,
                           #{body => stream}),
ok = erqwest:send(H, <<"data, ">>),
ok = erqwest:send(H, <<"more data.">>),
{ok, #{body := Body}} = erqwest:finish_send(H).

Stream response body

{ok, #{body := Handle}} = erqwest:get(default, <<"https://httpbin.org/stream-bytes/1000">>,
                                      #{response_body => stream}),
ReadAll = fun Self() ->
              case erqwest:read(Handle, #{length => 0}) of
                {ok, Data} ->
                  [Data];
                {more, Data} ->
                  [Data|Self()]
              end
          end,
1000 = iolist_size(ReadAll()).

Conditionally consume response body

{ok, Resp} = erqwest:get(default, <<"https://httpbin.org/status/200,500">>,
                         #{response_body => stream}),
case Resp of
  #{status := 200, body := Handle} ->
    {ok, <<>>} = erqwest:read(Handle),
  #{status := BadStatus, body := Handle} ->
    %% ensures the connection is closed/can be reused immediately
    erqwest:cancel(Handle),
    io:format("Status is ~p, not interested~n", [BadStatus])
end.

Asynchronous interface

erqwest_async:req(default, self(), Ref=make_ref(), 
                  #{method => get, url => <<"https://httpbin.org/get">>}),
receive
    {erqwest_response, Ref, reply, #{body := Body}} -> Body
end.

See the docs for more details and and the test suite for more examples.

erqwest's People

Contributors

dlesl avatar dependabot[bot] avatar puzza007 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.