Skip to main content

The FAIRagro advanced middleware API client

Project description

Middleware API Client

Python client for the FAIRagro Middleware API with certificate-based authentication (mTLS).

Features

  • ✅ Certificate-based authentication (mutual TLS)
  • ✅ Configuration via YAML files, environment variables, or Docker secrets
  • ✅ Async context manager support
  • ✅ Comprehensive error handling
  • ✅ Type-safe with Pydantic models

Installation

This package is part of the FAIRagro Advanced Middleware project and uses local dependencies.

Quick Start

1. Create Configuration File

# config.yaml
log_level: INFO
api_url: https://your-api-server:8000
client_cert_path: /path/to/client-cert.pem
client_key_path: /path/to/client-key.pem
ca_cert_path: /path/to/ca-cert.pem  # optional
timeout: 30.0
verify_ssl: true

2. Use the Client

import asyncio
from pathlib import Path
from arctrl import ARC, ArcInvestigation
from middleware.api_client import Config, ApiClient


async def main():
    # Load configuration
    config = Config.from_yaml_file(Path("config.yaml"))

    # Create ARC object
    inv = ArcInvestigation.create(identifier="my-arc", title="My ARC")
    arc = ARC.from_arc_investigation(inv)

    # Use client with context manager
    async with ApiClient(config) as client:
        # Send a single ARC
        response = await client.create_or_update_arc(
            rdi="my-rdi",
            arc=arc,
        )
        print(f"ARC status: {response.status}")

        # Or run a harvest workflow
        async def arc_stream():
            yield arc

        harvest = await client.harvest_arcs(
            rdi="my-rdi",
            arcs=arc_stream(),
            expected_datasets=1,
        )
        print(f"Harvest status: {harvest.status}")


asyncio.run(main())

Configuration Options

Option Type Required Default Description
log_level string No INFO Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
api_url string Yes - Base URL of the Middleware API
client_cert_path string No null Path to client certificate (PEM format)
client_key_path string No null Path to client private key (PEM format)
ca_cert_path string No null Path to CA certificate for server verification
timeout float No 30.0 Request timeout in seconds
verify_ssl bool No true Enable SSL certificate verification
max_concurrency int No 10 Maximum concurrent API requests (also default for harvest_arcs)

API Methods

create_or_update_arc(rdi: str, arc: ARC | dict) -> ArcResult

Create or update one ARC in the Middleware API.

Parameters:

  • rdi (str): The RDI identifier (e.g., "edaphobase").
  • arc (ARC | dict): ARC object from arctrl or pre-serialised RO-Crate dict.

Returns:

  • ArcResult: Contains the result of the operation.

Raises:

  • ApiClientError: If the request fails due to HTTP errors or network issues.

Example:

from arctrl import ARC, ArcInvestigation

inv = ArcInvestigation.create(identifier="my-arc-001", title="My ARC")
arc = ARC.from_arc_investigation(inv)

response = await client.create_or_update_arc(
    rdi="edaphobase",
    arc=arc,
)

harvest_arcs(rdi: str, arcs: AsyncIterator[ARC | dict], expected_datasets: int | None = None) -> HarvestResult

Convenience workflow to create a harvest, upload all ARCs from an async iterator, and complete the harvest.

  • Uses config.max_concurrency by default.
  • Continues on item-level submission errors and skips failed items.
  • Cancels the harvest only for catastrophic errors.

All errors are raised as ApiClientError exceptions:

from middleware.api_client import ApiClientError

try:
    response = await client.create_or_update_arc(
        rdi="my-rdi",
        arc=arc,
    )
except ApiClientError as e:
    print(f"API Error: {e}")

Configuration via Environment Variables

You can override configuration values using environment variables:

export API_URL="https://production-api:8000"
export CLIENT_CERT_PATH="/secure/certs/prod-cert.pem"
export CLIENT_KEY_PATH="/secure/certs/prod-key.pem"

Or use Docker secrets in /run/secrets/.

License

This is part of the FAIRagro Advanced Middleware project.

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

fairagro_middleware_api_client-8.6.3.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

fairagro_middleware_api_client-8.6.3-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file fairagro_middleware_api_client-8.6.3.tar.gz.

File metadata

  • Download URL: fairagro_middleware_api_client-8.6.3.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fairagro_middleware_api_client-8.6.3.tar.gz
Algorithm Hash digest
SHA256 b947b1810068f918b34c12481cad31ed7292c1b97a0f88db7401a1d6f799d59b
MD5 e09d89b2c456e1004e27f8dac7240aee
BLAKE2b-256 bb6ff97dfb1b35eb95e4f5875416161c106d89e31d7873e5bf9b16f5d86e2a0d

See more details on using hashes here.

File details

Details for the file fairagro_middleware_api_client-8.6.3-py3-none-any.whl.

File metadata

  • Download URL: fairagro_middleware_api_client-8.6.3-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fairagro_middleware_api_client-8.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 feac2c5ac7cda351deac137d99eb1b56f02f33cd713c43e7764b706eff20ed7b
MD5 db088f8da94f8ee290ac9eec28a7acce
BLAKE2b-256 d9c44716f97465f25eb94bfb89f1ee7b3b7956e2d3db4248345f5f79982f284d

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