Skip to main content

External API Caller with comprehensive logging

Project description

LDC-XAC - External API Caller

A Python package for making external API calls with comprehensive logging capabilities.

Features

  • Comprehensive Logging: Log all external API requests, responses, and errors in a structured format
  • Request Tracking: Track request duration and performance metrics
  • Error Handling: Detailed error logging with exception information
  • Data Truncation: Automatic truncation of large request/response data for logging
  • Flexible Configuration: Customizable API codes, system identifiers, and logging parameters

Installation

pip install ldc-xac

Quick Start

from xac import make_external_api_request

# 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(response.status_code)
print(response.json())

Advanced Usage

from xac import ExternalAPICaller

# 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
import requests
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"
)

Configuration

The package uses the following default configuration:

  • MAX_REQUEST_BODY_SIZE: 10,000 characters (configurable in xac.config)

Logging Format

The package logs in JSON format with the following structure:

Request Log

{
    "descr": "External API Request",
    "system": "EXTERNAL",
    "api_code": "API_CALL",
    "request_method": "GET",
    "request_url": "https://api.example.com/data"
}

Response Log

{
    "descr": "External API Response",
    "system": "EXTERNAL",
    "api_code": "API_CALL",
    "response_code": 200,
    "response_for_request": "https://api.example.com/data",
    "time_taken_ms": 150.25
}

Error Log

{
    "descr": "External API Error",
    "system": "EXTERNAL",
    "api_code": "API_CALL",
    "response_code": null,
    "response_for_request": "https://api.example.com/data",
    "time_taken_ms": 5000.0,
    "error_message": "Connection timeout",
    "error_type": "ConnectTimeout"
}

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
  • **kwargs: Additional arguments passed to requests.request()

Returns:

  • requests.Response: The response object

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 xac
pip install -e ".[dev]"

Running Tests

pytest

Code Formatting

black xac/

Type Checking

mypy 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
  3. Make your changes
  4. Add tests for your changes
  5. Run the test suite
  6. Submit a pull request

Support

For support and questions, please open an issue on GitHub.

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.0.tar.gz (6.8 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.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ldc_xac-1.0.0.tar.gz
  • Upload date:
  • Size: 6.8 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.0.tar.gz
Algorithm Hash digest
SHA256 78e9c3d55ea9a117a00c4650fcd76dfb2b703b49e1a56e5373e1e211dd42e25d
MD5 5a9237c3fa416ed15f3de956044878a1
BLAKE2b-256 b4b100a53f975d841835dfbd5e8d035b32a1e6c74d84a3c908b91be1d0678a83

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ldc_xac-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66bc8ec367828f3a91185a83769a63f403955e25112454282c69ee5bbf915511
MD5 6fc0d230f27ac46892e51394b2a9c840
BLAKE2b-256 20addc10305d42f0fcdd670267490ca54f601177ed323888d09ed56818dec1a7

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