Skip to main content

Python ScanUpload API Client for seamless integration into ScanUpload SAAS product

Reason this release was yanked:

its obsolete

Project description

ScanUpload API Client - Python Integration Guide

ScanUpload enables the integration and the ability to use QR codes to scan and upload files directly from a mobile device to your webapp.

This guide explains how to integrate scan-upload-api-client into a Python application. The client library requires Python 3.11+ and is compatible with:

  • FastAPI
  • Starlette
  • Django (with async views)
  • Flask (with async support)
  • Any ASGI application

Prerequisites

Installation

pip install scan-upload-api-client

For ASGI/web framework integration:

pip install scan-upload-api-client[asgi]

Configuration

Configure using environment variables or a .env file:

SCANUPLOAD_TARGET_BASE_URL=https://hub.scanupload.net/api/front-end
SCANUPLOAD_ROUTE_PREFIX=/scanupload-api
SCANUPLOAD_TOKEN_ROUTE=/scanupload-api/token
SCANUPLOAD_STRIP_ROUTE_PREFIX=true
SCANUPLOAD_REQUEST_TIMEOUT=90

SCANUPLOAD_API_CLIENT_BASE_URL=https://hub.scanupload.net

KEYCLOAK_SERVER_URL=https://identity.scanupload.net/
KEYCLOAK_REALM=scanupload-hub
KEYCLOAK_CLIENT_ID=your-client-id
KEYCLOAK_CLIENT_SECRET=your-client-secret
KEYCLOAK_SCOPE=openid profile email scanupload.hub

# Optional: enable HTTPS for FastAPI example
UVICORN_SSL_CERTFILE=C:/path/to/localhost.pem
UVICORN_SSL_KEYFILE=C:/path/to/localhost-key.pem

The example apps in examples/ load .env first, then fall back to terminal environment variables.

WARNING: Never commit secrets to source control. Use environment variables or a secrets management system.

Basic Usage

Keycloak Token Client

import asyncio
from scan_upload_api_client import KeycloakClient, ScanUploadProxyOptions

async def main():
    options = ScanUploadProxyOptions(
        keycloak_server_url="https://identity.scanupload.net/",
        keycloak_realm="scanupload-hub",
        keycloak_client_id="your-client-id",
        keycloak_client_secret="your-client-secret",
    )

    async with KeycloakClient(options) as client:
        token = await client.get_client_credentials_token()
        print(f"Access token: {token.access_token}")

asyncio.run(main())

Token Provider with Caching

from scan_upload_api_client import TokenProvider, KeycloakClient, ScanUploadProxyOptions

async def main():
    options = ScanUploadProxyOptions(
        keycloak_server_url="https://identity.scanupload.net/",
        keycloak_realm="scanupload-hub",
        keycloak_client_id="your-client-id",
        keycloak_client_secret="your-client-secret",
        keycloak_early_refresh_seconds=120,
    )

    async with KeycloakClient(options) as keycloak_client:
        provider = TokenProvider(keycloak_client, options)

        # First call fetches token
        token1 = await provider.get_access_token()

        # Second call uses cache
        token2 = await provider.get_access_token()

        assert token1.access_token == token2.access_token

asyncio.run(main())

Download Files

from scan_upload_api_client import ScanUploadApiClient, TokenProvider

async def main():
    # Setup token provider...
    api_client = ScanUploadApiClient(
        base_url="https://hub.scanupload.net",
        token_provider=provider,
    )

    async def process_file(filename: str, content: bytes):
        print(f"Processing {filename} ({len(content)} bytes)")
        # Save or process file content

    await api_client.download_async("session-id-123", process_file)

asyncio.run(main())

FastAPI/Starlette Integration

from fastapi import FastAPI
from contextlib import asynccontextmanager
from scan_upload_api_client import (
    KeycloakClient,
    ScanUploadProxyOptions,
    TokenProvider,
)
from scan_upload_api_client.middleware import ScanUploadProxyMiddleware

options = ScanUploadProxyOptions(
    target_base_url="https://hub.scanupload.net/api/front-end",
    route_prefix="/scanupload-api",
    token_route="/scanupload-api/token",
    strip_route_prefix=True,
    keycloak_server_url="https://identity.scanupload.net/",
    keycloak_realm="scanupload-hub",
    keycloak_client_id="your-client-id",
    keycloak_client_secret="your-client-secret",
)

@asynccontextmanager
async def lifespan(app: FastAPI):
    keycloak_client = KeycloakClient(options)
    token_provider = TokenProvider(keycloak_client, options)
    app.state.scan_upload_token_provider = token_provider
    try:
        yield
    finally:
        await keycloak_client.close()

app = FastAPI(title="ScanUpload FastAPI Example", lifespan=lifespan)
app.add_middleware(ScanUploadProxyMiddleware, options=options)

@app.get("/")
async def root():
    return {"message": "ScanUpload API client is active"}

To run the full FastAPI example app from this repository:

pip install -e .[asgi]
python examples/fastapi_example.py

To run the full Starlette example app from this repository:

pip install -e .[asgi]
python examples/starlette_example.py

By default, the example runs on http://localhost:7021. If both UVICORN_SSL_CERTFILE and UVICORN_SSL_KEYFILE are set, it runs on https://localhost:7021.

Running Tests

# Install test dependencies
pip install -e .[test]

# Run tests
pytest

Compatibility Notes

  • Requires Python 3.11+
  • Fully async (uses httpx.AsyncClient)
  • Thread-safe token caching
  • Compatible with FastAPI, Starlette, Django, Flask

License

See the repository license file for terms.

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

scan_upload_api_client-0.1.0a1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

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

scan_upload_api_client-0.1.0a1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file scan_upload_api_client-0.1.0a1.tar.gz.

File metadata

  • Download URL: scan_upload_api_client-0.1.0a1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for scan_upload_api_client-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 775a4f47b65907dcc828eafe76c42e8fd70844a7118aaa70528ef5ff9dcb0bcf
MD5 f880c26865c9dd14e01d1b826325ef78
BLAKE2b-256 0a9ec366eb96ba116721d989f91397f24faade903c87b3f0356d0481e8254e76

See more details on using hashes here.

File details

Details for the file scan_upload_api_client-0.1.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for scan_upload_api_client-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 a72239d3602bffe99de6490d99d64a8ec8aee511e29c8a620669cc99fc1d0e4a
MD5 d96aceb718ab106bb8fa912ee0541ef5
BLAKE2b-256 c7382bc2d665ad48e8d28c35c791ad99e7126f67b11044a3c7e4ed6ac55e796b

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