Skip to content

Create check-if-rest. py #2

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
38 changes: 38 additions & 0 deletions check-if-rest. py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests

# Define the base URL and endpoint to test
base_url = "https://api.example.com"
endpoint = "/users"

# Send a GET request to the endpoint and inspect the response
response = requests.get(base_url + endpoint)

# Check the response status code
if response.status_code != 200:
print("Endpoint does not comply with REST - invalid status code")
exit(1)

# Check the response content type
if response.headers["Content-Type"] != "application/json":
print("Endpoint does not comply with REST - invalid content type")
exit(1)

# Check that the response contains a list of resources
if not isinstance(response.json(), list):
print("Endpoint does not comply with REST - response should be a list")
exit(1)

# Check that each resource in the response contains a unique ID
resource_ids = set()
for resource in response.json():
if "id" not in resource:
print("Endpoint does not comply with REST - resource has no ID")
exit(1)
resource_id = resource["id"]
if resource_id in resource_ids:
print("Endpoint does not comply with REST - duplicate resource ID")
exit(1)
resource_ids.add(resource_id)

# If all checks pass, the endpoint complies with REST
print("Endpoint complies with REST")