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
See OAuth2.Client.delete/5
Use these to send DELETE requests to the endpoints via OAuth2.Client
See OAuth2.Client.delete!/3
See OAuth2.Client.delete!/4
See OAuth2.Client.delete!/5
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
See OAuth2.Client.post/5
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.post!/5
See OAuth2.Client.put/2
See OAuth2.Client.put/3
See OAuth2.Client.put/4
See OAuth2.Client.put/5
Use these to send PUT requests to the endpoints via OAuth2.Client
See OAuth2.Client.put!/3
See OAuth2.Client.put!/4
See OAuth2.Client.put!/5
Functions
same as endpoints/1 but raises error
Example getting URI for further requests
[%{"uri" => uri} | _] = XmartThings.endpoints!(st_client).body
Fetches an OAuth2.AccessToken struct by making a request to the token endpoint.