Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import requests
  4. import getpass
  5. import json
  6.  
  7. class TempObj(object):
  8.  
  9. def __init__(self,data):
  10. self.__dict__ = json.load(data)
  11.  
  12. def checkStatusCode(status_code, Url):
  13.  
  14. if status_code == 200 or status_code == 201:
  15. return
  16. else:
  17. print("Call to URL {0} failed with status code {1}".format(Url,status_code))
  18. print("Exiting")
  19. exit(1)
  20.  
  21.  
  22. def getDeviceHeader():
  23.  
  24. global deviceHeader
  25.  
  26. authHeaders = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
  27.  
  28. userName = input("Concert username[iotpadminuser] ") or "iotpadminuser"
  29. password = "P@55w0rd"
  30.  
  31. authUrl = 'https://sso.pool4.iotpdev.com/auth/realms/authenticate/protocol/openid-connect/token'
  32.  
  33. try:
  34. print("authURL: {0}".format(authUrl))
  35.  
  36. response = requests.post(authUrl,
  37. headers = authHeaders,
  38. data = {"client_id" : "postman",
  39. "username" : userName,
  40. "password" : password,
  41. "grant_type" : "password"
  42. }
  43. )
  44. checkStatusCode(response.status_code,authUrl)
  45. print (response)
  46.  
  47. except requests.exceptions.ConnectionError as connErr:
  48. print("Connection Error: {0}".format(str(connErr)))
  49. exit(1)
  50. except requests.exceptions.RequestException as e:
  51. print("Response Exception Raised on auth URL: {0}".format(str(e)))
  52. print("Address exception: Quitting")
  53. exit(1)
  54.  
  55. deviceHeader = {"Authorization" : "Bearer " + response.json()['access_token'],
  56. "Content-Type" : "application/json"}
  57.  
  58. def tempRequest():
  59. req = requests.get('https://api.pool4.iotpdev.com/api/1/devices/fb903ae2-67de-46cb-8cb7-9eb3faf62e1d/sensors/e370df3d-6283-459f-9d8e-0b614dec578b', headers = deviceHeader)
  60. print(req.json())
  61. #tempObj = TempObj(req.json())
  62. #print(tempObj.a)
  63.  
  64. def humRequest():
  65. req = requests.get('https://api.pool4.iotpdev.com/api/1/devices/fb903ae2-67de-46cb-8cb7-9eb3faf62e1d/sensors/7021ee31-f8b2-4b38-bd4f-14fbec90fd87', headers = deviceHeader)
  66. print(req.json())
  67.  
  68. def lightRequest():
  69. req = requests.get('https://api.pool4.iotpdev.com/api/1/devices/fb903ae2-67de-46cb-8cb7-9eb3faf62e1d/sensors/6c8810fe-8c78-4fec-bd54-e9586975da98', headers = deviceHeader)
  70. print(req.json())
  71.  
  72. def postRequest():
  73. print("post request started")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement