Skip to main content

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 URL
  • method (str): HTTP method (default: "GET")
  • headers (dict, optional): Request headers
  • params (dict, optional): Query parameters
  • timeout (int, optional): Request timeout in seconds (default: 300)
  • api_code (str, optional): Custom API code identifier
  • system (str, optional): System identifier
  • verify (bool, optional): SSL certificate verification (default: True)
  • **kwargs: Additional arguments passed to requests.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 details
  • log_external_api_response(): Log response details
  • log_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

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for your changes
  5. Run the test suite (pytest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. 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

ldc_xac-1.0.5.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ldc_xac-1.0.5-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file ldc_xac-1.0.5.tar.gz.

File metadata

  • Download URL: ldc_xac-1.0.5.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

Hashes for ldc_xac-1.0.5.tar.gz
Algorithm Hash digest
SHA256 572f01e24fd4974d14bc9b8ab1d849167454ec9fa2ec3386246f0bf60f836578
MD5 6b4e42f24ed88c913a23ad83b1e9bf27
BLAKE2b-256 14d02f0125edd22d8b6b330218cbdf5a98d5264cdbb96cf03ba76752b0c42646

See more details on using hashes here.

File details

Details for the file ldc_xac-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: ldc_xac-1.0.5-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

Hashes for ldc_xac-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 25947dda7c9725625b9eb11561a7f6b69919c02debbbf45379fa30378b560933
MD5 a72705a3e5f9f4a53f48708835f3dc5c
BLAKE2b-256 f6a10fde34cc989a5ec0dbf0f40b623a1e4709ee0b2c6bf98b57782ed988630d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page