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
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.
: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.
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
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!"
Specs
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
Specs
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, ""}
Specs
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
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
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!"
Specs
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}
Specs
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
request(pid(), Kadabra.Request.t() | [Kadabra.Request.t()] | request_opts()) :: no_return()
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"}