o
    i                     @  s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd dZdS )a  API gateway skeleton for multi-tenant request processing.

The gateway does not replace existing Flask routes yet. Instead, it provides a
composable flow that the main entry point can adopt gradually while leaving all
existing applications under ``/apps`` untouched.
    )annotationsN)Any)Request)RequestContext)SignatureValidator)get_platform_logger)
QuotaGuard)RateLimiter)	AppRouter)DatabaseRouter)StorageRouter)TenantResolverc                   @  sL   e Zd ZdZddddddddd ddZddd!ddZddd!ddZdS )"
APIGatewayz7Coordinate tenant resolution, guards, and app dispatch.N)tenant_resolver	db_routerstorage_router
app_routerrate_limiterquota_guardsignature_validatorr   TenantResolver | Noner   DatabaseRouter | Noner   StorageRouter | Noner   AppRouter | Noner   RateLimiter | Noner   QuotaGuard | Noner   SignatureValidator | NonereturnNonec                C  sb   |pt  | _|p
t | _|pt | _|pt | _|pt | _	|p"t
 | _|p(t | _td| _d S )Napi_gateway)r   r   r   r   r   r   r
   r   r	   r   r   r   r   r   r   logger)selfr   r   r   r   r   r   r    r"   platform/gateway/api_gateway.py__init__   s   zAPIGateway.__init__authrequestr   r&   dict[str, Any] | Noner   c             	   C  sl   | j |}| j|j}| jj||d}| jj||d}| jj	||d}t
tt ||||||p3i dS )z7Resolve the request into a normalized platform context.tenantapp_id)
request_idr*   appdbstoragelimitsr&   )r   resolve_from_requestr   resolve_app_idpathr   resolver   r   describe_limitsr   struuiduuid4)r!   r'   r&   r*   r+   
db_bindingstorage_bindingr0   r"   r"   r#   build_request_context1   s   
z APIGateway.build_request_contextc                C  sT   | j ||d}| jj||j|jd | jj|j|jd | jjdd| id |S )z>Run lightweight gateway checks and return the request context.r%   )r'   r*   r+   r)   zgateway.preflightcontext)extra)	r;   r   checkr*   r-   r   r    infoas_dict)r!   r'   r&   r<   r"   r"   r#   	preflightB   s
   zAPIGateway.preflight)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r'   r   r&   r(   r   r   )__name__
__module____qualname____doc__r$   r;   rA   r"   r"   r"   r#   r      s    r   )rE   
__future__r   r7   typingr   flaskr    platform.gateway.request_contextr   $platform.gateway.signature_validatorr    platform.logging.platform_loggerr   platform.quotas.quota_guardr   platform.quotas.rate_limiterr	   platform.routing.app_routerr
   platform.routing.db_routerr   platform.routing.storage_routerr    platform.tenants.tenant_resolverr   r   r"   r"   r"   r#   <module>   s    