o
    ˷e                     @   s|   d Z ddlZddlZddlZddlmZ ddlZedZG dd dejj	Z
G dd dZG d	d
 d
ZG dd dZdS )zzSSEClient module to stream realtime updates from the Firebase Database.

Based on a similar implementation from Pyrebase.
    N)	transportz\r\n\r\n|\r\r|\n\nc                       s(   e Zd ZdZ fddZdd Z  ZS )KeepAuthSessionzIA session that does not drop authentication on redirects between domains.c                    s   t t| | d S N)superr   __init__)self
credential	__class__ P/var/www/ideatree/venv/lib/python3.10/site-packages/firebase_admin/_sseclient.pyr   $   s   zKeepAuthSession.__init__c                 C   s   d S r   r   )r   prepared_requestresponser   r   r   rebuild_auth'      zKeepAuthSession.rebuild_auth)__name__
__module____qualname____doc__r   r   __classcell__r   r   r	   r   r   !   s    r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zedd	 Zed
d Z	dS )_EventBufferz6A helper class for buffering and parsing raw SSE data.c                 C   s   g | _ d| _d S N )_buffer_tailr   r   r   r   r   .   s   
z_EventBuffer.__init__c                 C   s.   | j | |  j|7  _| jdd  | _d S )N)r   appendr   )r   charr   r   r   r   2   s   z_EventBuffer.appendc                 C   s6   | j d\}}}|| }t|| _|dd  | _d S )N
r   )buffer_string
rpartitionlistr   r   )r   headsep_remr   r   r   truncate7   s   
z_EventBuffer.truncatec                 C   s(   | j dd  }|dkp|dkp| j dkS )N

zz

)r   )r   last_two_charsr   r   r   is_end_of_field=   s   z_EventBuffer.is_end_of_fieldc                 C   s   d | jS r   )joinr   r   r   r   r   r    B   s   z_EventBuffer.buffer_stringN)
r   r   r   r   r   r   r'   propertyr+   r    r   r   r   r   r   +   s    
r   c                   @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )	SSEClientzSSE client implementation.  c                 K   s^   || _ || _|| _|| _d| _d| _d| _| jdi }d|d< d|d< || jd< |   dS )	a  Initializes the SSEClient.

        Args:
          url: The remote url to connect to.
          session: The requests session.
          retry: The retry interval in milliseconds (optional).
          **kwargs: Extra kwargs that will be sent to ``requests.get()`` (optional).
        TNr   headerszno-cachezCache-Controlztext/event-streamAccept)	urlsessionretryrequests_kwargsshould_connectlast_idbufget_connect)r   r2   r3   r4   kwargsr0   r   r   r   r   J   s   	
zSSEClient.__init__c                 C   s   d| _ d| _| j  dS )zCloses the SSEClient instance.Fr   N)r6   r4   respcloser   r   r   r   r=   c   s   zSSEClient.closec                 C   s^   | j r,| jr| j| jd d< | jj| jfddi| j| _| jjdd| _| j	  dS t
 )z&Connects to the server using requests.r0   zLast-Event-IDstreamT)decode_unicodeN)r6   r7   r5   r3   r9   r2   r<   iter_contentresp_iteratorraise_for_statusStopIterationr   r   r   r   r:   i   s   zSSEClient._connectc                 C   s   | S r   r   r   r   r   r   __iter__t   r   zSSEClient.__iter__c              	   C   s   t t| js=t }|js9zt| j}|| W n t	t
jfy5   t| jd  |   |  Y q
w |jr|j| _t t| j}|d }d|dd  | _t|}|jdkrb|   d S |jdkrid S |jrp|j| _|jrw|j| _|S )Ng     @@r   r)      zcredential is no longer validnull)researchend_of_fieldr8   r   r+   nextrA   r   rC   requestsRequestExceptiontimesleepr4   r:   r'   r    splitr,   Eventparsedataevent_idr7   )r   temp_buffernextcharrO   r#   eventr   r   r   __next__w   s8   



zSSEClient.__next__c                 C   s   |   S r   )rW   r   r   r   r   rJ      s   zSSEClient.nextN)r/   )
r   r   r   r   r   r=   r:   rD   rW   rJ   r   r   r   r   r.   G   s    
%r.   c                   @   s0   e Zd ZdZedZd
ddZedd	 Z	dS )rP   z)Event represents the events fired by SSE.z#(?P<name>[^:]*):?( ?(?P<value>.*))?r   messageNc                 C   s   || _ || _|| _|| _d S r   )rR   
event_typerS   r4   )r   rR   rY   rS   r4   r   r   r   r      s   
zEvent.__init__c                 C   s   |  }| dD ]S}| j|}|du rtd| t q| d }| d }|dkr.q|dkrB|jr>d|j|f |_q||_q|d	krJ||_q|d
krR||_	q|dkr[t
||_q|S )zGiven a possibly-multiline string representing an SSE message, parses it
        and returns an Event object.

        Args:
          raw: the raw data to parse.

        Returns:
          Event: A new ``Event`` with the parameters initialized.
        r   NzInvalid SSE line: "%s"namevaluer   rR   z%s
%srV   idr4   )rO   sse_line_patternmatchwarningswarnSyntaxWarning	groupdictrR   rY   rS   intr4   )clsrawrV   liner^   rZ   r[   r   r   r   rQ      s,   
zEvent.parse)r   rX   NN)
r   r   r   r   rG   compiler]   r   classmethodrQ   r   r   r   r   rP      s    

rP   )r   rG   rM   r_   google.authr   rK   rg   rI   AuthorizedSessionr   r   r.   rP   r   r   r   r   <module>   s   

Y