From 4e73b14ab139ad2657b2b956ef3a61a19a474d32 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 3 Aug 2020 23:13:27 +0200 Subject: [PATCH] Async requests --- .gitignore | 1 + hue_controller.py | 19 ++++++++++++------- main.py | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index cffdafc..7f8b085 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.py lib/__pycache__/ +__pycache__/ diff --git a/hue_controller.py b/hue_controller.py index 18d5f55..3aafd08 100644 --- a/hue_controller.py +++ b/hue_controller.py @@ -1,15 +1,20 @@ import requests as req # Used for HTTP requests for the Hue API import json # API uses JSON -import config # Configuration for the controller +import config # Configuration for the controller (/config.py <- change this file) def genUrl(params: str): - return "https://" + config.address + "/api/" + config.username + params + return "http://" + config.address + "/api/" + config.username + params -def API_Request(ReqType: str, params: str): - apiReq = req.Request( ReqType, genUrl(params) ) +await def API_Request(ReqType: str="GET", params: str=""): # Requests for the API + try: + apiReq = req.Request( ReqType, genUrl(params) ) + apiReqPrep = apiReq.prepare() - apiReqPrep = apiReq.prepare() - apiSession = req.Session() - apiSession.send(apiReqPrep) + apiSession = req.Session() + response = apiSession.send(apiReqPrep) + + print(response.body) + except req.exceptions.RequestException as err: + raise SystemExit(err) # throw the error diff --git a/main.py b/main.py index 4fad088..29effa0 100755 --- a/main.py +++ b/main.py @@ -1,9 +1,19 @@ #!/usr/bin/env python from lib.input import * # Commandline parser -import hue_controller # Actual controller +import hue_controller as hue # Actual controller -def init(): +params = "" +if( inputHasKeys(["-p"]) ): + params = getValueOfKey("-p") + +reqtype = "GET" +if( inputHasKeys(["-t"]) ): + reqtype = getValueOfKey("-t") +def init(): + print(reqtype, params) + hue.API_Request(reqtype, params) + init()