o
    r)i                     @  s   d dl mZ d dlZd dlZd dlZd dl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mZ dd	lmZ dd
lmZ ddgZG dd deZG dd deZdS )    )annotationsN)cast   )HeadersLike)get_required_header)construct_type)SyncAPIResourceAsyncAPIResource)InvalidWebhookSignatureError)UnwrapWebhookEventWebhooksAsyncWebhooksc                   @  .   e Zd ZdddddZddddddZdS )r   Nsecretpayloadstr | bytesheadersr   r   
str | Nonereturnr   c                C  s8   |du r| j j}| j|||d ttttt|dS )KValidates that the given payload was sent by OpenAI and parses the payload.Nr   r   r   type_value)_clientwebhook_secretverify_signaturer   r   r   jsonloads)selfr   r   r    r!   f/var/www/html/voicebot/backend/venv/lib/python3.10/site-packages/openai/resources/webhooks/webhooks.pyunwrap   s   zWebhooks.unwrap,  r   	tolerancer&   intNonec                  sn  |du r| j j}|du rtdt|d}t|d}t|d}zt|}W n ty1   tddw tt }	|	| |krCtdd||	| krNtddg }
| D ]}|d	re|
	|d
d  qT|
	| qT|drzt
|dd }n| }t|tr|dn|}| d| d| }t
t|| tj   t fdd|
D stdddS )\  Validates whether or not the webhook payload was sent by OpenAI.

        Args:
            payload: The webhook payload
            headers: The webhook headers
            secret: The webhook secret (optional, will use client secret if not provided)
            tolerance: Maximum age of the webhook in seconds (default: 300 = 5 minutes)
        NThe webhook secret must either be set using the env var, OPENAI_WEBHOOK_SECRET, on the client class, OpenAI(webhook_secret='123'), or passed to this functionwebhook-signaturewebhook-timestamp
webhook-id Invalid webhook timestamp formatWebhook timestamp is too oldWebhook timestamp is too newv1,r   whsec_   utf-8.c                 3      | ]	}t  |V  qd S Nhmaccompare_digest.0sigexpected_signaturer!   r"   	<genexpr>p       z,Webhooks.verify_signature.<locals>.<genexpr>AThe given webhook signature does not match the expected signaturer   r   
ValueErrorr   r'   r
   timesplit
startswithappendbase64	b64decodeencode
isinstancebytesdecode	b64encoder9   newhashlibsha256digestanyr    r   r   r   r&   signature_header	timestamp
webhook_idtimestamp_secondsnow
signaturespartdecoded_secretbodysigned_payloadr!   r>   r"   r   ,   sR   







zWebhooks.verify_signaturer   r   r   r   r   r   r   r   
r   r   r   r   r   r   r&   r'   r   r(   __name__
__module____qualname__r#   r   r!   r!   r!   r"   r      s    c                   @  r   )r   Nr   r   r   r   r   r   r   r   r   c                C  sP   |du r| j j}| j|||d t|tr|dn|}ttttt	
|dS )r   Nr   r4   r   )r   r   r   rL   rM   rN   r   r   r   r   r   )r    r   r   r   r^   r!   r!   r"   r#   w   s   zAsyncWebhooks.unwrapr$   r%   r&   r'   r(   c                  sn  |du r| j j}|du rtddt|d}t|d}t|d}zt|}W n ty2   tddw tt }	|	| |krDtdd||	| krOtddg }
| D ]}|d	rf|
	|d
d  qU|
	| qU|dr{t
|dd }n| }t|tr|dn|}| d| d| }t
t|| tj   t fdd|
D stddS )r)   Nr*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   r4   r5   c                 3  r6   r7   r8   r;   r>   r!   r"   r@      rA   z1AsyncWebhooks.verify_signature.<locals>.<genexpr>rB   rC   rU   r!   r>   r"   r      sN   







zAsyncWebhooks.verify_signaturer`   ra   rb   r!   r!   r!   r"   r   v   s    )
__future__r   r9   r   rE   rI   rQ   typingr   _typesr   _utilsr   _modelsr   	_resourcer   r	   _exceptionsr
   #types.webhooks.unwrap_webhook_eventr   __all__r   r   r!   r!   r!   r"   <module>   s    `