
    b6i                         d dl Z d dlZd dlZd dlZd dlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ  G d de      Z G d	 d
e      Zy)    N)AnyDict   )BadRequestErrorBody)BadRequestError)WebhooksClientAsyncWebhooksClientc                   (    e Zd ZdZdedededefdZy)r   z
    A client to handle ElevenLabs webhook-related functionality
    Extends the autogenerated client to include custom webhook methods
    rawBody
sig_headersecretreturnc                    |st        t        d            |st        t        d            |j                  d      }d}d}|D ]-  }|j                  d      r|dd }|j                  d	      s,|}/ |r|st        t        d
            t	        |      dz  }t	        t        j
                         dz        dz
  }	||	k  rt        t        d            | d| }
d	t        j                  |j                  d      |
j                  d      t        j                        j                         z   }||k7  rt        t        d            t        j                  |      S a  
        Constructs a webhook event object from a payload and signature.
        Verifies the webhook signature to ensure the event came from ElevenLabs.

        Args:
            rawBody: The webhook request body. Must be the raw body, not a JSON object
            sig_header: The signature header from the request
            secret: Your webhook secret

        Returns:
            The verified webhook event

        Raises:
            BadRequestError: If the signature is invalid or missing
        zMissing signature header)error)bodyzWebhook secret not configured,Nzt=   zv0=z/No signature hash found with expected scheme v0i  i@w z$Timestamp outside the tolerance zone.zutf-8zESignature hash does not match the expected signature hash for payloadr   r   split
startswithinttimehmacnewencodehashlibsha256	hexdigestjsonloadsselfr   r   r   headers	timestamp	signatureheaderreq_timestamp	tolerancemessagedigests               e/root/.openclaw/workspace/visionaryfx/venv/lib/python3.12/site-packages/elevenlabs/webhooks_custom.pyconstruct_eventzWebhooksClient.construct_event   `   " !':A['\]]!':A`'abb""3'		 	#F  &"12J	""5)"			# 	!':Ar'stt I-		d*+n<	9$!':Ag'hii Kq	*MM'"NN7#NN
 )+	 !(/vw  zz'""    N__name__
__module____qualname____doc__strr   r.    r0   r-   r   r      s(    
8#s 8# 8#S 8#T 8#r0   r   c                   (    e Zd ZdZdedededefdZy)r	   zU
    Async version of WebhooksClient that extends the autogenerated async client
    r   r   r   r   c                    |st        t        d            |st        t        d            |j                  d      }d}d}|D ]-  }|j                  d      r|dd }|j                  d	      s,|}/ |r|st        t        d
            t	        |      dz  }t	        t        j
                         dz        dz
  }	||	k  rt        t        d            | d| }
d	t        j                  |j                  d      |
j                  d      t        j                        j                         z   }||k7  rt        t        d            t        j                  |      S r   r   r#   s               r-   r.   z#AsyncWebhooksClient.construct_eventQ   r/   r0   Nr1   r7   r0   r-   r	   r	   L   s(    8#s 8# 8#S 8#T 8#r0   r	   )r!   r   r   r   typingr   r   types.bad_request_error_bodyr   errorsr   webhooks.clientr   AutogeneratedWebhooksClientr	    AutogeneratedAsyncWebhooksClientr7   r0   r-   <module>r@      s;         = # D>#0 >#B=#: =#r0   