This directory contains comprehensive examples demonstrating the HTTP request execution capabilities of the python-postman library.
To run these examples, you need to install the library with execution support:
pip install python-postman[execution]
# or
pip install python-postman httpxDemonstrates fundamental request execution concepts:
- Loading Postman collections
- Creating and configuring RequestExecutor
- Executing single requests
- Executing entire collections
- Basic error handling
Key concepts covered:
RequestExecutorinitializationExecutionContextfor variable management- Async request execution
- Collection execution with results
Shows advanced execution features:
- Runtime variable substitutions
- Request extensions for modifying requests
- Parallel vs sequential collection execution
- Folder execution with variable inheritance
- Performance comparisons
Key concepts covered:
RequestExtensionsfor runtime modifications- Parallel execution patterns
- Variable scoping and inheritance
- Performance optimization
Demonstrates synchronous execution patterns:
- Synchronous request execution
- Context manager usage
- Request and collection convenience methods
- Simple error handling for sync workflows
Key concepts covered:
execute_request_sync()method- Context managers for resource cleanup
- Direct request execution methods
- Synchronous workflow patterns
Covers authentication handling:
- Bearer token authentication
- Basic authentication
- API key authentication
- Collection vs request-level auth
- Dynamic token refresh patterns
Key concepts covered:
- Authentication configuration
- Auth inheritance and precedence
- Dynamic credential management
- Security best practices
Focuses on variable management:
- Variable scoping and precedence
- Nested variable resolution
- Dynamic variable updates
- Error handling for missing variables
- Circular reference protection
Key concepts covered:
- Variable precedence rules
ExecutionContextmanagement- Variable resolution patterns
- Error handling strategies
Each example is self-contained and can be run independently:
# Run basic execution example
python examples/basic_execution.py
# Run advanced features example
python examples/advanced_execution.py
# Run synchronous execution example
python examples/synchronous_execution.py
# Run authentication examples
python examples/authentication_examples.py
# Run variable management examples
python examples/variable_examples.pyThe examples reference sample Postman collection files. You can create these collections in Postman and export them, or create them programmatically:
{
"info": {
"name": "Sample API Collection",
"description": "Example collection for python-postman"
},
"variable": [
{ "key": "base_url", "value": "https://jsonplaceholder.typicode.com" },
{ "key": "api_version", "value": "v1" }
],
"item": [
{
"name": "Get Users",
"request": {
"method": "GET",
"url": "{{base_url}}/users",
"header": [{ "key": "Accept", "value": "application/json" }]
}
}
]
}{
"info": {
"name": "Auth API Collection"
},
"auth": {
"type": "bearer",
"bearer": [{ "key": "token", "value": "{{bearer_token}}" }]
},
"variable": [{ "key": "api_base", "value": "https://api.example.com" }],
"item": [
{
"name": "Get Protected Resource",
"request": {
"method": "GET",
"url": "{{api_base}}/protected"
}
}
]
}import asyncio
from python_postman import PythonPostman
from python_postman.execution import RequestExecutor, ExecutionContext
async def execute_request():
collection = PythonPostman.from_file("collection.json")
async with RequestExecutor() as executor:
context = ExecutionContext(
environment_variables={"base_url": "https://api.example.com"}
)
request = collection.get_request_by_name("API Call")
result = await executor.execute_request(request, context)
return result.success, result.response.status_code
success, status = asyncio.run(execute_request())async def execute_collection():
collection = PythonPostman.from_file("collection.json")
async with RequestExecutor() as executor:
result = await executor.execute_collection(
collection,
parallel=True,
stop_on_error=False
)
return {
"total": result.total_requests,
"successful": result.successful_requests,
"failed": result.failed_requests,
"time_ms": result.total_time_ms
}def setup_execution_context():
return ExecutionContext(
environment_variables={
"env": "production",
"base_url": "https://api.example.com"
},
collection_variables={
"api_version": "v1",
"timeout": "30"
}
)
# Use in request execution
context = setup_execution_context()
context.set_variable("session_token", "abc123", "environment")from python_postman.execution import RequestExtensions
def create_debug_extensions():
return RequestExtensions(
header_extensions={
"X-Debug": "true",
"X-Request-ID": "{{$timestamp}}"
},
param_extensions={
"debug": "1",
"trace": "true"
}
)
# Use in request execution
extensions = create_debug_extensions()
result = await executor.execute_request(request, context, extensions=extensions)- Resource Management: Always use context managers or explicitly close executors
- Error Handling: Check
result.successand handle specific exception types - Variable Scoping: Use appropriate variable scopes for different use cases
- Parallel Execution: Use parallel execution for independent requests
- Authentication: Store sensitive credentials in environment variables
- Testing: Use test scripts in collections for automated validation
- httpx not installed: Install with
pip install httpx - SSL verification errors: Set
verify=Falsein client_config (not recommended for production) - Variable resolution errors: Check variable names and scoping
- Authentication failures: Verify auth configuration and credentials
- Timeout errors: Increase timeout values in client_config
- Enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)- Check variable resolution:
resolved = context.resolve_variables("{{your_variable}}")
print(f"Resolved: {resolved}")- Inspect request preparation:
# Add debug prints in your execution flow
print(f"Request URL: {request.url}")
print(f"Request headers: {request.headers}")If you have additional examples or improvements to existing ones, please feel free to contribute by submitting a pull request.