xmart_things v0.1.0 XmartThings

OAuth Strategy module to work with SmartThings Web Services API

Examples

# Generate the authorization to redirect client for authorization
XmartThings.authorize_url!

# Capture the `code` from redirect in your callback handler route
st_client = XmartThings.get_token!(code: code)

# Use the access token to access resources

locks = XmartThings.get!(st_client, "/locks").body

# or
case XmartThings.get(st_client, "/locks") do
  {:ok, %OAuth2.Response{status_code: 401, body: body}} ->
    {:error, "unauthorized token"}
  {:ok, %OAuth2.Response{status_code: status_code, body: locks}} when status_code in [200..399] ->
    locks
  {:error, %OAuth2.Error{reason: reason}} ->
    {:error, reason}
end

# Use [`endpoints/1`](#endpoints/1) or [`endpoints!/1`](#endpoints!/1) to get the list of endpoints that you can use to talk with SmartApp
XmartThings.endpoints!(st_client)

# It can return bunch of responses. Pick one of those (usually the first one) to perform your requests to SmartApp

[%{"uri" => uri} | _] = XmartThings.endpoints!(st_client).body

# and now send the requests to URLs like below:

XmartThings.get(%{st_client | site: uri}, "/locks")

Summary

Functions

Creates authorization URL based on the client configuration

Creates a OAuth client struct for SmartThings Authorization

See OAuth2.Client.delete/2

See OAuth2.Client.delete/3

See OAuth2.Client.delete/4

Use these to send DELETE requests to the endpoints via OAuth2.Client

See OAuth2.Client.delete!/3

See OAuth2.Client.delete!/4

Retrieve the lists of endpoints exposed for a SmartApp

same as endpoints/1 but raises error

See OAuth2.Client.get/2

See OAuth2.Client.get/3

See OAuth2.Client.get/4

Use these to send GET requests to the endpoints via OAuth2.Client

See OAuth2.Client.get!/3

See OAuth2.Client.get!/4

Fetches an OAuth2.AccessToken struct by making a request to the token endpoint

See OAuth2.Client.post/2

See OAuth2.Client.post/3

See OAuth2.Client.post/4

Use these to send POST requests to the endpoints via OAuth2.Client

See OAuth2.Client.post!/3

See OAuth2.Client.post!/4

See OAuth2.Client.put/2

See OAuth2.Client.put/3

See OAuth2.Client.put/4

Use these to send PUT requests to the endpoints via OAuth2.Client

See OAuth2.Client.put!/3

See OAuth2.Client.put!/4

Functions

authorize_url(client, params)
authorize_url!()

Creates authorization URL based on the client configuration

client()

Creates a OAuth client struct for SmartThings Authorization

delete(client, endpoint)

See OAuth2.Client.delete/2.

delete(client, endpoint, body)

See OAuth2.Client.delete/3.

delete(client, endpoint, body, headers)

See OAuth2.Client.delete/4.

delete(client, endpoint, body, headers, opts)

See OAuth2.Client.delete/5.

delete!(client, endpoint)

Use these to send DELETE requests to the endpoints via OAuth2.Client.

delete!(client, endpoint, body)

See OAuth2.Client.delete!/3.

delete!(client, endpoint, body, headers)

See OAuth2.Client.delete!/4.

delete!(client, endpoint, body, headers, opts)

See OAuth2.Client.delete!/5.

endpoints(client)

Retrieve the lists of endpoints exposed for a SmartApp

endpoints!(client)

same as endpoints/1 but raises error

Example getting URI for further requests

[%{"uri" => uri} | _] = XmartThings.endpoints!(st_client).body
get(client, endpoint)

See OAuth2.Client.get/2.

get(client, endpoint, headers)

See OAuth2.Client.get/3.

get(client, endpoint, headers, params)

See OAuth2.Client.get/4.

get!(client, endpoint)

Use these to send GET requests to the endpoints via OAuth2.Client.

get!(client, endpoint, headers)

See OAuth2.Client.get!/3.

get!(client, endpoint, headers, params)

See OAuth2.Client.get!/4.

get_token(client, params, headers)
get_token!(params \\ [], headers \\ [], opts \\ [])

Fetches an OAuth2.AccessToken struct by making a request to the token endpoint.

post(client, endpoint)

See OAuth2.Client.post/2.

post(client, endpoint, body)

See OAuth2.Client.post/3.

post(client, endpoint, body, headers)

See OAuth2.Client.post/4.

post(client, endpoint, body, headers, opts)

See OAuth2.Client.post/5.

post!(client, endpoint)

Use these to send POST requests to the endpoints via OAuth2.Client.

post!(client, endpoint, body)

See OAuth2.Client.post!/3.

post!(client, endpoint, body, headers)

See OAuth2.Client.post!/4.

post!(client, endpoint, body, headers, opts)

See OAuth2.Client.post!/5.

put(client, endpoint)

See OAuth2.Client.put/2.

put(client, endpoint, body)

See OAuth2.Client.put/3.

put(client, endpoint, body, headers)

See OAuth2.Client.put/4.

put(client, endpoint, body, headers, opts)

See OAuth2.Client.put/5.

put!(client, endpoint)

Use these to send PUT requests to the endpoints via OAuth2.Client.

put!(client, endpoint, body)

See OAuth2.Client.put!/3.

put!(client, endpoint, body, headers)

See OAuth2.Client.put!/4.

put!(client, endpoint, body, headers, opts)

See OAuth2.Client.put!/5.