Apitools

0.1.4 · abandoned · verified Fri Apr 17

Apitools (version 0.1.4) is a Python library designed to assist with JSON schema validation and interaction with REST APIs. It provides utilities for defining and validating data against JSON schemas and for creating client proxies for API interactions. The library has not seen updates since 2016, indicating it is no longer actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates defining a JSON schema, validating data against it using `apitools.jsonschema.validate`, and using `apitools.dataproxy.Dataproxy` for schema-aware access and validation when modifying data.

from apitools.jsonschema import Schema, validate
from apitools.dataproxy import Dataproxy

# Define a simple JSON schema
my_schema = Schema({
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0}
    },
    "required": ["name", "age"]
})

# Validate data against the schema
valid_data = {"name": "Alice", "age": 30}
invalid_data = {"name": "Bob", "age": -5} # age cannot be negative

try:
    validate(valid_data, my_schema)
    print("Valid data:", valid_data)
except Exception as e:
    print("Validation error for valid_data (should not happen):", e)

try:
    validate(invalid_data, my_schema)
    print("Validation attempt for invalid_data:", invalid_data)
except Exception as e:
    print("Validation error for invalid_data (expected):", e)
    # Expected: -5 is less than the minimum of 0

# Using Dataproxy for schema-aware data access
proxy_data = Dataproxy(valid_data, my_schema)
print(f"Proxy Name: {proxy_data.name}")
print(f"Proxy Age: {proxy_data.age}")

# Trying to set an invalid value (will raise ValidationError)
try:
    proxy_data.age = -10
except Exception as e:
    print("Attempt to set invalid age via Dataproxy failed as expected:", e)

view raw JSON →