Mas Mensajes client v0.1.2 MasMensajes.Api View Source

Make generic HTTP calls a web service. Please update (or remove) the tests to a sample service in the examples below, they refer to a sample project available at https://github.com/work-samples/myserver

This includes updating the @default_service_url below

Link to this section Summary

Functions

Resolve the shared secret token, if provided then simply return itself, otherwise lookup in the configs.

Retrieve data from the API using any method (:get, :post, :put, :delete, etc) available

Clean the URL, if there is a port, but nothing after, then ensure there's a ending '/' otherwise you will encounter something like hackney_url.erl:204: :hackney_url.parse_netloc/2

Clean the URL, if there is a port, but nothing after, then ensure there's a ending '/' otherwise you will encounter something like hackney_url.erl:204: :hackney_url.parse_netloc/2

Extract the content type of the headers

Decode the response body

Send a DELETE request to the API

Encode the body to pass along to the server

Send a GET request to the API

Send a POST request to the API

Send a PUT request to the API

The service's default URL, it will lookup the config, possibly check the env variables and default if still not found

Link to this section Functions

Link to this function

authorization_header(token \\ nil)

View Source

Resolve the shared secret token, if provided then simply return itself, otherwise lookup in the configs.

Examples

iex> MasMensajes.Api.authorization_header("abc123")
{"Authorization", "Bearer abc123"}

iex> MasMensajes.Api.authorization_header()
{"Authorization", "Bearer "}
Link to this function

call(url, method, body \\ "", query_params \\ %{}, headers \\ [])

View Source

Retrieve data from the API using any method (:get, :post, :put, :delete, etc) available

Examples

iex> MasMensajes.Api.call("http://localhost:4000", :post, %{version: "2.0.0"}, %{user: "james"})
{201, %{version: "2.0.0", user: "james"}}

Clean the URL, if there is a port, but nothing after, then ensure there's a ending '/' otherwise you will encounter something like hackney_url.erl:204: :hackney_url.parse_netloc/2

Also allow headers to be provided as a %{}, makes it easier to ensure defaults are set

Examples

iex> MasMensajes.Api.clean_headers(%{})
[{"Content-Type", "application/json; charset=utf-8"}]

iex> MasMensajes.Api.clean_headers(%{"Content-Type" => "application/xml"})
[{"Content-Type", "application/xml"}]

iex> MasMensajes.Api.clean_headers(%{"Authorization" => "Bearer abc123"})
[{"Authorization","Bearer abc123"}, {"Content-Type", "application/json; charset=utf-8"}]

iex> MasMensajes.Api.clean_headers(%{"Authorization" => "Bearer abc123", "Content-Type" => "application/xml"})
[{"Authorization","Bearer abc123"}, {"Content-Type", "application/xml"}]

iex> MasMensajes.Api.clean_headers([])
[{"Content-Type", "application/json; charset=utf-8"}]

iex> MasMensajes.Api.clean_headers([{"apples", "delicious"}])
[{"Content-Type", "application/json; charset=utf-8"}, {"apples", "delicious"}]

iex> MasMensajes.Api.clean_headers([{"apples", "delicious"}, {"Content-Type", "application/xml"}])
[{"apples", "delicious"}, {"Content-Type", "application/xml"}]
Link to this function

clean_params(query_params)

View Source

Clean the URL, if there is a port, but nothing after, then ensure there's a ending '/' otherwise you will encounter something like hackney_url.erl:204: :hackney_url.parse_netloc/2

Examples

iex> MasMensajes.Api.clean_url("http://localhost")
"http://localhost"

iex> MasMensajes.Api.clean_url("http://localhost:4000/b")
"http://localhost:4000/b"

iex> MasMensajes.Api.clean_url("http://localhost:4000")
"http://localhost:4000/"

Extract the content type of the headers

Examples

iex> MasMensajes.Api.content_type({:ok, "<xml />", [{"Server", "GitHub.com"}, {"Content-Type", "application/xml; charset=utf-8"}]})
{:ok, "<xml />", "application/xml"}

iex> MasMensajes.Api.content_type([])
"application/json"

iex> MasMensajes.Api.content_type([{"Content-Type", "plain/text"}])
"plain/text"

iex> MasMensajes.Api.content_type([{"Content-Type", "application/xml; charset=utf-8"}])
"application/xml"

iex> MasMensajes.Api.content_type([{"Server", "GitHub.com"}, {"Content-Type", "application/xml; charset=utf-8"}])
"application/xml"

Decode the response body

Examples

iex> MasMensajes.Api.decode({:ok, "{\"a\": 1}", "application/json"})
{:ok, %{a: 1}}

iex> MasMensajes.Api.decode({500, "", "application/json"})
{500, ""}

iex> MasMensajes.Api.decode({:error, "{\"a\": 1}", "application/json"})
{:error, %{a: 1}}

iex> MasMensajes.Api.decode({:ok, "{goop}", "application/json"})
{:error, "{goop}"}

iex> MasMensajes.Api.decode({:error, "{goop}", "application/json"})
{:error, "{goop}"}

iex> MasMensajes.Api.decode({:error, :nxdomain, "application/dontcare"})
{:error, :nxdomain}
Link to this function

delete(url, query_params \\ %{}, headers \\ [])

View Source

Send a DELETE request to the API

Encode the body to pass along to the server

Examples

iex> MasMensajes.Api.encode(%{a: 1}, "application/json")
"{\"a\":1}"

iex> MasMensajes.Api.encode("<xml/>", "application/xml")
"<xml/>"

iex> MasMensajes.Api.encode(%{a: "o ne"}, "application/x-www-form-urlencoded")
"a=o+ne"

iex> MasMensajes.Api.encode("goop", "application/mytsuff")
"goop"
Link to this function

get(url, query_params \\ %{}, headers \\ [])

View Source

Send a GET request to the API

Examples

iex> MasMensajes.Api.get("http://localhost:4849")
{:error, :econnrefused}

iex> MasMensajes.Api.get("http://localhost:4000")
{200, %{version: "0.1.0"}}

iex> MasMensajes.Api.get("http://localhost:4000", %{user: "andrew"})
{200, %{version: "0.1.0", user: "andrew"}}

iex> MasMensajes.Api.get("http://localhost:4000/droids/bb10")
{404, %{error: "unknown_resource", reason: "/droids/bb10 is not the path you are looking for"}}
Link to this function

post(url, body \\ nil, headers \\ [])

View Source

Send a POST request to the API

Examples

iex> MasMensajes.Api.post("http://localhost:4000", %{version: "2.0.0"})
{201, %{version: "2.0.0"}}
Link to this function

put(url, body \\ nil, headers \\ [])

View Source

Send a PUT request to the API

The service's default URL, it will lookup the config, possibly check the env variables and default if still not found

Examples

iex> MasMensajes.Api.service_url()
"http://localhost:4000"