Created API class and other stuff

fetchIP
E. Almqvist 4 years ago
parent ecb7d269ad
commit aada66a1d2
  1. 46
      hue_controller.py

@ -14,26 +14,42 @@ def boolToString(v: bool): # To fix the dumb python syntax
else: else:
return "false" return "false"
async def API_Request(method: str="GET", params: str="", body: str=""): class APIrequest:
try: # Get Req
apiReq = req.Request( method, genUrl(params) ) # initialize the request async def get( dest: str="", payload: str="{}" ):
apiReqPrep = apiReq.prepare() # prepair it try:
apiReq = req.get( genUrl(dest), data = payload )
return apiReq
apiReqPrep.body = body # apply the body to the request except req.exceptions.RequestException as err:
print(err)
apiSession = req.Session() # POST Req
response = apiSession.send(apiReqPrep) # send it to the Hue bridge async def post( dest: str="", payload: str="{}" ):
try:
apiReq = req.post( genUrl(params), data = payload )
return apiReq
print(response) except req.exceptions.RequestException as err:
print(response.text) print(err)
except req.exceptions.RequestException as err:
raise SystemExit(err) # throw the error
async def setLightPower(index: int=1, on: bool=True): # PUT Req
await API_Request( "PUT", "/lights/" + str(index) + "/state", "{'on':" + boolToString(on) + "}" ) async def put( dest: str="", payload: str="{}" ):
try:
apiReq = req.put( genUrl(dest), data = payload ) # send the payload
print(apiReq)
print(apiReq.text)
return apiReq
except req.exceptions.RequestException as err:
print(err)
class controller:
async def toggleLight(index: int=1, isOn: bool=True):
await APIrequest.put( "/lights/1/state", '{"on":' + boolToString(isOn) + '}' )
def testReq(): def testReq():
# loop.run_until_complete( API_Request("GET", "/lights/1") ) # get test loop.run_until_complete( controller.toggleLight(1, True) ) # try to turn on/off a light
loop.run_until_complete( setLightPower(1, False) ) # try to turn on/off a light
loop.close() loop.close()

Loading…
Cancel
Save