o
    g                     @   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 ej Zd	Zd
ZeZde Zde
e defddZ	d)d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 d*dede
e dedede	f
ddZ!	d*d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#d$Z#G d%d& d&e$Z%G d'd( d(e j&Z'dS ),    N)datedatetime)GzipFile)BytesIO)AnyOptionalUnion)tzutc)remove_trailing_slash)VERSIONz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   F/var/www/visachat/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   r3   "   s&   



r3   T)return_jsonr<   success_messager=   c             	   C   st   t d}| jdkr|| |r|  S | S z|  }|d| t| j|d  ttfy9   t| j| jw )Nr   r$   zreceived response: %sdetail)	r&   r'   r4   r-   r*   APIErrorKeyError
ValueErrortext)r<   r>   r=   r7   payloadr   r   r   _process_response?   s   


rE   c                 K   s$   t | |d||fi |}t|ddS )z+Post the `kwargs to the decide API endpointz/decide/?v=3z"Feature flags decided successfullyr>   r3   rE   r   r   r   r   r6   r<   r   r   r   decideN   s   rI   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)r>   r=   rG   rH   r   r   r   
batch_postT   s   rJ   r9   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 successfullyrF   )r
   r   requestsgetr.   rE   )r   r9   r   r   r<   r   r   r   rM   \   s   rM   c                   @   s.   e Zd Zdeeef defddZdd ZdS )r@   statusmessagec                 C   s   || _ || _d S N)rO   rN   )selfrN   rO   r   r   r   __init__c   s   
zAPIError.__init__c                 C   s   d}| | j| jS )Nz[PostHog] {0} ({1}))formatrO   rN   )rQ   msgr   r   r   __str__g   s   zAPIError.__str__N)__name__
__module____qualname__r   intstrrR   rU   r   r   r   r   r@   b   s    r@   c                   @   s   e Zd ZdefddZdS )r,   objc                 C   s$   t |ttfr| S tj| |S rP   )
isinstancer   r   r)   r*   JSONEncoderdefault)rQ   r[   r   r   r   r^   m   s   zDatetimeSerializer.defaultN)rV   rW   rX   r   r^   r   r   r   r   r,   l   s    r,   )NNFr   )NFr   )NN)(r*   r&   r   r   r   r   ior   typingr   r   r   rL   dateutil.tzr	   posthog.utilsr
   posthog.versionr   sessionsSessionr2   r   r   r   r.   rZ   r   boolrY   Responser3   rE   rI   rJ   rM   	Exceptionr@   r]   r,   r   r   r   r   <module>   sl    


$
*
