o
    Dg                     @   s   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
m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 ejjed
d
d
ddZej Zede dZdZeZde Zde
e  de fddZ!	d5de de
e  de"de#dej$f
ddZ%dddej$de d e"deej$e	f fd!d"Z&d6de de
e  de"de#de	f
d#d$Z'd7d&e de
e  d'e de#de	f
d(d)Z(	d6de de
e  de"de#dej$f
d*d+Z)d8de d,e de
e  de
e# dej$f
d-d.Z*G d/d0 d0e+Z,G d1d2 d2e,Z-G d3d4 d4e j.Z/dS )9    N)datedatetime)GzipFile)BytesIO)AnyOptionalUnion)tzutc)Retry)remove_trailing_slash)VERSION   )totalconnectread)max_retrieszhttps://zhttps://us.i.posthog.comzhttps://eu.i.posthog.comzposthog-python/hostreturnc                 C   s,   | pt }t|}|dv rtS |dkrtS |S )z"Determines the server host to use.)zhttps://app.posthog.comzhttps://us.posthog.comzhttps://eu.posthog.com)DEFAULT_HOSTr   US_INGESTION_ENDPOINTEU_INGESTION_ENDPOINT)r   host_or_defaulttrimmed_host r   i/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/posthog/request.pydetermine_server_host!   s   r   F   api_keygziptimeoutc                 K   s   t d}|}tjt d |d< t|pt| }| |d< tj	|t
d}	|d|	 dtd}
|r[d	|
d
< t }t|dd}||	d W d   n1 sRw   Y  | }	tj||	|
|d}|jdkrn|d |S )zPost the `kwargs` to the APIposthog)tzsentAtr   )clszmaking request: %szapplication/json)zContent-Type
User-Agentr   zContent-Encodingw)fileobjmodezutf-8N)dataheadersr      data uploaded successfully)logging	getLoggerr   nowr	   	isoformatr   r   jsondumpsDatetimeSerializerdebug
USER_AGENTr   r   writeencodegetvalue_sessionpoststatus_code)r   r   pathr   r   kwargslogbodyurlr(   r)   bufgzresr   r   r   r9   -   s&   



r9   T)return_jsonrB   success_messagerC   c             	   C   s   t d}| jdkr:|| |r|  n| }t|tr8d|v r8t|d tr8d|d v r8|d t	| jd|S z|  }|d| t
| j|d  ttfy\   t
| j| jw )	Nr    r*   quotaLimitedfeature_flagsz[FEATURE FLAGS] PostHog feature flags quota limited, resetting feature flag data.  Learn more about billing limits at https://posthog.com/docs/billing/limits-alertszFeature flags quota limitedzreceived response: %sdetail)r,   r-   r:   r3   r0   
isinstancedictlistwarningQuotaLimitErrorAPIErrorKeyError
ValueErrortext)rB   rD   rC   r=   responsepayloadr   r   r   _process_responseJ   s,   


rS   c                 K   s$   t | |d||fi |}t|ddS )z+Post the `kwargs to the decide API endpointz/decide/?v=4z"Feature flags decided successfullyrD   r9   rS   r   r   r   r   r<   rB   r   r   r   decideg   s   rW    personal_api_keykeyc                 C   s   t | d| d||S )z<Get remote config flag value from remote_config API endpointz%/api/projects/@current/feature_flags/z/remote_config/)get)rY   r   rZ   r   r   r   r   remote_configm   s   r\   c                 K   s&   t | |d||fi |}t|dddS )z6Post the `kwargs` to the batch API endpoint for eventsz/batch/r+   F)rD   rC   rU   rV   r   r   r   
batch_postr   s   r]   r?   c                 C   s>   t |pt| }tj|d|  td|d}t|d| ddS )Nz	Bearer %s)Authorizationr$   )r)   r   zGET z completed successfullyrT   )r   r   requestsr[   r4   rS   )r   r?   r   r   rB   r   r   r   r[   z   s   r[   c                   @   s.   e Zd Zdeeef defddZdd ZdS )rM   statusmessagec                 C   s   || _ || _d S N)ra   r`   )selfr`   ra   r   r   r   __init__   s   
zAPIError.__init__c                 C   s   d}| | j| jS )Nz[PostHog] {0} ({1}))formatra   r`   )rc   msgr   r   r   __str__   s   zAPIError.__str__N)__name__
__module____qualname__r   intstrrd   rg   r   r   r   r   rM      s    rM   c                   @   s   e Zd ZdS )rL   N)rh   ri   rj   r   r   r   r   rL      s    rL   c                   @   s   e Zd ZdefddZdS )r2   objc                 C   s$   t |ttfr| S tj| |S rb   )rH   r   r   r/   r0   JSONEncoderdefault)rc   rm   r   r   r   ro      s   zDatetimeSerializer.defaultN)rh   ri   rj   r   ro   r   r   r   r   r2      s    r2   )NNFr   )NFr   )NrX   r   )NN)0r0   r,   r   r   r   r   ior   typingr   r   r   r_   dateutil.tzr	   urllib3.util.retryr
   posthog.utilsr   posthog.versionr   adaptersHTTPAdapteradaptersessionsSessionr8   mountr   r   r   r4   rl   r   boolrk   Responser9   rS   rW   r\   r]   r[   	ExceptionrM   rL   rn   r2   r   r   r   r   <module>   s    


$$
*
