APIMatic Core Interfaces

0.1.8 · active · verified Fri Apr 17

apimatic-core-interfaces provides an abstract layer of functionalities for APIMatic's core library, HTTP client adapters, and generated SDKs. It defines standard interfaces (Abstract Base Classes) for components like HTTP clients, loggers, and authentication managers. The current version is 0.1.8, with irregular but frequent minor releases introducing new interface definitions.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to implement the `IHttpClient` interface, which is a common requirement when extending or customizing APIMatic SDK behavior. It defines a `CustomHttpClient` class that fulfills the `send` and `convert_response` abstract methods. In a real application, the `send` method would perform an actual HTTP request, and `convert_response` would adapt the underlying library's response object to `apimatic_core_interfaces.client.http.HttpResponse`.

import abc
from apimatic_core_interfaces.client.http.http_client import IHttpClient
from apimatic_core_interfaces.client.http.request import HttpRequest
from apimatic_core_interfaces.client.http.response import HttpResponse
from apimatic_core_interfaces.client.http.http_context import HttpContext
from typing import Any # Used for type hinting a generic raw response

class CustomHttpClient(IHttpClient):
    """
    A minimal custom HTTP client implementation demonstrating how to fulfill
    the apimatic-core-interfaces. It shows how to implement the required methods.
    This class would typically wrap an existing HTTP library like `requests` or `httpx`.
    """
    def send(self, request: HttpRequest, context: HttpContext = None) -> HttpResponse:
        print(f"CustomHttpClient received request for: {request.query_url}")
        # Simulate a raw response object/dictionary from an underlying HTTP client.
        # In a real scenario, this would involve making an actual HTTP call.
        raw_response_mock = {
            'status_code': 200,
            'reason_phrase': 'OK',
            'headers': {'Content-Type': 'application/json'},
            'text': '{"message": "Hello from custom client!"}',
            'request_method': request.method,
            'request_url': request.query_url
        }

        # Convert the raw response using the provided interface method
        return self.convert_response(raw_response_mock)

    def convert_response(self, raw_response: Any) -> HttpResponse:
        """
        Converts a raw HTTP client response (e.g., from `requests`, `httpx`, or a mock)
        into an `HttpResponse` object as expected by APIMatic Core.
        """
        return HttpResponse(
            status_code=raw_response.get('status_code', 200),
            reason_phrase=raw_response.get('reason_phrase', 'OK'),
            headers=raw_response.get('headers', {}),
            body=raw_response.get('text', ''),
            request=HttpRequest(
                method=raw_response.get('request_method', 'GET'),
                path=raw_response.get('request_url', '/'),
                query_url=raw_response.get('request_url', '/')
            )
        )

# Example of how to use this custom client (uncomment to run):
# my_custom_client = CustomHttpClient()
# sample_request = HttpRequest(
#     method='GET',
#     path='/api/data',
#     query_url='https://api.example.com/api/data',
#     headers={'Accept': 'application/json'}
# )
# response = my_custom_client.send(sample_request)
# print(f"\nCustom client got response - Status: {response.status_code}, Body: {response.body}")

view raw JSON →