From 10d9f91e93498a1dad17917ac0cac3a637a99ac6 Mon Sep 17 00:00:00 2001 From: "franz.nemeth" Date: Sat, 21 Mar 2020 15:30:39 +0100 Subject: [PATCH 1/3] implemented http_status platform --- dashmachine/platform/http_status.py | 94 +++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 dashmachine/platform/http_status.py diff --git a/dashmachine/platform/http_status.py b/dashmachine/platform/http_status.py new file mode 100644 index 0000000..3f0259b --- /dev/null +++ b/dashmachine/platform/http_status.py @@ -0,0 +1,94 @@ +""" + +##### http_status +Make a http call on a given URL and display if the service is online. +```ini +[variable_name] +platform = http_status +resource = https://your-website.com/api +method = get +authentication = basic +username = my_username +password = my_password +headers = {"Content-Type": "application/json"} +return_codes = 2xx,3xx +``` +> **Returns:** a right-aligned colored bullet point on the app card. + +| Variable | Required | Description | Options | +|-----------------|----------|-----------------------------------------------------------------|-------------------| +| [variable_name] | Yes | Name for the data source. | [variable_name] | +| platform | Yes | Name of the platform. | rest | +| resource | Yes | Url of rest api resource. | url | +| method | No | Method for the api call, default is GET | GET,POST | +| authentication | No | Authentication for the api call, default is None | None,basic,digest | +| username | No | Username to use for auth. | string | +| password | No | Password to use for auth. | string | +| headers | No | Request headers | json | +| return_codes | No | Acceptable http status codes, x is handled as wildcard | string | + +> **Working example:** +>```ini +>[http_status_test] +>platform = http_status +>resource = https://google.com +>return_codes = 2xx,3xx +> +>[Google] +>prefix = https:// +>url = google.com +>icon = static/images/apps/default.png +>open_in = this_tab +>data_sources = http_status_test +>``` + +""" + +from requests import Request, Session +from requests.auth import HTTPBasicAuth, HTTPDigestAuth + + +class Platform: + def __init__(self, *args, **kwargs): + # parse the user's options from the config entries + for key, value in kwargs.items(): + self.__dict__[key] = value + + # set defaults for omitted options + if not hasattr(self, "method"): + self.method = "GET" + if not hasattr(self, "authentication"): + self.authentication = None + if not hasattr(self, "headers"): + self.headers = None + if not hasattr(self, "return_codes"): + self.return_codes = "2xx,3xx" + + def process(self): + # Check if method is within allowed methods for http_status + if self.method.upper() not in ["GET", "HEAD", "OPTIONS", "TRACE"]: + raise NotImplementedError + + s = Session() + # prepare Authentication mechanism + if self.authentication: + if self.authentication.lower() == "digest": + auth = HTTPDigestAuth(self.username, self.password) + else: + auth = HTTPBasicAuth(self.username, self.password) + else: + auth = None + + # Send request + req = Request(self.method.upper(), self.resource, headers=self.headers, auth=auth) + prepped = req.prepare() + resp = s.send(prepped) + + return_codes = tuple([x.replace('x', '') for x in self.return_codes.split(',')]) + + if str(resp.status_code).startswith(return_codes): + icon_class = "theme-success-text" + else: + icon_class = "theme-failure-text" + + return f"fiber_manual_record " From 13dffd5954d4cdf0888d4e74477e3ff05e30ab9f Mon Sep 17 00:00:00 2001 From: "franz.nemeth" Date: Sat, 21 Mar 2020 15:56:04 +0100 Subject: [PATCH 2/3] fixed method options --- dashmachine/platform/http_status.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashmachine/platform/http_status.py b/dashmachine/platform/http_status.py index 3f0259b..a74741f 100644 --- a/dashmachine/platform/http_status.py +++ b/dashmachine/platform/http_status.py @@ -20,7 +20,7 @@ return_codes = 2xx,3xx | [variable_name] | Yes | Name for the data source. | [variable_name] | | platform | Yes | Name of the platform. | rest | | resource | Yes | Url of rest api resource. | url | -| method | No | Method for the api call, default is GET | GET,POST | +| method | No | Method for the api call, default is GET | GET,HEAD,OPTIONS,TRACE| | authentication | No | Authentication for the api call, default is None | None,basic,digest | | username | No | Username to use for auth. | string | | password | No | Password to use for auth. | string | From 0f9cafb456a9077bdcd3bc4ad05818c30b5f1f31 Mon Sep 17 00:00:00 2001 From: "franz.nemeth" Date: Sun, 22 Mar 2020 00:38:24 +0100 Subject: [PATCH 3/3] implemented headers for rest platform and option to turn ssl verification off --- dashmachine/platform/rest.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dashmachine/platform/rest.py b/dashmachine/platform/rest.py index b8b914a..77fd143 100644 --- a/dashmachine/platform/rest.py +++ b/dashmachine/platform/rest.py @@ -12,6 +12,8 @@ authentication = basic username = my_username password = my_password payload = {"var1": "hi", "var2": 1} +headers = {"Content-Type": "application/json"} +verify = false ``` > **Returns:** `value_template` as rendered string @@ -26,6 +28,8 @@ payload = {"var1": "hi", "var2": 1} | username | No | Username to use for auth. | string | | password | No | Password to use for auth. | string | | payload | No | Payload for post request. | json | +| headers | No | Custom headers for get or post | json | +| verify | No | Turn TLS verification on or off, default is True | true,false | > **Working example:** >```ini @@ -62,6 +66,10 @@ class Platform: self.method = "GET" if not hasattr(self, "authentication"): self.authentication = None + if not hasattr(self, "headers"): + self.headers = None + if not hasattr(self, "verify"): + self.verify = True def process(self): if self.authentication: @@ -72,14 +80,16 @@ class Platform: else: auth = None + verify = False if str(self.verify).lower() == "false" else True + if self.method.upper() == "GET": try: - value = get(self.resource, auth=auth).json() + value = get(self.resource, auth=auth, headers=self.headers, verify=verify).json() except Exception as e: value = f"{e}" elif self.method.upper() == "POST": payload = json.loads(self.payload.replace("'", '"')) - value = post(self.resource, data=payload, auth=auth) + value = post(self.resource, data=payload, auth=auth, headers=self.headers, verify=verify) value_template = render_template_string(self.value_template, value=value) return value_template