brighterx v0.0.2 Brighterx.Api

API interface to communicate with brighterlink-io api

Todo: refactor code so that we can move auth part to single place

Summary

Functions

Builds URL based on the resource, id and parameters

Wrapper for POST requests

Issues a DELETE request to the given url

Issues a DELETE request to the given url, raising an exception in case of failure

Wrapper for GET requests

Issues a GET request to the given url

Issues a GET request to the given url, raising an exception in case of failure

Convenience function to get company by id or name

Convenience function to get devices by id or name

Convenience function to get facility by id or name

Issues a HEAD request to the given url

Issues a HEAD request to the given url, raising an exception in case of failure

Issues an OPTIONS request to the given url

Issues a OPTIONS request to the given url, raising an exception in case of failure

Issues a PATCH request to the given url

Issues a PATCH request to the given url, raising an exception in case of failure

Issues a POST request to the given url

Issues a POST request to the given url, raising an exception in case of failure

Creating URL based on url from config and resources paths

Issues a PUT request to the given url

Issues a PUT request to the given url, raising an exception in case of failure

Wrapper for DELETE requests

Issues an HTTP request with the given method to the given url

Issues an HTTP request with the given method to the given url, raising an exception in case of failure

Add authorization header which is basically a JWT token and also the user agent

Starts HTTPoison and its dependencies

Wrapper for PUT requests

Types

body ::
  binary |
  {:form, [{atom, any}]} |
  {:file, binary}
headers ::
  [{binary, binary}] |
  %{binary => binary}

Functions

build_url(module, id, params \\ %{})

Specs

build_url(any, integer, map) :: String.t

Builds URL based on the resource, id and parameters

create(module, post_data, opts \\ [])

Wrapper for POST requests

Examples

  • Brighterx.Api.create(Brighterx.Resources.Device, %{name: “Test Thermostat”, identifier: “00:01”, facility_id: 1, type: “thermostat”})
  • Brighterx.Api.create(Brighterx.Resources.Company, “{“name”: “Samar”}”)
delete(url, headers \\ [], options \\ [])

Specs

delete(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a DELETE request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

delete!(url, headers \\ [], options \\ [])

Specs

delete!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a DELETE request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

find(module, opts \\ [])

Specs

find(any, list) :: Brighterx.response
find(any, list) :: Brighterx.response
find(any, list) :: Brighterx.response
find(any, list) :: Brighterx.response

Wrapper for GET requests

Example

  • Brighterx.Api.find(Brighterx.Resources.Company, [params: %{name: "Brightergy"}])

  • A tuple of {:ok, [%Brighterx.Resource.{}]} is returned for successful requests
  • For http status code 204, a :ok atom is returned indicating the request was fulfilled successfully but no response body i.e. message-body
  • For all other errors, a tuple of {:ok, , status_code} is returned
get(url, headers \\ [], options \\ [])

Specs

get(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a GET request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

get!(url, headers \\ [], options \\ [])

Specs

get!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a GET request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

get_company(id)

Specs

get_company(integer) :: Brighterx.response
get_company(String.t) :: Brighterx.response

Convenience function to get company by id or name

Examples

  • Brighterx.Api.get_company(1)
  • Brighterx.Api.get_company(“Brightergy”)
get_device(id)

Specs

get_device(integer) :: Brighterx.response
get_device(String.t) :: Brighterx.response

Convenience function to get devices by id or name

Examples

  • Brighterx.Api.get_device(1)
  • Brighterx.Api.get_device(“00:01:02:03:04:de:f0:cc”)
get_facility(id)

Specs

get_facility(integer) :: Brighterx.response
get_facility(String.t) :: Brighterx.response

Convenience function to get facility by id or name

Examples

  • Brighterx.Api.get_facility(1)
  • Brighterx.Api.get_facility(“Main Office”)
head(url, headers \\ [], options \\ [])

Specs

head(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a HEAD request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

head!(url, headers \\ [], options \\ [])

Specs

head!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a HEAD request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

options(url, headers \\ [], options \\ [])

Specs

options(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues an OPTIONS request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

options!(url, headers \\ [], options \\ [])

Specs

options!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a OPTIONS request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

patch(url, body, headers \\ [], options \\ [])

Specs

patch(binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a PATCH request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

patch!(url, body, headers \\ [], options \\ [])

Specs

patch!(binary, body, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a PATCH request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

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

Specs

post(binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a POST request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

post!(url, body, headers \\ [], options \\ [])

Specs

post!(binary, body, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a POST request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

process_url(path)

Specs

process_url(String.t) :: String.t

Creating URL based on url from config and resources paths

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

Specs

put(binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a PUT request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

put!(url, body, headers \\ [], options \\ [])

Specs

put!(binary, body, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a PUT request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

remove(module, id, opts \\ [])

Wrapper for DELETE requests

Examples

  • Brighterx.Api.delete(Brighterx.Resources.Device, 1)
request(method, url, body \\ "", headers \\ [], options \\ [])

Specs

request(atom, binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues an HTTP request with the given method to the given url.

This function is usually used indirectly by get/3, post/4, put/4, etc

Args:

  • method - HTTP method as an atom (:get, :head, :post, :put, :delete, etc.)
  • url - target url as a binary string or char list
  • body - request body. See more below
  • headers - HTTP headers as an orddict (e.g., [{"Accept", "application/json"}])
  • options - Keyword list of options

Body:

  • binary, char list or an iolist
  • {:form, [{K, V}, ...]} - send a form url encoded
  • {:file, "/path/to/file"} - send a file

Options:

  • :timeout - timeout to establish a connection, in milliseconds. Default is 8000
  • :recv_timeout - timeout used when receiving a connection. Default is 5000
  • :stream_to - a PID to stream the response to
  • :proxy - a proxy to be used for the request; it can be a regular url or a {Host, Proxy} tuple
  • :proxy_auth - proxy authentication {User, Password} tuple
  • :ssl - SSL options supported by the ssl erlang module
  • :follow_redirect - a boolean that causes redirects to be followed
  • :max_redirect - an integer denoting the maximum number of redirects to follow
  • :params - an enumerable consisting of two-item tuples that will be appended to the url as query string parameters

Timeouts can be an integer or :infinity

This function returns {:ok, response} or {:ok, async_response} if the request is successful, {:error, reason} otherwise.

Examples

request(:post, "https://my.website.com", "{\"foo\": 3}", [{"Accept", "application/json"}])
request!(method, url, body \\ "", headers \\ [], options \\ [])

Specs

request!(atom, binary, body, headers, Keyword.t) :: HTTPoison.Response.t

Issues an HTTP request with the given method to the given url, raising an exception in case of failure.

request!/5 works exactly like request/5 but it returns just the response in case of a successful request, raising an exception in case the request fails.

request_header(opts)
request_header(map, headers)

Add authorization header which is basically a JWT token and also the user agent

request_header_content_type(opts)
start()

Starts HTTPoison and its dependencies.

update(module, id, put_data)

Wrapper for PUT requests

Examples With body as map

  • Brighterx.Api.update(Brighterx.Resources.Device, 1, %{name: “7th floor south”})

With body as JSON string

  • Brighterx.Api.update(Brighterx.Resources.Device, 1, “{“name”: “7th Floor West”}”)
url()
url(arg1)