shotgun

Shotgun's main interface.

Shotgun's main interface. Use the functions provided in this module to open a connection and make requests.

Types


connection_type() = http | https

headers() = #{}

http_verb() =
            get | post | head | delete | patch | put | options

options() =
            #{async => boolean(),
              async_data => binary | sse,
              handle_event =>
                  fun((fin | nofin, reference(), binary()) -> any()),
              basic_auth => {string(), string()},
              timeout => integer() >= 1 | infinity}

response() =
            #{status_code => integer(), header => #{}, body => binary()}

result() = {ok, reference() | response()} | {error, term()}

Functions


start() -> {ok, [atom()]}

Starts the application and all the ones it depends on.

stop() -> ok | {error, term()}

Stops the application

open(Host::string(), Port::integer()) -> {ok, pid()}

Equivalent to get(Host, Port, http).

open(Host::string(), Port::integer(), Type::connection_type()) -> {ok, pid()}

Opens a connection of the type provided with the host in port specified.

close(Pid::pid()) -> ok

Closes the connection with the host.

get(Pid::pid(), Uri::string()) -> result()

Equivalent to get(Pid, Uri, #{}, #{}).

get(Pid::pid(), Uri::string(), Headers::headers()) -> result()

get(Pid::pid(), Uri::string(), Headers::headers(), Options::options()) -> result()

Performs a GET request to Uri using Headers. Available options are:

async: specifies if the request performed will return a chunked response. It currently only works for GET requests. Default value is false.

async_data: when async is true the mode specifies how the data received will be processed. binary mode treats eat chunk received as raw binary. sse mode buffers each chunk, splitting the data received into SSE. Default value is binary.

handle_event: this function will be called each time either a chunk is received (async_data = binary) or an event is parsed (async_data = sse). If no handle_event function is provided the data received is added to a queue, whose values can be obtained calling the shotgun:events/1. Default value is undefined.

post(Pid::pid(), Uri::string(), Headers::headers(), Body::iodata(), Options::options()) -> result()

Performs a POST request to Uri using Headers and Body as the content data.

delete(Pid::pid(), Uri::string(), Headers::headers(), Options::options()) -> result()

Performs a DELETE request to Uri using Headers.

head(Pid::pid(), Uri::string(), Headers::headers(), Options::options()) -> result()

Performs a HEAD request to Uri using Headers.

options(Pid::pid(), Uri::string(), Headers::headers(), Options::options()) -> result()

Performs a OPTIONS request to Uri using Headers.

patch(Pid::pid(), Uri::string(), Headers::headers(), Body::iodata(), Options::options()) -> result()

Performs a PATCH request to Uri using Headers and Body as the content data.

put(Pid::pid(), Uri::string(), Headers0::headers(), Body::iodata(), Options::options()) -> result()

Performs a PUT request to Uri using Headers and Body as the content data.

request(Pid::pid(), Method::http_verb(), Uri::string(), Headers0::headers(), Body::iodata(), Options::options()) -> result()

Performs a request to Uri using the HTTP method specified by Method, Body as the content data and Headers as the request's headers.

events(Pid::pid()) -> [{nofin | fin, reference(), binary()}]

Returns a list of all received events up to now.

parse_event(EventBin::binary()) -> {Data::binary(), Id::binary(), EventName::binary()}

Parse an SSE event in binary format. For example the following binary <<"data: some content\n">> will be turned into the following map #{<<"data">> => <<"some content">>}.

Federico Carrone federico@inaka.net Juan Facorro juan@inaka.net