External API Caller with comprehensive logging
Project description
LDC-XAC
External API Caller with Comprehensive Logging
A Python package for making external API calls with automatic request/response logging, error tracking, and performance monitoring.
Features
- Automatic Logging: Structured JSON logs for all API requests and responses
- Performance Tracking: Request duration and timing metrics
- Error Handling: Detailed error logging with exception information
- Data Management: Automatic truncation of large payloads for logging
- Flexible Configuration: Custom API codes, system identifiers, and SSL options
- SSL Support: Configurable SSL certificate verification
Installation
pip install ldc-xac
Quick Start
Basic Usage
import logging
from ldc_xac import make_external_api_request
# Configure logging to see API call logs
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Make a simple GET request
response = make_external_api_request(
url="https://api.example.com/data",
method="GET",
api_code="EXAMPLE_API",
system="MY_SYSTEM"
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
POST Request with JSON Data
response = make_external_api_request(
url="https://api.example.com/users",
method="POST",
json={"name": "John Doe", "email": "john@example.com"},
api_code="CREATE_USER",
system="USER_MANAGEMENT"
)
Handling SSL Issues
# For testing with self-signed certificates or SSL issues
response = make_external_api_request(
url="https://api.example.com/data",
method="GET",
api_code="EXAMPLE_API",
system="MY_SYSTEM",
verify=False # Disable SSL verification
)
Advanced Usage
Manual Logging Control
from ldc_xac import ExternalAPICaller
import requests
# Log a request manually
start_time = ExternalAPICaller.log_external_api_request(
url="https://api.example.com/users",
method="POST",
api_code="CREATE_USER",
system="USER_MANAGEMENT"
)
# Make your request
response = requests.post(
"https://api.example.com/users",
json={"name": "John Doe", "email": "john@example.com"}
)
# Log the response
ExternalAPICaller.log_external_api_response(
url="https://api.example.com/users",
response=response,
start_time=start_time,
api_code="CREATE_USER",
system="USER_MANAGEMENT"
)
Request with Headers and Parameters
response = make_external_api_request(
url="https://api.example.com/search",
method="GET",
headers={
"Authorization": "Bearer your-token",
"Content-Type": "application/json"
},
params={"q": "search term", "limit": 10},
api_code="SEARCH_API",
system="SEARCH_SYSTEM"
)
Configuration
The package uses the following default configuration:
- MAX_REQUEST_BODY_SIZE: 10,000 characters (configurable in
ldc_xac.config)
Logging Format
The package logs in structured JSON format with the following structure:
Request Log
{
"descr": "External API Request",
"system": "MY_SYSTEM",
"api_code": "EXAMPLE_API",
"request_method": "GET",
"request_url": "https://api.example.com/data"
}
Response Log
{
"descr": "External API Response",
"system": "MY_SYSTEM",
"api_code": "EXAMPLE_API",
"response_code": 200,
"response_for_request": "https://api.example.com/data",
"time_taken_ms": 150.25
}
Error Log
{
"descr": "External API Error",
"system": "MY_SYSTEM",
"api_code": "EXAMPLE_API",
"response_code": null,
"response_for_request": "https://api.example.com/data",
"time_taken_ms": 5000.0,
"error_message": "Connection timeout",
"error_type": "ConnectTimeout"
}
Warning Log (4xx/5xx responses)
{
"descr": "External API Response",
"system": "MY_SYSTEM",
"api_code": "EXAMPLE_API",
"response_code": 404,
"response_for_request": "https://api.example.com/data",
"time_taken_ms": 250.50,
"request_data": {
"params": {"id": "123"},
"json": {"name": "John Doe"}
}
}
API Reference
make_external_api_request()
Make an external API request with automatic logging.
Parameters:
url(str): The API endpoint URLmethod(str): HTTP method (default: "GET")headers(dict, optional): Request headersparams(dict, optional): Query parameterstimeout(int, optional): Request timeout in seconds (default: 300)api_code(str, optional): Custom API code identifiersystem(str, optional): System identifierverify(bool, optional): SSL certificate verification (default: True)**kwargs: Additional arguments passed torequests.request()
Returns:
requests.Response: The response object
Raises:
Exception: Any exception that occurs during the request
ExternalAPICaller
Main class for logging external API calls.
Methods:
log_external_api_request(): Log request detailslog_external_api_response(): Log response detailslog_external_api_error(): Log error details_truncate_large_data(): Truncate large data for logging
Development
Setup Development Environment
git clone https://github.com/ayushsonar-lendenclub/ldc-xac
cd ldc-xac
pip install -e ".[dev]"
Running Tests
pytest
Code Formatting
black ldc_xac/
Type Checking
mypy ldc_xac/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Run the test suite (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Support
For support and questions, please open an issue on GitHub.
Use Cases
- Microservices Communication: Log API calls between services
- Third-party API Integration: Track external API usage and performance
- API Monitoring: Monitor API health and response times
- Debugging: Detailed logs for troubleshooting API issues
- Compliance: Structured logging for audit trails
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ldc_xac-1.0.4.tar.gz.
File metadata
- Download URL: ldc_xac-1.0.4.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e44e22933e789c365adf13a02a805f00a8bc6bdf258506316c8474bfc39aba3e
|
|
| MD5 |
ec73ef2bd1b42b654d13cd0015ab9c48
|
|
| BLAKE2b-256 |
c115ec2d012f90086e2a0b81ae5201da4fc7505142b428792e45ca1d67a39f96
|
File details
Details for the file ldc_xac-1.0.4-py3-none-any.whl.
File metadata
- Download URL: ldc_xac-1.0.4-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45340750a417012affe5079987ec34645a1c9cbbcedf24518e0812eb696f2673
|
|
| MD5 |
37c7f5989eee46594be82f5a8b76e256
|
|
| BLAKE2b-256 |
d2a0051d8ef2d1099b06e86b95c8e52aaee64bed6d40b8414f77d5e03c0aa269
|