Skip to main content

A wrapper around httpx for working with the Sierra ILS REST API

Project description

sierra-ils-utils

Tests PyPI version Python 3.10+

A Python wrapper around HTTPX for working with the Sierra ILS REST API.

Features

  • Sync and async support - Use request() for blocking calls or async_request() for async/await
  • Automatic authentication - Handles OAuth2 client credentials flow automatically
  • Token management - Automatically refreshes tokens when expired or on 401 responses
  • Retry logic - Configurable retries with exponential backoff for 5xx errors and timeouts
  • Context manager support - Use with or async with for automatic resource cleanup
  • Custom client injection - Inject your own httpx client (e.g., for caching with hishel)
  • Type hints - Full type annotations with py.typed marker for IDE support

Installation

# Install from PyPI
pip install sierra-ils-utils

# Or with uv
uv add sierra-ils-utils

Requires Python 3.10+

Quick Start

from sierra_ils_utils import SierraRESTClient

# Using context manager (recommended)
with SierraRESTClient(
    base_url="https://catalog.library.org/iii/sierra-api/v6/",
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET"
) as client:
    response = client.request("GET", "bibs/", params={"limit": 10})
    response.raise_for_status()
    print(response.json())

Async Usage

import asyncio
from sierra_ils_utils import SierraRESTClient

async def main():
    async with SierraRESTClient(
        base_url="https://catalog.library.org/iii/sierra-api/v6/",
        client_id="YOUR_CLIENT_ID",
        client_secret="YOUR_CLIENT_SECRET"
    ) as client:
        response = await client.async_request("GET", "bibs/", params={"limit": 10})
        response.raise_for_status()
        print(response.json())

asyncio.run(main())

Configuration Options

client = SierraRESTClient(
    base_url="https://catalog.library.org/iii/sierra-api/v6/",
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
    max_retries=3,        # Number of retries for 5xx errors (default: 3)
    backoff_factor=1.0,   # Exponential backoff multiplier (default: 1.0)
    timeout=30.0,         # Request timeout in seconds (default: 30.0)
)

Custom Client Injection

You can inject a custom httpx client for advanced use cases like caching:

import httpx
from sierra_ils_utils import SierraRESTClient

# Example: custom client with different timeout
custom_client = httpx.Client(
    base_url="https://catalog.library.org/iii/sierra-api/v6/",
    timeout=60.0
)

client = SierraRESTClient(
    base_url="https://catalog.library.org/iii/sierra-api/v6/",
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
    sync_client=custom_client  # or async_client for async
)

Utilities

SierraDateTime and SierraDate

Helpers for Sierra API-compatible date formatting:

from sierra_ils_utils import SierraDateTime, SierraDate
from datetime import timedelta

# Current timestamp in Sierra format
now = SierraDateTime.now()
print(now)  # 2025-01-30T19:31:43Z

# Date arithmetic works
yesterday = now - timedelta(days=1)

# Create date ranges for API queries
date_range = f"[{yesterday},{now}]"

# Query items created in the last day
response = client.request(
    "GET", "items/",
    params={"createdDate": date_range, "limit": 2000}
)

Timezone Support

# Parse dates with timezone conversion
dt = SierraDateTime.from_string('2025-01-06 00:00:00', 'America/New_York')
print(dt)  # 2025-01-06T05:00:00Z (converted to UTC)

get_max_record_id()

Find the maximum valid record ID using efficient binary search:

from sierra_ils_utils import SierraRESTClient, get_max_record_id

with SierraRESTClient(...) as client:
    max_bib_id = get_max_record_id(client, "bibs/")
    print(f"Max bib ID: {max_bib_id}")  # e.g., 3934049

Development

# Clone and install
git clone https://github.com/chimpy-me/sierra-ils-utils.git
cd sierra-ils-utils
uv sync --all-extras

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest -v

Version

from sierra_ils_utils import __version__
print(__version__)  # 0.1.0

License

This project is released under the MIT License.

Author

Ray Voelker – ray.voelker@gmail.com

Contributing

Issues and pull requests welcome at 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

sierra_ils_utils-0.1.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

sierra_ils_utils-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file sierra_ils_utils-0.1.0.tar.gz.

File metadata

  • Download URL: sierra_ils_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sierra_ils_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b3d50a6fd0b1153d393a7217075db6f528ff157a554f98b50e465a383a9bf1e9
MD5 0eed5b1cc726f33a5b1a7e14a2852829
BLAKE2b-256 880a8500700ceecb9d91dbc46c6f26b7717210800d68083a5390bf45ddafe93a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sierra_ils_utils-0.1.0.tar.gz:

Publisher: publish.yml on chimpy-me/sierra-ils-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sierra_ils_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sierra_ils_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e4b5f1e82ca9e05ed37b386c2e5eaffc5321c1440a1d10990f3de405d0997fc
MD5 dc04c5fdb7269baa8f3b952a2db541d1
BLAKE2b-256 5b2f2ec128403358d2a24f7709948ec28400c2a823d2da2f5685f7074b7b10ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for sierra_ils_utils-0.1.0-py3-none-any.whl:

Publisher: publish.yml on chimpy-me/sierra-ils-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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