
    ;fJN              	          d dl mZ d dlZd dlZd dlZd dlmZmZ  G d de      Z	 G d de	      Z
 G d d	e	      Z G d
 de	      Z G d de	      Z G d de      Z G d d edg d            Zd eej"                        z  ej$                  _         G d d edg d            Z G d d edg d            Z G d de      Zy)    )
namedtupleN)urljoin	urlencodec                   "     e Zd ZdZ fdZ xZS )PushTicketErrorz%Base class for all push ticket errorsc                     |j                   r|j                   | _         nd| _         t        t        |   | j                          || _        y )NzUnknown push ticket error)messagesuperr   __init__push_response)selfr   	__class__s     U/home/api-vastappli/venv/lib/python3.12/site-packages/exponent_server_sdk/__init__.pyr   zPushTicketError.__init__
   s;      (00DL6DLot-dll;*    __name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r      s    /+ +r   r   c                       e Zd ZdZy)DeviceNotRegisteredErrorzuRaised when the push token is invalid

    To handle this error, you should stop sending messages to this token.
    Nr   r   r   r    r   r   r   r           	r   r   c                       e Zd ZdZy)MessageTooBigErrorzwRaised when the notification was too large.

    On Android and iOS, the total payload must be at most 4096 bytes.
    Nr   r   r   r   r   r      r   r   r   c                       e Zd ZdZy)MessageRateExceededErrorzRaised when you are sending messages too frequently to a device

    You should implement exponential backoff and slowly retry sending messages.
    Nr   r   r   r   r    r    $   r   r   r    c                       e Zd ZdZy)InvalidCredentialsErrora  Raised when our push notification credentials for your standalone app 
    are invalid (ex: you may have revoked them).

    Run expo build:ios -c to regenerate new push notification credentials for
    iOS. If you revoke an APN key, all apps that rely on that key will no
    longer be able to send or receive push notifications until you upload a
    new key to replace it. Uploading a new APN key will not change your users'
    Expo Push Tokens.
    Nr   r   r   r   r"   r"   ,   s     	r   r"   c                   $     e Zd ZdZd fd	Z xZS )PushServerErrora  Raised when the push token server is not behaving as expected

    For example, invalid push notification arguments result in a different
    style of error. Instead of a "data" array containing errors per
    notification, an "error" array is returned.

    {"errors": [
      {"code": "API_ERROR",
       "message": "child "to" fails because ["to" must be a string]. "value" must be an array."
      }
    ]}
    c                 x    || _         || _        || _        || _        t        t
        |   | j                          y N)r	   responseresponse_dataerrorsr
   r$   r   )r   r	   r'   r(   r)   r   s        r   r   zPushServerError.__init__F   s3     *ot-dll;r   )NNr   r   s   @r   r$   r$   9   s    < <r   r$   c                       e Zd ZdZd Zy)PushMessagea,	  An object that describes a push notification request.

    You can override this class to provide your own custom validation before
    sending these to the Exponent push servers. You can also override the
    get_payload function itself to take advantage of any hidden or new
    arguments before this library updates upstream.

        Args:
            to: A token of the form ExponentPushToken[xxxxxxx]
            data: A dict of extra data to pass inside of the push notification.
                The total notification payload must be at most 4096 bytes.
            title: The title to display in the notification. On iOS, this is
                displayed only on Apple Watch.
            body: The message to display in the notification.
            sound: A sound to play when the recipient receives this
                notification. Specify "default" to play the device's default
                notification sound, or omit this field to play no sound.
            ttl: The number of seconds for which the message may be kept around
                for redelivery if it hasn't been delivered yet. Defaults to 0.
            expiration: UNIX timestamp for when this message expires. It has
                the same effect as ttl, and is just an absolute timestamp
                instead of a relative one.
            priority: Delivery priority of the message. 'default', 'normal',
                and 'high' are the only valid values.
            badge: An integer representing the unread notification count. This
                currently only affects iOS. Specify 0 to clear the badge count.
            category: ID of the Notification Category through which to display
                 this notification.
            channel_id: ID of the Notification Channel through which to display
                this notification on Android devices.
            display_in_foreground: Displays the notification when the app is
                foregrounded. Defaults to `false`. No longer available?
            subtitle: The subtitle to display in the notification below the 
                title (iOS only).
            mutable_content: Specifies whether this notification can be 
                intercepted by the client app. In Expo Go, defaults to true.
                In standalone and bare apps, defaults to false. (iOS Only)

    c                 4   t         j                  | j                        st        d      d| j                  i}| j                  | j                  |d<   | j
                  | j
                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   | j                  | j                  |d	<   | j                  | j                  |d
<   | j                  | j                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   | j                   | j                   |d<   |S )NzInvalid push tokentodatatitlebodyttl
expirationprioritysubtitlesoundbadge	channelId
categoryIdmutableContent_displayInForeground)
PushClientis_exponent_push_tokenr-   
ValueErrorr.   r/   r0   r1   r2   r3   r4   r5   r6   
channel_idcategorymutable_contentdisplay_in_foreground)r   payloads     r   get_payloadzPushMessage.get_payload{   sz   009122 $''

 99 "iiGFO::!#zzGG99 "iiGFO88!XXGEN??&$(OOGL!==$"&--GJ==$"&--GJ::!#zzGG::!#zzGG??&#'??GK ==$$(MMGL!+(,(<(<G$% %%1.2.H.HG*+r   N)r   r   r   r   rC   r   r   r   r+   r+   N   s    &N'r   r+   )r-   r.   r/   r0   r5   r1   r2   r3   r6   r?   rA   r>   r4   r@   r&   c                   0    e Zd ZdZdZdZdZdZdZd Z	d Z
y	)

PushTicketzWrapper class for a push notification response.

    A successful single push notification:
        {'status': 'ok'}

    An invalid push token
        {'status': 'error',
         'message': '"adsf" is not a registered push notification recipient'}
    errorokDeviceNotRegisteredMessageTooBigMessageRateExceededc                 <    | j                   t        j                  k(  S z9Returns True if this push notification successfully sent.)statusrE   SUCCESS_STATUSr   s    r   
is_successzPushTicket.is_success   s    {{j7777r   c                 >   | j                         ry| j                  rv| j                  j                  dd      }|t        j                  k(  rt        |       |t        j                  k(  rt        |       |t        j                  k(  rt        |       t        |       zRaises an exception if there was an error. Otherwise, do nothing.

        Clients should handle these errors, since these require custom handling
        to properly resolve.
        NrF   )rP   detailsgetrE   ERROR_DEVICE_NOT_REGISTEREDr   ERROR_MESSAGE_TOO_BIGr   ERROR_MESSAGE_RATE_EXCEEDEDr    r   r   rF   s     r   validate_responsezPushTicket.validate_response   s     ?? <<LL$$Wd3E
>>>.t44*:::(..*@@@.t44 d##r   N)r   r   r   r   ERROR_STATUSrN   rU   rV   rW   rP   rY   r   r   r   rE   rE      s1     LN #8+"78$r   rE   push_messagerM   r	   rS   idc                   4    e Zd ZdZdZdZdZdZdZdZ	d Z
d	 Zy
)PushReceiptzWrapper class for a PushReceipt response. Similar to a PushResponse

    A successful single push notification:
        'data': {
            'id': {'status': 'ok'}
        }
    Errors contain 'errors'

    rF   rG   rH   rI   rJ   InvalidCredentialsc                 <    | j                   t        j                  k(  S rL   )rM   r_   rN   rO   s    r   rP   zPushReceipt.is_success   s    {{k8888r   c                 z   | j                         ry| j                  r| j                  j                  dd      }|t        j                  k(  rt        |       |t        j                  k(  rt        |       |t        j                  k(  rt        |       |t        j                  k(  rt        |       t        |       rR   )rP   rS   rT   r_   rU   r   rV   r   rW   r    INVALID_CREDENTIALSr"   r   rX   s     r   rY   zPushReceipt.validate_response   s     ?? <<LL$$Wd3E???.t44+;;;(..+AAA.t44+999-d33 d##r   N)r   r   r   r   rZ   rN   rU   rV   rW   rc   rP   rY   r   r   r   r_   r_      s7     LN #8+"7.9$r   r_   )r]   rM   r	   rS   c                   b    e Zd ZdZdZdZdZdZddZe	d        Z
d	 Zd
 Zd Zd Zd Zd Zd Zy)r;   zExponent push client

    See full API docs at https://docs.expo.io/versions/latest/guides/push-notifications.html#http2-api
    zhttps://exp.hostz
/--/api/v2d   i  Nc                    || _         | j                   st        j                  | _         || _        | j                  st        j                  | _        || _        d|v r|d   nt        j                  | _        d|v r|d   nt        j                  | _	        d|v r|d   nd| _
        || _        | j                  sCt        j                         | _        | j                  j                  j                  dddd       yy)a  Construct a new PushClient object.

        Args:
            host: The server protocol, hostname, and port.
            api_url: The api url at the host.
            session: Pass in your own requests.Session object if you prefer
                to customize
            force_fcm_v1: If True, send Android notifications via FCM V1, regardless
                of whether a valid credential exists.
        max_message_countmax_receipt_counttimeoutNapplication/jsongzip, deflateacceptzaccept-encodingzcontent-type)hostr;   DEFAULT_HOSTapi_urlDEFAULT_BASE_API_URLforce_fcm_v1DEFAULT_MAX_MESSAGE_COUNTrg   DEFAULT_MAX_RECEIPT_COUNTrh   ri   sessionrequestsSessionheadersupdate)r   rn   rp   ru   rr   kwargss         r   r   zPushClient.__init__  s     	yy"//DI||%::DL( %86$A "("!GQGkGk 	 %86$A "("!GQGkGk 	,5,?vi(T||#++-DLLL  '',#2 2)  r   c                 \    ddl }t        ||j                        xr |j                  d      S )z5Returns `True` if the token is an Exponent push tokenr   NExponentPushToken)six
isinstancestring_types
startswith)clstokenr}   s      r   r<   z!PushClient.is_exponent_push_token=  s0     	5#"2"23 :$$%89	;r   c                    t        | j                  | j                  dz         }| j                  #d| j                  rdndi}|dt	        |      z   z  }| j
                  j                  |t        j                  |D cg c]  }|j                          c}      | j                        }	 |j                         }d	|v rt        d
|||d	         d|vrt        d||      |j                          t        |      t        |d         k7  r9t        dt        |      t        |      dk(  rdndt        |d         fz  ||      g }t        |d         D ]u  \  }}	|j!                  t#        ||   |	j%                  dt"        j&                        |	j%                  dd      |	j%                  dd      |	j%                  dd                   w |S c c}w # t        $ r |j                          t        d|      w xY w)a  Send push notifications

        The server will validate any type of syntax errors and the client will
        raise the proper exceptions for the user to handle.

        Each notification is of the form:
        {
          'to': 'ExponentPushToken[xxx]',
          'body': 'This text gets display in the notification',
          'badge': 1,
          'data': {'any': 'json object'},
        }

        Args:
            push_messages: An array of PushMessage objects.
        z
/push/sendNuseFcmV1truefalse?)r.   ri   Invalid server responser)   Request failedr(   r)   r.   r(   z?Mismatched response length. Expected %d %s but only received %d   receiptreceiptsrM   r	    rS   r]   r[   )r   rn   rp   rr   r   ru   postjsondumpsrC   ri   r=   raise_for_statusr$   len	enumerateappendrE   rT   rZ   )
r   push_messagesurlquery_paramspmr'   r(   push_ticketsipush_tickets
             r   _publish_internalzPushClient._publish_internalE  s   $ dii!<=(&$2C2CQL3<000C<<$$F"R^^-FGLL % "	G$MMOM }$!"2"*0=)6x)@B B &!";"*0=? ? 	!!# }]6%:!;;!m$3}3E3Jic-"78:: +- - 'f(=> 	3NA{!.q!1&??8Z5L5LM'OOIr:'OOIt<"tR023	3 k G  	G %%'!";XFF	Gs   <G+G &G;c                 ,    | j                  |g      d   S )zSends a single push notification

        Args:
            push_message: A single PushMessage object.

        Returns:
           A PushTicket object which contains the results.
        r   )publish_multiple)r   r\   s     r   publishzPushClient.publish  s     $$l^4Q77r   c           	          g }t        j                  d| j                        D ]T  }t        t        j                  |||| j                  z               }|s |S |j                  | j                  |             V |S )zSends multiple push notifications at once

        Args:
            push_messages: An array of PushMessage objects.

        Returns:
           An array of PushTicket objects which contains the results.
        r   )	itertoolscountrg   listisliceextendr   )r   r   r   startchunks        r   r   zPushClient.publish_multiple  s     __Q(>(>? 	?E  !&)?)?!?ABE   6 6u =>	? r   c           	          g }t        j                  d| j                        D ]T  }t        t        j                  |||| j                  z               }|s |S |j                  | j                  |             V |S )zD
        Check receipts in batches of 1000 as per expo docs
        r   )r   r   rh   r   r   r   _check_receipts_internal)r   r   r   r   r   s        r   check_receipts_multiplez"PushClient.check_receipts_multiple  s     __Q(>(>? 	BE  u!&)?)?!?ABE  OOD99%@A	B r   c                     | j                   j                  | j                  | j                  z   dz   d|D cg c]  }|j                   c}i| j
                        }| j                  |      }|S c c}w )z=
        Helper function for check_receipts_multiple
        /push/getReceiptsids)r   ri   )ru   r   rn   rp   r]   ri   validate_and_get_receiptsr   r   r   r'   r   s        r   r   z#PushClient._check_receipts_internal  sn     <<$$II$'::LI[+..IJLL % "
 11(;	 Js   A0c           
         t        j                  | j                  | j                  z   dz   t	        j
                  d|D cg c]  }|j                   c}i      dddd| j                        }| j                  |      }|S c c}w )z5  Checks the push receipts of the given push tickets r   r   rj   rk   rl   )r.   rx   ri   )	rv   r   rn   rp   r   r   r]   ri   r   r   s        r   check_receiptszPushClient.check_receipts  s     ==II$'::<HKHIK -#2 2
 LL	" 11(; Is   Bc                    	 |j                         }d|v rt        d|||d         d|vrt        d||      |j                          |d   }g }|j	                         D ]j  \  }}|j                  t        t               |j                  dt        j                        |j                  dd	      |j                  d
d      |             l |S # t        $ r |j                          t        d|      w xY w)z8
        Validate and get receipts for requests
        r   r)   r   r   r.   r   rM   r	   r   rS   Nr[   )
r   r=   r   r$   itemsr   rE   r+   rT   rZ   )r   r'   r(   retr_idvals         r   r   z$PushClient.validate_and_get_receipts  s   
	G$MMOM }$!"2"*0=)6x)@B B &!";"*0=? ? 	!!# &f-&,,. 	%ID#JJ"%''(J4K4K"L#&779b#9#&779d#;"	$%	% 
E  	G %%'!";XFF		Gs   C &C6)NNNN)r   r   r   r   ro   rq   rs   rt   r   classmethodr<   r   r   r   r   r   r   r   r   r   r   r;   r;     s_     &L' # $"H ; ;N`	8&
")r   r;   )collectionsr   r   r   rv   urllib.parser   r   	Exceptionr   r   r   r    r"   r$   r+   r   _fields__new____defaults__rE   r_   objectr;   r   r   r   <module>r      s    "    +	+i 	+	 		 		 	
	o 
	<i <*T= #
 	Tt $,c+2E2E.F#F    .$<I	K.$b0$="HI0$fx xr   