Commit 38f38982 authored by Jan Reimes's avatar Jan Reimes
Browse files

refactor(config, http, adobe, test): improve configuration and update Adobe provider tests

* Update model_config in ProviderSettings to ignore extra fields.
* Refactor HTTP client creation to streamline caching logic.
* Adjust Adobe provider to correctly handle input stream mime type.
* Enhance tests for Adobe provider with mock assets and improved assertions.
parent 7afc014b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
class ProviderSettings(BaseSettings):
    """API credentials and application settings loaded from environment variables."""

    model_config = SettingsConfigDict(env_prefix="", case_sensitive=False)
    model_config = SettingsConfigDict(env_prefix="", case_sensitive=False, extra="ignore")

    cloudconvert_api_key: str | None = None
    adobe_client_id: str | None = None
+8 −8
Original line number Diff line number Diff line
@@ -4,9 +4,8 @@ from __future__ import annotations

from pathlib import Path

from hishel import CacheOptions, Controller, SpecificationPolicy
from hishel import CacheOptions, SpecificationPolicy, SyncSqliteStorage
from hishel.httpx import SyncCacheClient
from hishel.storages import SyncSqliteStorage


def create_cached_client(cache_dir: Path | None = None) -> SyncCacheClient:
@@ -14,11 +13,12 @@ def create_cached_client(cache_dir: Path | None = None) -> SyncCacheClient:
    resolved_cache_dir = cache_dir or Path("~/.cache/pdf-remote-converter").expanduser()
    resolved_cache_dir.mkdir(parents=True, exist_ok=True)

    controller = Controller(
        cacheable_methods=["GET", "POST"],
        cacheable_status_codes=[200, 201],
    policy = SpecificationPolicy(
        cache_options=CacheOptions(
            shared=True,
            supported_methods=["GET", "POST"],
        )
    policy = SpecificationPolicy(cache_options=CacheOptions(always_cache=True))
    storage = SyncSqliteStorage(resolved_cache_dir / "http_cache.sqlite")
    )
    storage = SyncSqliteStorage(database_path=resolved_cache_dir / "http_cache.sqlite")

    return SyncCacheClient(storage=storage, controller=controller, policy=policy)
    return SyncCacheClient(storage=storage, policy=policy)
+4 −4
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@ from adobe.pdfservices.operation.io.stream_asset import StreamAsset
from adobe.pdfservices.operation.pdf_services import PDFServices
from adobe.pdfservices.operation.pdf_services_media_type import PDFServicesMediaType
from adobe.pdfservices.operation.pdfjobs.jobs.create_pdf_job import CreatePDFJob
from adobe.pdfservices.operation.pdfjobs.results.create_pdf_job_result import (
    CreatePDFJobResult,
from adobe.pdfservices.operation.pdfjobs.result.create_pdf_result import (
    CreatePDFResult as CreatePDFJobResult,
)
from hishel.httpx import SyncCacheClient

@@ -66,8 +66,8 @@ class AdobeProvider(AbstractProvider):
            credentials = ServicePrincipalCredentials(client_id=self.client_id, client_secret=self.client_secret)
            pdf_services = PDFServices(credentials=credentials)
            media_type = self._media_type_for_format(input_format)
            input_stream = StreamAsset(input_path.read_bytes())
            input_asset = pdf_services.upload(input_stream=input_stream, mime_type=media_type)
            input_stream = StreamAsset(input_path.read_bytes(), mime_type=media_type.value)
            input_asset = pdf_services.upload(input_stream=input_stream)

            job = CreatePDFJob(input_asset=input_asset)
            polling_url = pdf_services.submit(job)
+8 −2
Original line number Diff line number Diff line
@@ -15,6 +15,10 @@ from pdf_remote_converter.exceptions import (
from pdf_remote_converter.providers.adobe import AdobeProvider


class MockAsset:
    """Mock Asset class for testing."""


class DummyResultResource:
    """Minimal resource wrapper for Adobe SDK result."""

@@ -66,10 +70,11 @@ def test_adobe_convert_success(tmp_path: Path) -> None:
    with (
        mock.patch("pdf_remote_converter.providers.adobe.PDFServices") as pdf_services,
        mock.patch("pdf_remote_converter.providers.adobe.ServicePrincipalCredentials") as credentials,
        mock.patch("pdf_remote_converter.providers.adobe.CreatePDFJob"),
    ):
        credentials.return_value = object()
        pdf_instance = pdf_services.return_value
        pdf_instance.upload.return_value = object()
        pdf_instance.upload.return_value = mock.MagicMock()
        pdf_instance.submit.return_value = "polling-url"
        pdf_instance.get_job_result.return_value = dummy_result

@@ -95,10 +100,11 @@ def test_adobe_convert_failure_quota(tmp_path: Path) -> None:
            "pdf_remote_converter.providers.adobe.ServiceUsageException",
            new=RuntimeError,
        ),
        mock.patch("pdf_remote_converter.providers.adobe.CreatePDFJob"),
    ):
        credentials.return_value = object()
        pdf_instance = pdf_services.return_value
        pdf_instance.upload.return_value = object()
        pdf_instance.upload.return_value = mock.MagicMock()
        pdf_instance.submit.side_effect = RuntimeError("Quota exceeded")

        provider = _build_provider()