Tirexs v0.8.15 Tirexs.HTTP

A set of functions for working over HTTP.

This bare-bone module provides all you need for getting things done over elasticsearch REST API. The functions designed to be handful for various possible use cases.

A set of request functions such as head/1-3, get/1-3, put/1-4, post/1-4 and delete/1-3 also available with bangs functions. The bang functions raise RuntimeError exceptions.

iex> get("unknown")
{:error, 404, ... }

iex> get!("unknown")
** (RuntimeError) {"error":{"root_cause": [{"type":"index_not_found_exception" ... }]

The common set of allowed parameters are path, params, uri and body.

iex> get("/bear_test/my_type/1")
iex> get("/articles/document/1", [_source: false])
iex> put("/bear_test/my_type/1?version=2", [user: "kimchy"])
iex> put("/bear_test/my_type/1", [version: 2], [user: "kimchy"])
iex> put("/bear_test/my_type/1", [version: 2], %URI{ host: "example.com" }, [user: "kimchy"])

The uri parameter has a special purpose. You can use it for overriding any fields from application’s environment uri (see Tirexs.ENV.get_uri_env/0.

iex> Tirexs.get_uri_env()
%URI{host: "127.0.0.1", port: 9200, scheme: "http"}

iex> url("/articles/document/1")
"http://127.0.0.1:9200/articles/document/1"

iex> url("/articles/document/1", %URI{ port: 92, query: "_source=false" })
"http://127.0.0.1:92/articles/document/1?_source=false"

A query params could be as a part of path or used as a standalone params. The params param is allowed to be a %{} or [].

iex> put("/bear_test/my_type/1?version=2", [user: "kimchy"])
iex> put("/bear_test/my_type/1", [version: 2], [user: "kimchy"])
iex> put("/bear_test/my_type/1", %{version: 2}, %{user: "kimchy"})

A request body is allowed to be a Keyword or Map instances or event just as String.

Summary

Functions

Sends a DELETE request

Sends a DELETE request and raise an exception if something wrong

Sends a GET request

Sends a GET request and raise an exception if something wrong

Sends a HEAD request

Sends a HEAD request and raise an exception if something wrong

Raises RuntimeError if { :error, _, _ } = response, otherwise returns response back

Returns false if { :error, _, _ } = response, otherwise returns true

Sends a POST request

Sends a POST request and raise an exception if something wrong

Sends a PUT request

Sends a PUT request and raise an exception if something wrong

Composes a complete URL by given params

Functions

delete(url_or_path_or_uri)
delete(path, uri)
delete(path, params, uri)

Sends a DELETE request.

delete!(a)
delete!(a, b)
delete!(a, b, c)

Sends a DELETE request and raise an exception if something wrong.

get(url_or_path_or_uri)
get(path, uri)
get(path, params, uri)

Sends a GET request.

get!(a)
get!(a, b)
get!(a, b, c)

Sends a GET request and raise an exception if something wrong.

head(url_or_path_or_uri)
head(path, uri)
head(path, params, uri)

Sends a HEAD request.

head!(a)
head!(a, b)
head!(a, b, c)

Sends a HEAD request and raise an exception if something wrong.

ok!(response)

Raises RuntimeError if { :error, _, _ } = response, otherwise returns response back.

ok?(arg1)

Returns false if { :error, _, _ } = response, otherwise returns true.

post(url_or_path_or_uri)
post(path, uri)
post(path, uri, body)
post(path, params, uri, body)

Sends a POST request.

post!(a)
post!(a, b)
post!(a, b, c)
post!(a, b, c, d)

Sends a POST request and raise an exception if something wrong.

put(url_or_path_or_uri)
put(path, uri)
put(path, uri, body)
put(path, params, uri, body)

Sends a PUT request.

put!(a)
put!(a, b)
put!(a, b, c)
put!(a, b, c, d)

Sends a PUT request and raise an exception if something wrong.

url()
url(url_or_path)
url(path, uri)
url(path, params, uri)

Composes a complete URL by given params.

Examples:

iex> url()
"http://127.0.0.1:9200"

iex> url("/articles/document/1")
"http://127.0.0.1:9200/articles/document/1"

iex> url("/articles/document/1", %URI{ host: "example.com" })
"http://example.com:9200/articles/document/1"

Also see Tirexs.Resources.urn/1.