Re-upload: CI infrastructure issue resolved, all tests verified passing
This commit is contained in:
156
examples/example_usage.py
Normal file
156
examples/example_usage.py
Normal file
@@ -0,0 +1,156 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Example usage of the API Mock CLI library.
|
||||
|
||||
This script demonstrates how to use the API Mock CLI programmatically
|
||||
to parse HAR files, generate mock servers, and run them.
|
||||
"""
|
||||
|
||||
import json
|
||||
from api_mock_cli.core.har_parser import HARParser
|
||||
from api_mock_cli.core.mock_generator import MockGenerator
|
||||
from api_mock_cli.core.server import create_mock_server_from_har
|
||||
from api_mock_cli.utils.auth_handler import AuthHandler
|
||||
|
||||
|
||||
def example_parse_har():
|
||||
print("=" * 60)
|
||||
print("Example 1: Parse HAR file")
|
||||
print("=" * 60)
|
||||
|
||||
parser = HARParser(har_file_path="examples/sample.har")
|
||||
result = parser.parse()
|
||||
|
||||
print(f"Base URL: {result.base_url}")
|
||||
print(f"Total entries: {result.entry_count}")
|
||||
print(f"Skipped entries: {result.skipped_count}")
|
||||
print(f"Valid requests: {len(result.requests)}")
|
||||
print()
|
||||
|
||||
for i, req in enumerate(result.requests[:3], 1):
|
||||
print(f"Request {i}:")
|
||||
print(f" Method: {req.method}")
|
||||
print(f" URL: {req.url}")
|
||||
print(f" Status: {req.status_code}")
|
||||
print()
|
||||
|
||||
|
||||
def example_generate_mock_server():
|
||||
print("=" * 60)
|
||||
print("Example 2: Generate Mock Server Code")
|
||||
print("=" * 60)
|
||||
|
||||
parser = HARParser(har_file_path="examples/sample.har")
|
||||
result = parser.parse()
|
||||
|
||||
generator = MockGenerator(result)
|
||||
routes = generator.get_route_summary()
|
||||
|
||||
print(f"Generated {len(routes)} routes:")
|
||||
for route in routes:
|
||||
print(f" [{route['method']}] {route['route']} -> {route['status']}")
|
||||
print()
|
||||
|
||||
code = generator.generate_app()
|
||||
print("Generated Flask app code (first 500 chars):")
|
||||
print(code[:500])
|
||||
print("...")
|
||||
print()
|
||||
|
||||
|
||||
def example_save_mock_server():
|
||||
print("=" * 60)
|
||||
print("Example 3: Save Mock Server to File")
|
||||
print("=" * 60)
|
||||
|
||||
parser = HARParser(har_file_path="examples/sample.har")
|
||||
result = parser.parse()
|
||||
|
||||
generator = MockGenerator(result)
|
||||
output_path = "examples/generated_mock_server.py"
|
||||
generator.save_mock_server(output_path)
|
||||
|
||||
print(f"Mock server saved to: {output_path}")
|
||||
print()
|
||||
|
||||
|
||||
def example_run_mock_server():
|
||||
print("=" * 60)
|
||||
print("Example 4: Run Mock Server")
|
||||
print("=" * 60)
|
||||
|
||||
parser = HARParser(har_file_path="examples/sample.har")
|
||||
result = parser.parse()
|
||||
|
||||
print("Creating mock server...")
|
||||
server = create_mock_server_from_har(result, host="localhost", port=5000)
|
||||
app = server.create_app()
|
||||
|
||||
print("Mock server created successfully!")
|
||||
print("Note: Run the server with: python examples/generated_mock_server.py")
|
||||
print()
|
||||
|
||||
|
||||
def example_auth_handler():
|
||||
print("=" * 60)
|
||||
print("Example 5: Authentication Handling")
|
||||
print("=" * 60)
|
||||
|
||||
handler = AuthHandler()
|
||||
|
||||
headers = {
|
||||
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
auth_info = handler.extract_auth(headers)
|
||||
if auth_info:
|
||||
print(f"Auth Type: {auth_info.auth_type}")
|
||||
print(f"Credentials: {auth_info.credentials}")
|
||||
print(f"Header: {auth_info.header_name}: {auth_info.header_value[:20]}...")
|
||||
print()
|
||||
|
||||
|
||||
def example_data_generator():
|
||||
print("=" * 60)
|
||||
print("Example 6: Generate Fake Data")
|
||||
print("=" * 60)
|
||||
|
||||
from api_mock_cli.core.data_generator import FakeDataGenerator
|
||||
|
||||
generator = FakeDataGenerator()
|
||||
|
||||
sample_data = {
|
||||
"id": 123,
|
||||
"name": "John Doe",
|
||||
"email": "john@example.com",
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
"is_active": True,
|
||||
"profile": {
|
||||
"avatar": "https://example.com/avatar.jpg",
|
||||
"bio": "Sample bio text",
|
||||
},
|
||||
"tags": ["developer", "python"],
|
||||
}
|
||||
|
||||
print("Original data:")
|
||||
print(json.dumps(sample_data, indent=2))
|
||||
print()
|
||||
|
||||
generated = generator.generate_from_dict(sample_data)
|
||||
print("Generated fake data:")
|
||||
print(json.dumps(generated, indent=2))
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
example_parse_har()
|
||||
example_generate_mock_server()
|
||||
example_save_mock_server()
|
||||
example_run_mock_server()
|
||||
example_auth_handler()
|
||||
example_data_generator()
|
||||
|
||||
print("=" * 60)
|
||||
print("All examples completed!")
|
||||
print("=" * 60)
|
||||
158
examples/sample.har
Normal file
158
examples/sample.har
Normal file
@@ -0,0 +1,158 @@
|
||||
{
|
||||
"log": {
|
||||
"version": "1.2",
|
||||
"creator": {
|
||||
"name": "API Mock CLI",
|
||||
"version": "0.1.0"
|
||||
},
|
||||
"entries": [
|
||||
{
|
||||
"startedDateTime": "2024-01-15T10:30:00.000Z",
|
||||
"time": 150,
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "https://api.example.com/users/123",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"},
|
||||
{"name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test"}
|
||||
],
|
||||
"queryString": [
|
||||
{"name": "include", "value": "profile"},
|
||||
{"name": "fields", "value": "name,email"}
|
||||
]
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"statusText": "OK",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"},
|
||||
{"name": "X-Request-Id", "value": "req_abc123"}
|
||||
],
|
||||
"content": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\"id\": 123, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\", \"created_at\": \"2024-01-01T00:00:00Z\", \"is_active\": true}"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"startedDateTime": "2024-01-15T10:30:01.000Z",
|
||||
"time": 200,
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "https://api.example.com/users",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"},
|
||||
{"name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test"}
|
||||
],
|
||||
"queryString": []
|
||||
},
|
||||
"response": {
|
||||
"status": 201,
|
||||
"statusText": "Created",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"}
|
||||
],
|
||||
"content": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\"id\": 456, \"name\": \"Jane Smith\", \"email\": \"jane.smith@example.com\", \"created_at\": \"2024-01-15T10:30:01Z\"}"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"startedDateTime": "2024-01-15T10:30:02.000Z",
|
||||
"time": 100,
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "https://api.example.com/posts",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"}
|
||||
],
|
||||
"queryString": [
|
||||
{"name": "page", "value": "1"},
|
||||
{"name": "limit", "value": "10"}
|
||||
]
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"statusText": "OK",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"}
|
||||
],
|
||||
"content": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\"posts\": [{\"id\": 1, \"title\": \"First Post\", \"author\": \"John Doe\"}, {\"id\": 2, \"title\": \"Second Post\", \"author\": \"Jane Smith\"}], \"total\": 2}"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"startedDateTime": "2024-01-15T10:30:03.000Z",
|
||||
"time": 180,
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "https://api.example.com/products/abc123-def456-ghi789",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"},
|
||||
{"name": "X-API-Key", "value": "sk_test_123456789"}
|
||||
],
|
||||
"queryString": []
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"statusText": "OK",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"}
|
||||
],
|
||||
"content": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\"id\": \"abc123-def456-ghi789\", \"name\": \"Premium Widget\", \"price\": 29.99, \"in_stock\": true}"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"startedDateTime": "2024-01-15T10:30:04.000Z",
|
||||
"time": 250,
|
||||
"request": {
|
||||
"method": "PUT",
|
||||
"url": "https://api.example.com/users/123",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"},
|
||||
{"name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test"}
|
||||
],
|
||||
"queryString": []
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"statusText": "OK",
|
||||
"headers": [
|
||||
{"name": "Content-Type", "value": "application/json"}
|
||||
],
|
||||
"content": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\"id\": 123, \"name\": \"John Updated\", \"email\": \"john.updated@example.com\", \"updated_at\": \"2024-01-15T10:30:04Z\"}"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"startedDateTime": "2024-01-15T10:30:05.000Z",
|
||||
"time": 100,
|
||||
"request": {
|
||||
"method": "DELETE",
|
||||
"url": "https://api.example.com/users/789",
|
||||
"headers": [
|
||||
{"name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test"}
|
||||
],
|
||||
"queryString": []
|
||||
},
|
||||
"response": {
|
||||
"status": 204,
|
||||
"statusText": "No Content",
|
||||
"headers": [],
|
||||
"content": {
|
||||
"mimeType": "text/plain",
|
||||
"text": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user