|
Download api-contract-validator-cli/instruction.md from slfs1991slfs/tb_osworld_test: direct link, hf CLI and curl.
- Browser
- Download file 7.13 kB
-
https://huggingface.co/datasets/slfs1991slfs/tb_osworld_test/resolve/main/api-contract-validator-cli/instruction.md
- Command line
-
hf download hf://datasets/slfs1991slfs/tb_osworld_test/api-contract-validator-cli/instruction.md
-
curl -L -o instruction.md https://huggingface.co/datasets/slfs1991slfs/tb_osworld_test/resolve/main/api-contract-validator-cli/instruction.md
7.13 kB
API Contract Validator CLI Tool
Objective
Create a Python CLI tool that validates API endpoints against OpenAPI/Swagger specifications by making HTTP requests and validating responses against the specification.
Requirements
1. Command-Line Interface
CRITICAL: File Structure Requirements
The tool MUST be implemented as a single standalone Python script file with the following exact specifications:
- File Location: The script must be located at exactly
/app/apivalidator(absolute path) - File Type: It must be a regular file (not a directory, symlink, or package)
- Shebang Line: The first line of the file must be
#!/usr/bin/env python3(no blank lines before it) - Executable Permissions: The file must have execute permissions set (use
chmod +x /app/apivalidator) - Execution Method: The tool must work when invoked as
python3 apivalidator <arguments>
Implementation Steps:
- Create a single Python file at
/app/apivalidator - Start the file with
#!/usr/bin/env python3as the very first line - Write your Python code below the shebang
- Make the file executable:
chmod +x /app/apivalidator - Ensure the script can be run with
python3 apivalidator --help(Python will execute the file even though it's not in a package)
Example File Structure:
/app/
└── apivalidator (single Python file, executable)
NOT Allowed:
/app/apivalidator/(directory)/app/apivalidator.py(different name)- Package structure with
__init__.py - Multiple files or modules
Required Arguments:
--spec FILE: Path to OpenAPI/Swagger specification file (JSON or YAML) (required)--base-url URL: Base URL of the API to test (required)--output FILE: Output file path for the validation report (required)
Optional Arguments:
--format {json,markdown,html}: Output format (default: json)--endpoints PATH: Comma-separated list of endpoint paths to test (optional, defaults to all)--methods METHOD: Comma-separated list of HTTP methods to test (optional, defaults to all)--timeout SECONDS: Request timeout in seconds (default: 10)--auth TOKEN: Authentication token (optional, simple format:Bearer TOKENor justTOKEN)--validate-response: Validate response payloads against schema (default: true)--generate-tests: Generate test cases for endpoints (default: false)--verbose: Enable verbose output
2. OpenAPI Specification Parsing
The tool must:
- Parse OpenAPI 3.0 and Swagger 2.0 specifications (both JSON and YAML formats)
- Extract all available endpoints and HTTP methods from the
pathssection - Parse request/response schemas (JSON Schema format)
- Handle basic path parameters, query parameters, request bodies, and headers
- Extract operation information (operation IDs, descriptions, summaries)
3. API Contract Validation
For each endpoint, the tool must:
- Make HTTP Requests: Send actual HTTP requests to the API endpoints using the base URL
- Validate Responses:
- Check HTTP status codes match expected responses in the spec
- If
--validate-responseis enabled, validate response body against response schema using JSON Schema validation
- Report Results: Document which endpoints passed, failed, or had errors
4. Output Formats
JSON Format
The output must be valid JSON with the following structure:
{
"summary": {
"total_endpoints": 5,
"tested_endpoints": 5,
"passed": 3,
"failed": 2,
"errors": 0
},
"endpoints": [
{
"path": "/users",
"method": "GET",
"status": "passed",
"status_code": 200,
"errors": []
}
]
}
Markdown Format
Generate a Markdown report with:
- Summary section with statistics
- Endpoint-by-endpoint validation results
- Failed validations with details
HTML Format
Generate an HTML report with:
- Basic HTML structure (
<html>,<head>,<body>tags) - Summary section
- Endpoint results
5. Error Handling
The tool must handle errors gracefully:
- Invalid Specification: If the spec file cannot be parsed (invalid JSON/YAML), exit with code 1 and print error message to stderr
- Missing Required Arguments: If required arguments (
--spec,--base-url,--output) are missing, exit with code 1 and print error message to stderr - Invalid Format: If
--formatis not one ofjson,markdown, orhtml, exit with code 1 and print error message to stderr - Network Errors: If HTTP requests fail (connection refused, timeout), document the error in the output but continue processing other endpoints
- Invalid Base URL: If the base URL is malformed, exit with code 1
Exit Codes:
0: Success (all endpoints processed, even if some failed validation)1: Error (invalid arguments, invalid spec file, or other fatal errors)
6. Implementation Requirements
- Use standard Python libraries where possible
- Use
requestslibrary for HTTP operations - Use
jsonschemalibrary for schema validation - Parse YAML using
pyyamlorruamel.yaml - Handle both OpenAPI 3.0 and Swagger 2.0 formats
- Provide clear, actionable error messages
- Exit with appropriate exit codes (0 for success, 1 for errors)
Constraints
- Must validate actual HTTP endpoints (not just schema validation)
- Must handle network errors gracefully (document but don't crash)
- Must generate valid JSON/Markdown/HTML output as specified
- Must handle missing or invalid specification files
- Must validate response schemas when
--validate-responseis enabled - Must support basic authentication via
--authparameter
Example Usage
# Basic validation
python3 apivalidator --spec api-spec.yaml --base-url https://api.example.com --output report.json
# With authentication
python3 apivalidator --spec api-spec.json --base-url https://api.example.com --output report.json --auth "Bearer token123"
# Generate Markdown report
python3 apivalidator --spec api-spec.yaml --base-url https://api.example.com --output report.md --format markdown
# Generate HTML report
python3 apivalidator --spec api-spec.yaml --base-url https://api.example.com --output report.html --format html
# Filter specific endpoints
python3 apivalidator --spec api-spec.json --base-url https://api.example.com --output report.json --endpoints "/users,/posts"
# With response validation
python3 apivalidator --spec api-spec.yaml --base-url https://api.example.com --output report.json --validate-response
# Generate test cases
python3 apivalidator --spec api-spec.yaml --base-url https://api.example.com --output report.json --generate-tests
Testing
Your solution will be tested by:
- Verifying the tool exists and is executable
- Testing help message display
- Testing required argument validation
- Testing OpenAPI specification parsing (JSON and YAML)
- Testing output file generation in different formats (JSON, Markdown, HTML)
- Testing error handling for invalid inputs
- Testing optional parameter acceptance (timeout, auth, endpoints, methods, validate-response, generate-tests)