o
    pjm                     @  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 ddlmZmZ ddlmZ eddG dd dZd-ddZd.ddZd/ddZ d d! Z!d0d"d#Z"d0d$d%Z#d0d&d'Z$d0d(d)Z%ed*d+ Z&d,S )1zInternal 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)AuthenticatedIdentity)
APIGateway)get_platform_loggerlog_structured)resolve_capabilityT)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%   =/var/www/html/flask_server/bridge_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  s4  t d}t| }t| }t|d||||d t }tt| |t| |dd| d| |p.i d}zAtt| dt| p?d	| d
dddd	| dfd|dd	}	t	  |j
||||	d}
W d   n1 sgw   Y  t|
|dW S  ty } 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   internalzapp:zinternal://flask-server)servicez:invoke)
token_typecaller_app_id)		tenant_idsite_idsubjectissuerkey_id
key_sourcerolesscopesclaims)trusted_identityN)r.   interapp_error)r3   r4   r/   r5   messageerrorstatusr.   rF   )r   _request_id_from_context_caller_app_from_contextr   r   r   _tenant_id_from_context_user_id_from_contextr   _app_contexthandle_normalize_gateway_result	Exceptionr   )contextr.   r/   r0   loggerr5   r3   gatewayfake_requestinternal_identityresultexcr%   r%   r&   call_app(   st   	
rY   r4   
capabilityc                 C  s"   t | }t|||}t| |||S )z<Standard v1 inter-app call authorized by both app manifests.)rK   r   rY   )rR   r4   rZ   r0   r3   r/   r%   r%   r&   call_capabilityl   s   r[   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_statusrG   zUnexpected response payload: rH   z#Unsupported gateway response type: )
isinstancetuple_response_to_jsonr-   
setdefaulttyper(   )rW   r.   responsestatus_coder0   r%   r%   r&   rP   s   s&   



rP   c                 C  s   t | tr| jddS | S )NT)r    )r]   r   r'   )rb   r%   r%   r&   r_      s   
r_   c                 C  s6   | pi  dpi }| dp| pi  d}t|pdS )Ntenantr;   
absolutemsgetr   )rR   rd   r;   r%   r%   r&   rL      s   rL   c                 C  s"   | pi  d}|d u rdS t|S )Nuser_id rf   )rR   rh   r%   r%   r&   rM      s   rM   c                 C  s   t | pi dp
t S )Nr5   )r   rg   r	   rR   r%   r%   r&   rJ      s   rJ   c                 C  s$   t | pi dp| pi dpdS )Napp_idr.   unknown)r   rg   rj   r%   r%   r&   rK      s   $rK   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&   rN      s   

"rN   N)r.   r   r/   r   r0   r-   )r4   r   rZ   r   r0   r-   )r.   r   r"   r   )r"   r   )'r+   
__future__r   
contextlibr   r   dataclassesr   r   typesr   typingr   uuidr	   flaskr
   r   r   r   bridge_platform.auth.jwt_authr   #bridge_platform.gateway.api_gatewayr   'bridge_platform.logging.platform_loggerr   r   bridge_platform.apps.registryr   r   rY   r[   rP   r_   rL   rM   rJ   rK   rN   r%   r%   r%   r&   <module>   s0    

D




