Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.4 KB
Raw Blame
"""Application service for compliance document downloads."""

from collections.abc import Callable
from typing import Protocol

from machinery.context import RequestContext
from services.errors.billing import ComplianceRateLimitExceededError


class ComplianceDownloadLink(Protocol):
    @property
    def url(self) -> str: ...


class ComplianceDownloadRateLimiter(Protocol):
    def is_rate_limited(self, key: str, /) -> bool: ...

    def increment_rate_limit(self, key: str, /) -> None: ...


class ComplianceDownloadService:
    def __init__(
        self,
        *,
        fetch_link: Callable[[str, str, str, str, str], ComplianceDownloadLink],
        rate_limiter: ComplianceDownloadRateLimiter,
    ) -> None:
        self._fetch_link = fetch_link
        self._rate_limiter = rate_limiter

    def get_link(
        self,
        *,
        request_context: RequestContext,
        document_name: str,
        ip_address: str,
        device_info: str,
    ) -> ComplianceDownloadLink:
        workspace_id = request_context.active_workspace_id
        account_id = request_context.account_id
        limiter_key = f"{account_id}:{workspace_id}"
        if self._rate_limiter.is_rate_limited(limiter_key):
            raise ComplianceRateLimitExceededError

        link = self._fetch_link(document_name, account_id, workspace_id, ip_address, device_info)
        self._rate_limiter.increment_rate_limit(limiter_key)
        return link