
    g                     B   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&                  j)                         Zd	Zd
ZeZdez   Zde
e   defdZ	 d!dede
e   dededej<                  f
dZdddej<                  dededeej<                  e	f   fdZ d"dede
e   dedede	f
dZ!	 d"dede
e   dededej<                  f
dZ"d#dedede
e   de
e   dej<                  f
dZ# G d de$      Z% G d d e jL                        Z'y)$    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                 Z    | xs 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_hosts      D/var/www/openai/venv/lib/python3.12/site-packages/posthog/request.pydetermine_server_hostr      s:    *lO(9LLL$$	1	1$$    api_keygziptimeoutc                 l   t        j                  d      }|}t        j                  t	                     j                         |d<   t        |xs t              |z   }| |d<   t        j                  |t              }	|j                  d|	       dt        d}
|rUd	|
d
<   t               }t        |d      5 }|j                  |	j!                  d             ddd       |j#                         }	t$        j'                  ||	|
|      }|j(                  dk(  r|j                  d       |S # 1 sw Y   TxY w)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ress                 r   r5   r5   "   s    

I
&CD\\UW-779DN
 4
5
<CDO::d 23DII"D)1LG&,"#ic, HHT[[)* - ||~
--$-
IC
#		./J -,s   6!D**D3T)return_jsonr>   success_messager?   c                r   t        j                  d      }| j                  dk(  r%|j                  |       |r| j	                         S | S 	 | j	                         }|j                  d|       t        | j                  |d         # t        t        f$ r! t        | j                  | j                        w xY w)Nr   r&   zreceived response: %sdetail)	r(   r)   r6   r/   r,   APIErrorKeyError
ValueErrortext)r>   r@   r?   r9   payloads        r   _process_responserH   ?   s     

I
&C
#		/"(sxxz1c12((*		)73s(9::j! 2s112s   ;B 0B6c                 <    t        | |d||fi |}t        |d      S )z+Post the `kwargs to the decide API endpointz/decide/?v=3z"Feature flags decided successfullyr@   r5   rH   r   r   r   r   r8   r>   s         r   deciderM   N   s'    
wndG
Fv
FCS2VWWr   c                 >    t        | |d||fi |}t        |dd      S )z6Post the `kwargs` to the batch API endpoint for eventsz/batch/r'   F)r@   r?   rK   rL   s         r   
batch_postrO   T   s,     wiw
A&
ACS2N\abbr   r;   c                     t        |xs t              |z   }t        j                  |d| z  t        d|      }t        |d| d      S )Nz	Bearer %s)Authorizationr    )r%   r   zGET z completed successfullyrJ   )r   r   requestsgetr0   rH   )r   r;   r   r   r>   s        r   rS   rS   \   sK    
 4
5
;C
,,skG6K[e$fpw
xCSD=T2UVVr   c                   ,    e Zd Zdeeef   defdZd Zy)rC   statusmessagec                      || _         || _        y N)rV   rU   )selfrU   rV   s      r   __init__zAPIError.__init__c   s    r   c                 R    d}|j                  | j                  | j                        S )Nz[PostHog] {0} ({1}))formatrV   rU   )rY   msgs     r   __str__zAPIError.__str__g   s     #zz$,,44r   N)__name__
__module____qualname__r	   intstrrZ   r^    r   r   rC   rC   b   s"    uS#X  5r   rC   c                       e Zd ZdefdZy)r.   objc                     t        |t        t        f      r|j                         S t        j
                  j                  | |      S rX   )
isinstancer   r   r+   r,   JSONEncoderdefault)rY   rf   s     r   rj   zDatetimeSerializer.defaultm   s6    cD(+,==?"''c22r   N)r_   r`   ra   r   rj   rd   r   r   r.   r.   l   s    33 3r   r.   )NNF   )NFrk   )NN)(r,   r(   r   r   r   r   ior   typingr   r   r	   rR   dateutil.tzr
   posthog.utilsr   posthog.versionr   sessionsSessionr4   r   r   r   r0   rc   r   boolrb   Responser5   rH   rM   rO   rS   	ExceptionrC   ri   r.   rd   r   r   <module>rv      s     #   ' '   / #$$&2 2 $(
	 	# 	 ]_ ?CVY< JN2			2-02BF2
8c!"2XC Xx} X4 XRU Xil X RTcc c48cKNccW W3 Whsm WXc] W^f^o^o W5y 53)) 3r   