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