From 9c77441d4ad3e3e55c385b74cb4e52c8eb5439c5 Mon Sep 17 00:00:00 2001 From: Nico Date: Wed, 14 Jul 2021 15:30:22 +0100 Subject: [PATCH 1/2] Removed unnessary dependencies, making use of salt naitve utils and other small improvements --- cloudflare.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/cloudflare.py b/cloudflare.py index 94d1db5..a309947 100644 --- a/cloudflare.py +++ b/cloudflare.py @@ -56,11 +56,10 @@ from collections import namedtuple import re -import json -import yaml -import requests import logging import salt.exceptions +import salt.utils.http +import salt.utils.json logger = logging.getLogger(__name__) @@ -169,7 +168,7 @@ def pure(self): def data(self): if self.type == "SRV": service, proto, name = self.name.split(".", 2) - parts = self.content.split("\t") + parts = self.content.split() if len(parts) == 3: # record should look like this: "priority weight port target" # cloudflare returns: "weight port target" @@ -187,13 +186,13 @@ def data(self): "target": target, } if self.type == "CAA": - parts = self.content.split(" ") + parts = self.content.split() flags, tag, value = parts return { "name": self.name, "flags": int(flags), "tag": tag, - "value": value[1:-1], + "value": value.strip('"'), } def __str__(self): @@ -258,25 +257,20 @@ def _request(self, uri, method="GET", json=None): else: headers = {"X-Auth-Email": self.auth_email, "X-Auth-Key": self.auth_key} + if method not in ["GET", "POST", "PUT", "DELETE"]: + raise Exception("Unknown request method: {0}".format(method)) + logger.info("Sending request: {0} {1} data: {2}".format(method, uri, json)) - if method == "GET": - resp = requests.get(uri, headers=headers) - elif method == "POST": - resp = requests.post(uri, headers=headers, json=json) - elif method == "PUT": - resp = requests.put(uri, headers=headers, json=json) - elif method == "DELETE": - resp = requests.delete(uri, headers=headers) - else: - raise Exception("Unknown request method: {0}".format(method)) + data = salt.utils.json.dumps(json) if json else None + resp = salt.utils.http.query(uri, header_dict=headers, data=data, decode=True, method=method) - if not resp.ok: + if resp.get('error'): raise Exception( - "Got HTTP code {0}: {1}".format(resp.status_code, resp.text) + "Got HTTP code {0}: {1}".format(resp.get('status'), resp.get('error')) ) - return resp.json() + return resp['dict'] def _add_record(self, record): self._request( From 0f038079cf98652b4790316da376b72706311c05 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 9 Feb 2023 21:35:52 +0000 Subject: [PATCH 2/2] Removed the imports and use the salt injected __utils__ functions --- cloudflare.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cloudflare.py b/cloudflare.py index a309947..7099d6e 100644 --- a/cloudflare.py +++ b/cloudflare.py @@ -58,8 +58,6 @@ import re import logging import salt.exceptions -import salt.utils.http -import salt.utils.json logger = logging.getLogger(__name__) @@ -262,8 +260,8 @@ def _request(self, uri, method="GET", json=None): logger.info("Sending request: {0} {1} data: {2}".format(method, uri, json)) - data = salt.utils.json.dumps(json) if json else None - resp = salt.utils.http.query(uri, header_dict=headers, data=data, decode=True, method=method) + data = __utils__["json.dumps"](json) if json else None + resp = __utils__["http.query"](uri, header_dict=headers, data=data, decode=True, method=method) if resp.get('error'): raise Exception(