o
    ޻i?                     @  s   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZmZmZ dd	lmZ dd
lmZmZ eddG dd dZd'ddZd(ddZdd Zd)ddZd)ddZd)d d!Zd)d"d#Z ed$d% Z!d&S )*zInternal inter-app client built on top of the platform gateway.

This module lets one app call another through the same gateway flow used by
external requests, without issuing HTTP requests and without direct app-to-app
imports.
    )annotations)contextmanagernullcontext)	dataclassfield)SimpleNamespace)Any)uuid4)FlaskResponsecurrent_apphas_app_context)
APIGateway)get_platform_loggerlog_structuredT)slotsc                   @  sv   e Zd ZU dZded< ded< dZded< dZded	< d
Zded< ee	dZ
ded< ee	dZded< ddddZdS )_InternalRequestzEMinimal request adapter compatible with the current gateway contract.zdict[str, str]headersstrpathPOSTmethodz	127.0.0.1remote_addrzinternal.absolutemshost)default_factorydict[str, Any]jsonargsFsilentboolreturndict[str, Any] | Nonec                 C  s   | j S )N)r   )selfr    r#   platform/interapp/client.pyget_json"   s   z_InternalRequest.get_jsonN)F)r   r   r    r!   )__name__
__module____qualname____doc____annotations__r   r   r   r   dictr   r   r%   r#   r#   r#   r$   r      s   
 r   appr   actionpayloadr+   c                 C  s   t d}t| }t| }t|d||||d t }tt| |t| |dd| d| |p.i d}z!t  |	|||}	W d   n1 sGw   Y  t
|	|d	W S  tyy }
 zt|d
||||t|
d d|t|
dW  Y d}
~
S d}
~
ww )zCall a target app through the internal gateway flow.

    The caller provides the current request context. The inter-app layer reuses
    the gateway rather than importing the target app directly.
    interappinterapp_call)
caller_app
target_appr-   
request_id)zX-Tenant-IDzX-App-IDz	X-User-IDzX-Request-IDz/api/v1//)r   r   r   N)r,   interapp_error)r1   r2   r-   r3   messageerrorstatusr,   r6   )r   _request_id_from_context_caller_app_from_contextr   r   r   _tenant_id_from_context_user_id_from_context_app_contexthandle_normalize_gateway_result	Exceptionr   )contextr,   r-   r.   loggerr3   r1   gatewayfake_requestresultexcr#   r#   r$   call_app&   sT   	
rH   r    r   c                C  s   t | tr)| \}}t|}t |tr |d| |d| |S d|d|dS t| }t |tr:|d| |S t | trG| d| | S d|dt| j dS )Nr,   http_statusr7   zUnexpected response payload: r8   z#Unsupported gateway response type: )
isinstancetuple_response_to_jsonr+   
setdefaulttyper&   )rF   r,   responsestatus_coder.   r#   r#   r$   r@   Z   s&   



r@   c                 C  s   t | tr| jddS | S )NT)r   )rJ   r   r%   )rO   r#   r#   r$   rL   t   s   
rL   c                 C  s6   | pi  dpi }| dp| pi  d}t|pdS )Ntenant	tenant_id
absolutemsgetr   )rB   rQ   rR   r#   r#   r$   r<   z   s   r<   c                 C  s"   | pi  d}|d u rdS t|S )Nuser_id rT   )rB   rV   r#   r#   r$   r=      s   r=   c                 C  s   t | pi dp
t S )Nr3   )r   rU   r	   rB   r#   r#   r$   r:      s   r:   c                 C  s$   t | pi dp| pi dpdS )Napp_idr,   unknown)r   rU   rX   r#   r#   r$   r;      s   $r;   c                  c  sv    t  rt  d V  W d    d S 1 sw   Y  d S td} |   d V  W d    d S 1 s4w   Y  d S )Nplatform_interapp)r   r   r
   app_context)temp_appr#   r#   r$   r>      s   

"r>   N)r,   r   r-   r   r.   r+   )r,   r   r    r   )r    r   )"r)   
__future__r   
contextlibr   r   dataclassesr   r   typesr   typingr   uuidr	   flaskr
   r   r   r   platform.gateway.api_gatewayr    platform.logging.platform_loggerr   r   r   rH   r@   rL   r<   r=   r:   r;   r>   r#   r#   r#   r$   <module>   s*    

4



