Kadabra (Kadabra v0.6.0) View Source

HTTP/2 client for Elixir.

Written to manage HTTP/2 connections for pigeon.

Requires Elixir 1.4/OTP 19.2 or later.

Usage

{:ok, pid} = Kadabra.open("https://http2.golang.org")
Kadabra.get(pid, "/")
receive do
  {:end_stream, %Kadabra.Stream.Response{} = stream} ->
  IO.inspect stream
after 5_000 ->
  IO.puts "Connection timed out."
end

%Kadabra.Stream.Response{
  body: "<html>\\n<body>\\n<h1>Go + HTTP/2</h1>\\n\\n<p>Welcome to..."
  headers: [
    {":status", "200"},
    {"content-type", "text/html; charset=utf-8"},
    {"content-length", "1708"},
    {"date", "Sun, 16 Oct 2016 21:20:47 GMT"}
  ],
  id: 1,
  status: 200
}

Making Requests Manually

{:ok, pid} = Kadabra.open("https://http2.golang.org")

path = "/ECHO" # Route echoes PUT body in uppercase
body = "sample echo request"
headers = [
  {":method", "PUT"},
  {":path", path},
]

Kadabra.request(pid, headers, body)

receive do
  {:end_stream, %Kadabra.Stream.Response{} = stream} ->
  IO.inspect stream
after 5_000 ->
  IO.puts "Connection timed out."
end

%Kadabra.Stream.Response{
  body: "SAMPLE ECHO REQUEST",
  headers: [
    {":status", "200"},
    {"content-type", "text/plain; charset=utf-8"},
    {"date", "Sun, 16 Oct 2016 21:28:15 GMT"}
  ],
  id: 1,
  status: 200
}

Link to this section Summary

Types

Options for connections.

Options for making requests.

Functions

Closes an existing connection.

Makes a DELETE request.

Makes a GET request.

Makes a HEAD request.

Opens a new connection.

Pings an existing connection.

Makes a POST request.

Makes a PUT request.

Makes a request with given headers and optional body.

Link to this section Types

Specs

conn_opts() :: [ssl: [...], tcp: [...]]

Options for connections.

Specs

request_opts() :: [
  headers: [{String.t(), String.t()}, ...],
  body: String.t(),
  on_response: (Kadabra.Stream.Response.t() -> no_return())
]

Options for making requests.

  • :headers - (Required) Headers for request.
  • :body - (Optional) Used for requests that can have a body, such as POST.
  • :on_response - (Optional) Async callback for handling stream response.

Specs

uri() :: charlist() | String.t()

Link to this section Functions

Specs

close(pid()) :: :ok

Closes an existing connection.

Examples

iex> {:ok, pid} = Kadabra.open("https://http2.golang.org")
iex> Kadabra.close(pid)
iex> receive do
...>   {:closed, _pid} -> "connection closed!"
...> end
"connection closed!"
Link to this function

delete(pid, path, opts \\ [])

View Source

Specs

delete(pid(), String.t(), Keyword.t()) :: no_return()

Makes a DELETE request.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> Kadabra.delete(pid, "/")
:ok
iex> stream = receive do
...>   {:end_stream, stream} -> stream
...> end
iex> stream.status
200
Link to this function

get(pid, path, opts \\ [])

View Source

Specs

get(pid(), String.t(), Keyword.t()) :: no_return()

Makes a GET request.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> Kadabra.head(pid, "/reqinfo")
:ok
iex> response = receive do
...>   {:end_stream, response} -> response
...> end
iex> {response.id, response.status, response.body}
{1, 200, ""}
Link to this function

head(pid, path, opts \\ [])

View Source

Specs

head(pid(), String.t(), Keyword.t()) :: no_return()

Makes a HEAD request.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> Kadabra.head(pid, "/")
:ok
iex> response = receive do
...>   {:end_stream, response} -> response
...> end
iex> {response.id, response.status, response.body}
{1, 200, ""}

Specs

open(uri(), conn_opts()) :: {:ok, pid()} | {:error, term()}

Opens a new connection.

Examples

iex> {:ok, pid} = Kadabra.open("http://http2.golang.org")
iex> is_pid(pid)
true

iex> {:ok, pid} = Kadabra.open("https://http2.golang.org")
iex> is_pid(pid)
true

Specs

ping(pid()) :: no_return()

Pings an existing connection.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> Kadabra.ping(pid)
iex> receive do
...>   {:pong, _pid} -> "got pong!"
...> end
"got pong!"
Link to this function

post(pid, path, opts \\ [])

View Source

Specs

post(pid(), String.t(), Keyword.t()) :: no_return()

Makes a POST request.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> Kadabra.post(pid, "/", body: "test=123")
:ok
iex> response = receive do
...>   {:end_stream, response} -> response
...> end
iex> {response.id, response.status}
{1, 200}
Link to this function

put(pid, path, opts \\ [])

View Source

Specs

put(pid(), String.t(), Keyword.t()) :: no_return()

Makes a PUT request.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> Kadabra.put(pid, "/crc32", body: "test")
:ok
iex> stream = receive do
...>   {:end_stream, stream} -> stream
...> end
iex> stream.status
200
iex> stream.body
"bytes=4, CRC32=d87f7e0c"

Specs

Makes a request with given headers and optional body.

Examples

iex> {:ok, pid} = Kadabra.open('https://http2.golang.org')
iex> path = "/ECHO" # Route echoes PUT body in uppercase
iex> body = "sample echo request"
iex> headers = [
...>   {":method", "PUT"},
...>   {":path", path},
...> ]
iex> Kadabra.request(pid, headers: headers, body: body)
iex> response = receive do
...>   {:end_stream, %Kadabra.Stream.Response{} = response} -> response
...> after 5_000 -> :timed_out
...> end
iex> {response.id, response.status, response.body}
{1, 200, "SAMPLE ECHO REQUEST"}