Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP validation script for API responses #528

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions valudate_api_response.py
Original file line number Diff line number Diff line change
@@ -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()