o
    hAO                     @   sh   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ ddlm	Z	m
Z
mZmZmZ G dd deZdS )    N)HTTPAdapter)Retry   )AuthenticationErrorInvalidDataErrorFCMErrorFCMServerErrorFCMNotRegisteredErrorc                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d'd
dZdd Zdd Zdd Zd	d	d	d	d	d	d	d	dd	d	ddd	d	d	d	d	d	d	d	d	d	dd	i fddZdd Zd(ddZdd Zg fddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd	S ))BaseAPIa  
    Base class for the pyfcm API wrapper for FCM

    Attributes:
        api_key (str): Firebase API key
        proxy_dict (dict): use proxy (keys: `http`, `https`)
        env (str): for example "app_engine"
        json_encoder
        adapter: requests.adapters.HTTPAdapter()
    zapplication/jsonz#https://fcm.googleapis.com/fcm/sendz$https://iid.googleapis.com/iid/info/i  normalhigh   Nc                 C   s.  |r|| _ ntdd rtdd | _ ntdd | _t | _tdddgtj	t
dgB d}| jd|p9t|d	 | jd
|pEt|d	 | jj|   | j| jt| jd	 |rut|trud|v skd|v ru|| _| jj| g | _|dkrzddlm} |  W n	 ty   Y nw || _d S )NZFCM_API_KEYz;Please provide the api_key in the google-services.json filer   i  i  POST)backoff_factorstatus_forcelistallowed_methodszhttp://)max_retrieszhttps://httphttpsZ
app_enginer   )	appengine)_FCM_API_KEYosgetenvr   ZFCM_REQ_PROXIESrequestsSessionrequests_sessionr   DEFAULT_ALLOWED_METHODS	frozensetmountr   headersupdaterequest_headersINFO_END_POINTINFO_RETRIES
isinstancedictproxiessend_request_responsesZrequests_toolbelt.adaptersr   ZmonkeypatchModuleNotFoundErrorjson_encoder)selfZapi_keyZ
proxy_dictenvr)   adapterretriesr    r.   U/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/pyfcm/baseapi.py__init__-   s4   


zBaseAPI.__init__c                 C   s   | j d| j dS )z
        Generates request headers including Content-Type and Authorization

        Returns:
            dict: request headers
        zkey=)zContent-TypeAuthorization)CONTENT_TYPEr   r*   r.   r.   r/   r!   L   s   zBaseAPI.request_headersc                 c   sR    z| W n t y   t}Y nw |dt|| jD ]}|||| j  V  qdS )a  
        Splits registration ids in several lists of max 1000 registration ids per list

        Args:
            registration_ids (list): FCM device registration ID

        Yields:
            generator: list including lists with registration ids
        r   N)	NameErrorrangelenFCM_MAX_RECIPIENTS)r*   registration_idsxrangeir.   r.   r/   registration_id_chunksX   s   
zBaseAPI.registration_id_chunksc                 C   s   t j|dd| jdddS )z
        Standardized json.dumps function with separators and sorted keys set

        Args:
            data (dict or list): data to be dumped

        Returns:
            string: json
        ),:TF)
separators	sort_keysclsensure_asciiutf8)jsondumpsr)   encode)r*   datar.   r.   r/   
json_dumpsk   s   
zBaseAPI.json_dumpsFc                 K   s<  t  }|rt|dkr||d< n|d |d< |r||d< n|r%d| |d< |r-| j|d< n| j|d< |	r8|	|d< |r>||d	< |
d
urPt|
trL|
|d< ntd|rV||d< |r\||d< |rlt|t rh||d< ntdi |d< |rx||d d< |r||d d< n|r||d d< |rt|tr||d d< ntd|r||d d< n|r||d d< |rt|tr||d d< ntd|r||d d< |rt|tr||d< |r||d d< t|tr|dkr||d d< |r||d d< |r||d d < |r||d d!< |r	|	| |r|d 	| |r|d= | 
|S )"a  
        Parses parameters of FCMNotification's methods to FCM nested json

        Args:
            registration_ids (list, optional): FCM device registration IDs
            topic_name (str, optional): Name of the topic to deliver messages to
            message_body (str, optional): Message string to display in the notification tray
            message_title (str, optional): Message title to display in the notification tray
            message_icon (str, optional): Icon that apperas next to the notification
            sound (str, optional): The sound file name to play. Specify "Default" for device default sound.
            condition (str, optiona): Topic condition to deliver messages to
            collapse_key (str, optional): Identifier for a group of messages
                that can be collapsed so that only the last message gets sent
                when delivery can be resumed. Defaults to `None`.
            delay_while_idle (bool, optional): deprecated
            time_to_live (int, optional): How long (in seconds) the message
                should be kept in FCM storage if the device is offline. The
                maximum time to live supported is 4 weeks. Defaults to `None`
                which uses the FCM default of 4 weeks.
            restricted_package_name (str, optional): Name of package
            low_priority (bool, optional): Whether to send notification with
                the low priority flag. Defaults to `False`.
            dry_run (bool, optional): If `True` no message will be sent but request will be tested.
            data_message (dict, optional): Custom key-value pairs
            click_action (str, optional): Action associated with a user click on the notification
            badge (str, optional): Badge of notification
            color (str, optional): Color of the icon
            tag (str, optional): Group notification by tag
            body_loc_key (str, optional): Indicates the key to the body string for localization
            body_loc_args (list, optional): Indicates the string value to replace format
                specifiers in body string for localization
            title_loc_key (str, optional): Indicates the key to the title string for localization
            title_loc_args (list, optional): Indicates the string value to replace format
                specifiers in title string for localization
            content_available (bool, optional): Inactive client app is awoken
            remove_notification (bool, optional): Only send a data message
            android_channel_id (str, optional): Starting in Android 8.0 (API level 26),
                all notifications must be assigned to a channel. For each channel, you can set the
                visual and auditory behavior that is applied to all notifications in that channel.
                Then, users can change these settings and decide which notification channels from
                your app should be intrusive or visible at all.
            extra_notification_kwargs (dict, optional): More notification keyword arguments
            **extra_kwargs (dict, optional): More keyword arguments

        Returns:
            string: json

        Raises:
            InvalidDataError: parameters do have the wrong type or format
        r   r8   r   to	conditionz
/topics/%sprioritydelay_while_idlecollapse_keyNtime_to_livez'Provided time_to_live is not an integerrestricted_package_namedry_runrF   z,Provided data_message is in the wrong formatZnotificationiconbodybody_loc_keybody_loc_argsz body_loc_args should be an arraytitletitle_loc_keytitle_loc_argsz!title_loc_args should be an arrayandroid_channel_idcontent_availableclick_actionbadgecolortagsound)r%   r6   FCM_LOW_PRIORITYFCM_HIGH_PRIORITYr$   intr   listboolr    rG   )r*   r8   
topic_namemessage_bodyZmessage_titleZmessage_iconr]   rI   rL   rK   rM   rN   Zlow_priorityrO   Zdata_messagerY   rZ   r[   r\   rR   rS   rU   rV   rX   Zremove_notificationrW   Zextra_notification_kwargsextra_kwargsZfcm_payloadr.   r.   r/   parse_payload}   s   N










zBaseAPI.parse_payloadc                 C   sX   | j j| j||d}d|jv r*t|jd dkr*t|jd }t| | ||S |S )N)rF   timeoutzRetry-Afterr   )r   postFCM_END_POINTr   r`   timesleep
do_request)r*   payloadrg   responseZ
sleep_timer.   r.   r/   rl   /  s   
zBaseAPI.do_requestc                 C   s,   g | _ |D ]}| ||}| j | qd S )N)r'   rl   append)r*   payloadsrg   rm   rn   r.   r.   r/   send_request7  s
   zBaseAPI.send_requestc                 C   s   | j j| j| ddidS )z
        Makes a request for registration info and returns the response object

        Args:
            registration_id: id to be checked

        Returns:
            response of registration info request
        detailstrue)params)r   getr"   )r*   registration_idr.   r.   r/   registration_info_request=  s   
z!BaseAPI.registration_info_requestc                 C   s0   g }|D ]}|  |}|jdkr|| q|S )z
        Checks registration ids and excludes inactive ids

        Args:
            registration_ids (list, optional): list of ids to be cleaned

        Returns:
            list: cleaned registration ids
           )rw   status_codero   )r*   r8   Zvalid_registration_idsrv   rr   r.   r.   r/   clean_registration_idsL  s   



zBaseAPI.clean_registration_idsc                 C   s    |  |}|jdkr| S dS )a  
        Returns details related to a registration id if it exists otherwise return None

        Args:
            registration_id: id to be checked

        Returns:
            dict: info about registration id
            None: if id doesn't exist
        rx   N)rw   ry   rC   )r*   rv   rn   r.   r.   r/   get_registration_id_info]  s   

z BaseAPI.get_registration_id_infoc                 C   T   d}d| |d}| j j||d}|jdkrdS |jdkr'| }t|d t )	a  
        Subscribes a list of registration ids to a topic

        Args:
            registration_ids (list): ids to be subscribed
            topic_name (str): name of topic

        Returns:
            True: if operation succeeded

        Raises:
            InvalidDataError: data sent to server was incorrectly formatted
            FCMError: an error occured on the server
        z*https://iid.googleapis.com/iid/v1:batchAdd/topics/rH   Zregistration_tokensrC   rx   T  errorr   rh   ry   rC   r   r   r*   r8   rc   urlrm   rn   r   r.   r.   r/   #subscribe_registration_ids_to_topicm     

z+BaseAPI.subscribe_registration_ids_to_topicc                 C   r|   )	a  
        Unsubscribes a list of registration ids from a topic

        Args:
            registration_ids (list): ids to be unsubscribed
            topic_name (str): name of topic

        Returns:
            True: if operation succeeded

        Raises:
            InvalidDataError: data sent to server was incorrectly formatted
            FCMError: an error occured on the server
        z-https://iid.googleapis.com/iid/v1:batchRemover}   r~   r   rx   Tr   r   r   r   r.   r.   r/   'unsubscribe_registration_ids_from_topic  r   z/BaseAPI.unsubscribe_registration_ids_from_topicc           
      C   s@  g dddg dd}| j D ]}|jdkr~d|jv r%t|jd dkr%td| }|dd}|dd}|d	d}|d
d}|dg }|dd}	|	rQd}|rZ|d | |d  |7  < |d	  |7  < |d
  |7  < |d | |	|d< q|jdkrt	d|jdkrt
|j|jdkrtdtd|S )a  
        Parses the json response sent back by the server and tries to get out the important return variables

        Returns:
            dict: multicast_ids (list), success (int), failure (int), canonical_ids (int),
                results (list) and optional topic_message_id (str but None by default)

        Raises:
            FCMServerError: FCM is temporary not available
            AuthenticationError: error authenticating the sender account
            InvalidDataError: data passed to FCM was incorrecly structured
        r   N)multicast_idssuccessfailurecanonical_idsresultstopic_message_idrx   zcontent-lengthz2FCM server connection error, the response is emptymulticast_idr   r   r   r   
message_idr   r   r   i  z4There was an error authenticating the sender accountr   i  zToken not registeredz%FCM server is temporarily unavailable)r'   ry   r   r`   r   rC   ru   ro   extendr   r   textr	   )
r*   Zresponse_dictrn   Zparsed_responser   r   r   r   r   r   r.   r.   r/   parse_responses  sF   
	





zBaseAPI.parse_responsesc                    sJ   dd l }ddlm}  fdd|D }| | j  ||d}|S )Nr   r   )fetch_tasksc                    s   g | ]
} j d i |qS )r.   )rf   ).0rt   r3   r.   r/   
<listcomp>  s    z.BaseAPI.send_async_request.<locals>.<listcomp>)Z	end_pointr   rp   rg   )asyncioZ	async_fcmr   Znew_event_loopZrun_until_completeri   r!   )r*   Zparams_listrg   r   r   rp   	responsesr.   r3   r/   send_async_request  s
    zBaseAPI.send_async_request)NNNNN)NN)__name__
__module____qualname____doc__r2   ri   r"   r7   r^   r_   r#   r0   r!   r;   rG   rf   rl   rq   rw   rz   r{   r   r   r   r   r.   r.   r.   r/   r
      sd    

 3
7r
   )rC   r   rj   r   Zrequests.adaptersr   urllib3r   errorsr   r   r   r   r	   objectr
   r.   r.   r.   r/   <module>   s    