From 740447a7f5131ecc49a89a0f9382ea27b001196c Mon Sep 17 00:00:00 2001 From: Nathan Vaughn Date: Sat, 2 Dec 2023 12:00:03 -0600 Subject: [PATCH] Show error on failure --- .gitignore | 4 +++- README.md | 8 ++++---- main.py | 18 ++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index ab31238..b6f24cf 100644 --- a/.gitignore +++ b/.gitignore @@ -137,4 +137,6 @@ dmypy.json # Cython debug symbols cython_debug/ -.vscode/* \ No newline at end of file +.vscode/* +# my own testing +token.txt \ No newline at end of file diff --git a/README.md b/README.md index 25bdd9d..869722a 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ c2547eb745079dac9320b638f5e225cf483cc5cfdda41 ### `files` (optional) -A space seperated list of URLs to purge. Example: +A space separated list of URLs to purge. Example: ```yml files: https://nathanv.me/assets/images/profile.png https://nathanv.me/assets/images/favicons/apple-touch-icon.png @@ -39,7 +39,7 @@ The key `urls` is also accepted for backwards compatibility. ### `tags` (optional) -A space seperated list of tags to purge. Example: +A space separated list of tags to purge. Example: ```yml tags: some-tag another-tag @@ -47,7 +47,7 @@ tags: some-tag another-tag ### `hosts` (optional) -A space seperated list of hosts to purge. Example: +A space separated list of hosts to purge. Example: ```yml hosts: nathanv.me blog.nathanv.me @@ -55,7 +55,7 @@ hosts: nathanv.me blog.nathanv.me ### `prefixes` (optional) -A space seperated list of prefixes to purge. Example: +A space separated list of prefixes to purge. Example: ```yml prefixes: nathanv.me/assets/ blog.nathanv.me/assets diff --git a/main.py b/main.py index 54d3381..5781340 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,9 @@ import argparse +import http.client import json import os import sys from typing import List -from urllib import request def split_and_flatten_list(items: List[str]) -> List[str]: @@ -107,7 +107,8 @@ def main() -> None: req_data["purge_everything"] = True # create the request - url = f"https://api.cloudflare.com/client/v4/zones/{args.cf_zone}/purge_cache" + conn = http.client.HTTPSConnection("api.cloudflare.com") + url = f"/client/v4/zones/{args.cf_zone}/purge_cache" headers = { "Authorization": f"Bearer {args.cf_auth}", "Content-Type": "application/json", @@ -116,31 +117,28 @@ def main() -> None: if os.getenv("NATHANVAUGHN_TESTING"): # when testing, don't actually make a request - print(url) + print(f"https://{conn.host}{url}") print(json.dumps(headers)) print(json.dumps(req_data)) sys.exit() else: print("Request:") - print_blue(url) + print_blue(f"https://{conn.host}{url}") print("Headers:") print_blue(json.dumps(headers, indent=4)) print("Payload:") print_blue(json.dumps(req_data, indent=4)) - req = request.Request( - url, data=json.dumps(req_data).encode("utf-8"), headers=headers - ) - resp = request.urlopen(req) + conn.request("POST", url, json.dumps(req_data).encode("utf-8"), headers) + resp = conn.getresponse() # process response resp_data = json.loads(resp.read()) - print("=========") print("Response:") print_blue(json.dumps(resp_data, indent=4)) - if resp_data["success"] != True: + if resp_data["success"] is not True: print("::error::Success NOT True") sys.exit(1)