From ff4eab2be2208ab65fe7a70333f9aff1873df15a Mon Sep 17 00:00:00 2001 From: michivonah Date: Sun, 7 Jan 2024 20:55:30 +0100 Subject: [PATCH] add validation and checks --- main.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 3807783..2508499 100644 --- a/main.py +++ b/main.py @@ -36,15 +36,23 @@ def addClient(api_base_url, api_username, api_password, domainname): currentBlockedHosts = currentList["blocked_hosts"] clientId = getIPAdress(domainname) - allowedList = currentAllowList + [clientId] + if not clientId in currentAllowList: + allowedList = currentAllowList + [clientId] - data = { + data = { "allowed_clients":allowedList, "disallowed_clients":currentDisallowList, "blocked_hosts":currentBlockedHosts - } - response = requests.post(endpoint, auth=HTTPBasicAuth(api_username, api_password), json=data) - return response + } + + response = requests.post(endpoint, auth=HTTPBasicAuth(api_username, api_password), json=data) + if response.status_code == 200: + return f"{clientId} successfully added to the list of allowed clients." + else: + return f"An error occured while adding {clientId} to the list of allowed clients. Error message: {response.text}" + else: + return f"{clientId} already in list of allowed clients. Nothing was changed." + # Resolves the ip address of a domainname def getIPAdress(domainname): @@ -55,5 +63,4 @@ def getIPAdress(domainname): # Main programm if __name__ == '__main__': - #print(getClients(API_BASE_URL, API_USERNAME, API_PASSWORD)) print(addClient(API_BASE_URL, API_USERNAME, API_PASSWORD, DOMAIN_NAME)) \ No newline at end of file