Added exception handling for unhandled api requests

This commit is contained in:
Timothy Miller 2022-07-30 20:28:54 -04:00
parent 86976e5133
commit 2b9ebdeab2

View File

@ -170,21 +170,25 @@ def cf_api(endpoint, method, config, headers={}, data=False):
"X-Auth-Email": config['authentication']['api_key']['account_email'], "X-Auth-Email": config['authentication']['api_key']['account_email'],
"X-Auth-Key": config['authentication']['api_key']['api_key'], "X-Auth-Key": config['authentication']['api_key']['api_key'],
} }
try:
if(data == False):
response = requests.request(
method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers)
else:
response = requests.request(
method, "https://api.cloudflare.com/client/v4/" + endpoint,
headers=headers, json=data)
if(data == False): if response.ok:
response = requests.request( return response.json()
method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers) else:
else: print("😡 Error sending '" + method +
response = requests.request( "' request to '" + response.url + "':")
method, "https://api.cloudflare.com/client/v4/" + endpoint, print(response.text)
headers=headers, json=data) return None
except Exception as e:
if response.ok: print("😡 An exception occurred while sending '" +
return response.json() method + "' request to '" + endpoint + "': " + str(e))
else:
print("📈 Error sending '" + method +
"' request to '" + response.url + "':")
print(response.text)
return None return None
@ -209,8 +213,8 @@ if __name__ == '__main__':
config = json.loads(config_file.read()) config = json.loads(config_file.read())
except: except:
print("😡 Error reading config.json") print("😡 Error reading config.json")
# wait 60 seconds to prevent excessive logging on docker auto restart # wait 10 seconds to prevent excessive logging on docker auto restart
time.sleep(60) time.sleep(10)
if config is not None: if config is not None:
try: try: