Kadabra v0.4.2 Kadabra 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

Link to this type conn_opts() View Source
conn_opts() :: [ssl: [...], tcp: [...]]

Options for connections.

  • :ssl - Specify custom options for :ssl.connect/3 when used with :https scheme.
  • :tcp - Specify custom options for :gen_tcp.connect/3 when used with :http scheme.
Link to this type request_opts() View Source
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.

Link to this section Functions

Link to this function close(pid) View Source
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
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
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
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, ""}
Link to this function open(uri, opts \\ []) View Source
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

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
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
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"

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"}