spacex_ex v1.0.0 SpacexEx.Client View Source

API Client Module.

Link to this section Summary

Functions

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

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

Encode the body to pass along to the server

Send a POST 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> Myclient.Api.authorization_header("def456")
{"Authorization", "Bearer def456"}

iex> Myclient.Api.authorization_header()
{"Authorization", "Bearer abc123"}
Link to this function call(url, method, body \\ "", query_params \\ %{}, headers \\ []) View Source

Call the API service

Examples

iex> Myclient.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> Myclient.Api.clean_headers(%{})
[{"Content-Type", "application/json; charset=utf-8"}]

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

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

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

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

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

iex> Myclient.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> Myclient.Api.clean_url()
"http://localhost:4000/"

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

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

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

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

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

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

Extract the content type of the headers

Examples

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

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

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

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

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

Decode the response body

Examples

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

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

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

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

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

iex> Myclient.Api.decode({:error, :nxdomain, "application/dontcare"})
{:error, :nxdomain}

Encode the body to pass along to the server

Examples

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

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

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

iex> Myclient.Api.encode("goop", "application/mytsuff")
"goop"
Link to this function get(url \\ "https://api.spacexdata.com/v2", query_params \\ %{}, headers \\ []) View Source

Send a GET request to the API

Examples

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

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

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

iex> Myclient.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> Myclient.Api.post("http://localhost:4000", %{version: "2.0.0"})
{201, %{version: "2.0.0"}}

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

Examples

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