From 5aae509c93e48a49da8d5eb3ae2c94e5a47d4166 Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Fri, 31 May 2024 10:28:28 +0100 Subject: [PATCH] MVP validation script for API responses --- valudate_api_response.py | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 valudate_api_response.py diff --git a/valudate_api_response.py b/valudate_api_response.py new file mode 100644 index 00000000..91daa14d --- /dev/null +++ b/valudate_api_response.py @@ -0,0 +1,60 @@ +import argparse +import json +from pathlib import Path + +import requests +from response_builder.v1.models.base import RootModel + +ENDPOINTS_ROOT = "api/endpoints/v1" + + +def validate_voting_information(postcode): + url = f"http://localhost:8000/api/v1/postcode/{postcode}/" + + try: + response = requests.get(url) + response.raise_for_status() + data = response.json() + except requests.exceptions.RequestException as e: + print(f"Request failed: {e}") + return + except json.JSONDecodeError as e: + print(f"Failed to decode JSON: {e}") + return + print(data) + print(RootModel.validate(data)) + + +def main(): + endpoints = { + path.name: path + for path in Path(ENDPOINTS_ROOT).glob("*") + if path.is_dir() and not path.name.startswith("_") + } + + # Set up command line argument parsing + parser = argparse.ArgumentParser( + description="Fetch and validate JSON data from an endpoint. " + "Needs local API to be running in a different process." + ) + parser.add_argument( + "--endpoint", + type=str, + help="The API endpoint to query", + choices=endpoints, + required=True, + ) + parser.add_argument( + "--postcode", + type=str, + help="The postcode to use in the query", + required=True, + ) + args = parser.parse_args() + + if args.endpoint == "voting_information": + validate_voting_information(args.postcode) + + +if __name__ == "__main__": + main()