Go to the documentation of this file.00001
00002
00003 import json
00004 import requests
00005
00006 def load_settings(filename="smartthings.json"):
00007 """Load the JSON Settings file.
00008
00009 See the documentation, but briefly you can
00010 get it from here:
00011 https://iotdb.org/playground/oauthorize
00012 """
00013 with open(filename) as fin:
00014 std = json.load(fin)
00015 return std['api'], std['api_location'], std['access_token']
00016
00017 def get_endpoint(url, api_location, access_token):
00018 endpoints_url = url
00019 endpoints_paramd = {
00020 "access_token": access_token
00021 }
00022
00023 endpoints_response = requests.get(url=endpoints_url, params=endpoints_paramd)
00024 end_url = endpoints_response.json()[0]['url']
00025 endpoint_url = 'http://%s%s'%(api_location, end_url)
00026 return endpoint_url
00027
00028 def request_get(url, access_token, command):
00029 request_url = "%s/%s"%(url, command)
00030 params = {}
00031 header = {
00032 "Authorization": "Bearer %s" % access_token,
00033 }
00034 resp = requests.get(url=request_url, params=params, headers=header)
00035 r = resp.json()
00036
00037 if type(r) == list:
00038 r = r[0]
00039
00040 if type(r) == dict:
00041 for k, v in r.items():
00042 print("--- %s"%str(k))
00043 if type(v) == list or type(v) == dict:
00044 if v:
00045 for kk in v:
00046 for kkk, vvv in kk.items():
00047 print("\t%s : %s"%(kkk, vvv))
00048 print("")
00049 else:
00050 print(str(" %s"%v))
00051 else:
00052 print(str(r))
00053
00054
00055
00056 def request_to_update_uri(url, access_token, paired_uri):
00057 request_url = "%s/%s"%(url, "configuration")
00058 params = {"uri": paired_uri}
00059 header = {
00060 "Authorization": "Bearer %s" % access_token,
00061 }
00062 resp = requests.put(url=request_url, params=params, headers=header)
00063 print(str(resp))
00064
00065 if __name__ == '__main__':
00066 api, api_location, access_token = load_settings()
00067 endpoint_url = get_endpoint(api, api_location, access_token)
00068
00069
00070
00071 r = request_to_update_uri(endpoint_url, access_token, "qiowjefqojiwefoqijfqwe")
00072 r = request_get(endpoint_url, access_token,"configuration")