
    yj              	          d dl mZ d dlmZmZ d dl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  ee          Zd	ed
ede	j        dee	j                 fdZd
edeeej                          fdZd	ed
ededdfdZdS )    )	getLogger)Selectselect)AsyncSession)modelsschemas)settings)get_workspace)ResourceNotFoundException)GetOrCreateResultdbworkspace_namewebhookreturnc                 v  K   t          | |           d{V  t          t          j                                      t          j        j        |k              }|                     |           d{V }|                                                                }|D ]B}|j	        |j	        k    r0t          t          j                            |          d          c S Ct          |          t          j        j        k    r"t#          dt          j        j         d          t          j        ||j	                  }|                     |           |                                  d{V  t(                              d|j	                   t          t          j                            |          d	          S )
a  
    Get an existing webhook endpoint for a workspace or create it if missing.

    Args:
        db: Database session
        workspace_name: Name of the workspace
        webhook: Webhook endpoint creation schema

    Returns:
        GetOrCreateResult containing the webhook endpoint and whether it was created

    Raises:
        ResourceNotFoundException: If the workspace does not exist
        ValueError: If the workspace already has the maximum number of webhook
            endpoints
    r   NF)createdz%Maximum number of webhook endpoints (z) reached for this workspace.)r   urlzWebhook endpoint created: %sT)r
   r   r   WebhookEndpointwherer   executescalarsallr   r   r   model_validatelenr	   WEBHOOKMAX_WORKSPACE_LIMIT
ValueErroraddcommitloggerdebug)r   r   r   stmtresult	endpointsendpointwebhook_endpoints           8/DATA/AppData/hermes/projects/honcho/src/crud/webhook.pyget_or_create_webhook_endpointr)      s     , >
:
:
::::::::&())//-? D ::d########F  $$&&I   <7;&&$'66x@@%      ' 9~~)===wH4D4Xwww
 
 	

 -%K   FF
))++
LL/===../?@@$       c                    K   t          t          j                                      t          j        j        | k              S )z
    List all webhook endpoints, optionally filtered by workspace.

    Args:
        db: Database session
        workspace_name: Name of the workspace (optional)

    Returns:
        List of webhook endpoints
    )r   r   r   r   r   r   s    r(   list_webhook_endpointsr,   H   s9       &())//-?  r*   endpoint_idNc                   K   t          t          j                                      t          j        j        |k    t          j        j        |k              }|                     |           d{V }|                                }|st          d| d|           | 	                    |           d{V  | 
                                 d{V  t                              d|           dS )z
    Delete a webhook endpoint.

    Args:
        db: Database session
        endpoint_id: ID of the webhook endpoint

    Raises:
        ResourceNotFoundException: If the webhook endpoint is not found
    NzWebhook endpoint z not found for workspace zWebhook endpoint %s deleted)r   r   r   r   idr   r   scalar_one_or_noner   deleter    r!   r"   )r   r   r-   r#   r$   r&   s         r(   delete_webhook_endpointr2   Z   s      &())//![0-? D ::d########F((**H 
'VVVnVV
 
 	
 ))H


))++
LL.<<<<<r*   )loggingr   
sqlalchemyr   r   sqlalchemy.ext.asyncior   srcr   r   
src.configr	   src.crud.workspacer
   src.exceptionsr   src.utils.typesr   __name__r!   strWebhookEndpointCreater   r)   tupler,   r2    r*   r(   <module>r@      sc         % % % % % % % % / / / / / /               , , , , , , 4 4 4 4 4 4 - - - - - -	8		666 *6 w./	6 6 6 6rE&()*   $==&)=8;=	= = = = = =r*   