o
    Rh                     @   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!				d7d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&			d8de de
e  de"de#de	f
d#d$Z'			d8de de
e  de"de#de	f
d%d&Z(	d9d(e de
e  d)e de#de	f
d*d+Z)			d8de de
e  de"de#dej$f
d,d-Z*	d:de d.e de
e  de
e# dej$f
d/d0Z+G d1d2 d2e,Z-G d3d4 d4e-Z.G d5d6 d6e j/Z0dS );    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   Q/home/air/sanwanet/backup_V2/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 sSw   Y  | }	tj||	|
|d}|jdkro|d |S )zPost the `kwargs` to the APIposthog)tzsentAtr   )clszmaking request: %s to url: %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_responseO   s,   


rS   c                 K   $   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   decidel   s   rX   c                 K   rT   )z*Post the `kwargs to the flags API endpointz/flags/?v=2z$Feature flags evaluated successfullyrU   rV   rW   r   r   r   flagsx   s   rY    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)r[   r   r\   r   r   r   r   remote_config   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   rV   rW   r   r   r   
batch_post   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 successfullyrU   )r   r   requestsr]   r4   rS   )r   r?   r   r   rB   r   r   r   r]      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)rc   rb   )selfrb   rc   r   r   r   __init__   s   
zAPIError.__init__c                 C   s   d}| | j| jS )Nz[PostHog] {0} ({1}))formatrc   rb   )re   msgr   r   r   __str__   s   zAPIError.__str__N)__name__
__module____qualname__r   intstrrf   ri   r   r   r   r   rM      s    rM   c                   @   s   e Zd ZdS )rL   N)rj   rk   rl   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 rd   )rH   r   r   r/   r0   JSONEncoderdefault)re   ro   r   r   r   rq      s   zDatetimeSerializer.defaultN)rj   rk   rl   r   rq   r   r   r   r   r2      s    r2   )NNFr   )NFr   )NrZ   r   )NN)1r0   r,   r   r   r   r   ior   typingr   r   r   ra   dateutil.tzr	   urllib3.util.retryr
   posthog.utilsr   posthog.versionr   adaptersHTTPAdapteradaptersessionsSessionr8   mountr   r   r   r4   rn   r   boolrm   Responser9   rS   rX   rY   r^   r_   r]   	ExceptionrM   rL   rp   r2   r   r   r   r   <module>   s    

#






