o
    ˷ea                     @   s"  d Z ddlZ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 dZ	dd Z
d+d	d
Zd+ddZd+ddZd+ddZd,ddZd,ddZdd Zdd Zdd Zdd ZG dd dZG dd  d ZG d!d" d"ZG d#d$ d$eZG d%d& d&eZG d'd( d(ZG d)d* d*ZdS )-zFirebase Project Management module.

This module enables management of resources in Firebase projects, such as Android and iOS apps.
    N)
exceptions)_http_client)_utils_project_managementc                 C   s   t | ttS N)r   get_app_service_PROJECT_MANAGEMENT_ATTRIBUTE_ProjectManagementServiceapp r   X/var/www/ideatree/venv/lib/python3.10/site-packages/firebase_admin/project_management.py_get_project_management_service#   s   r   c                 C      t | t|dS )zObtains a reference to an Android app in the associated Firebase project.

    Args:
        app_id: The app ID that identifies this Android app.
        app: An App instance (optional).

    Returns:
        AndroidApp: An ``AndroidApp`` instance.
    app_idservice)
AndroidAppr   r   r   r   r   r   android_app'      
r   c                 C   r   )zObtains a reference to an iOS app in the associated Firebase project.

    Args:
        app_id: The app ID that identifies this iOS app.
        app: An App instance (optional).

    Returns:
        IOSApp: An ``IOSApp`` instance.
    r   )IOSAppr   r   r   r   r   ios_app4   r   r   c                 C      t |  S )zLists all Android apps in the associated Firebase project.

    Args:
        app: An App instance (optional).

    Returns:
        list: a list of ``AndroidApp`` instances referring to each Android app in the Firebase
        project.
    )r   list_android_appsr
   r   r   r   r   A   s   
r   c                 C   r   )zLists all iOS apps in the associated Firebase project.

    Args:
        app: An App instance (optional).

    Returns:
        list: a list of ``IOSApp`` instances referring to each iOS app in the Firebase project.
    )r   list_ios_appsr
   r   r   r   r   N   s   	r   c                 C      t || |S )al  Creates a new Android app in the associated Firebase project.

    Args:
        package_name: The package name of the Android app to be created.
        display_name: A nickname for this Android app (optional).
        app: An App instance (optional).

    Returns:
        AndroidApp: An ``AndroidApp`` instance that is a reference to the newly created app.
    )r   create_android_app)package_namedisplay_namer   r   r   r   r   Z      r   c                 C   r   )aR  Creates a new iOS app in the associated Firebase project.

    Args:
        bundle_id: The bundle ID of the iOS app to be created.
        display_name: A nickname for this iOS app (optional).
        app: An App instance (optional).

    Returns:
        IOSApp: An ``IOSApp`` instance that is a reference to the newly created app.
    )r   create_ios_app)	bundle_idr   r   r   r   r   r!   h   r    r!   c                 C   s$   | d u s	t | tr| S td|)Nz{0} must be a string.
isinstancestr
ValueErrorformatobj
field_namer   r   r   _check_is_string_or_nonev   s   r+   c                 C   s    t | tr	| r	| S td|)Nz{0} must be a non-empty string.r#   r(   r   r   r   _check_is_nonempty_string|   s   r,   c                 C   s   | d u rd S t | |S r   )r,   r(   r   r   r   !_check_is_nonempty_string_or_none   s   
r-   c                 C   s   | d u rt d|| S )Nz{0} cannot be None.)r&   r'   r(   r   r   r   _check_not_none   s   r.   c                   @   sT   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )r   a!  A reference to an Android app within a Firebase project.

    Note: Unless otherwise specified, all methods defined in this class make an RPC.

    Please use the module-level function ``android_app(app_id)`` to obtain instances of this class
    instead of instantiating it directly.
    c                 C      || _ || _d S r   _app_id_serviceselfr   r   r   r   r   __init__      
zAndroidApp.__init__c                 C      | j S )zReturns the app ID of the Android app to which this instance refers.

        Note: This method does not make an RPC.

        Returns:
            string: The app ID of the Android app to which this instance refers.
        r1   r4   r   r   r   r         	zAndroidApp.app_idc                 C      | j | jS )a&  Retrieves detailed information about this Android app.

        Returns:
            AndroidAppMetadata: An ``AndroidAppMetadata`` instance.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service.
        )r2   get_android_app_metadatar1   r9   r   r   r   get_metadata      
zAndroidApp.get_metadatac                 C      | j | j|S )ah  Updates the display name attribute of this Android app to the one given.

        Args:
            new_display_name: The new display name for this Android app.

        Returns:
            NoneType: None.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service.
        )r2   set_android_app_display_namer1   r4   new_display_namer   r   r   set_display_name      zAndroidApp.set_display_namec                 C   r;   )zFRetrieves the configuration artifact associated with this Android app.)r2   get_android_app_configr1   r9   r   r   r   
get_config      zAndroidApp.get_configc                 C   r;   )a5  Retrieves the entire list of SHA certificates associated with this Android app.

        Returns:
            list: A list of ``SHACertificate`` instances.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service.
        )r2   get_sha_certificatesr1   r9   r   r   r   rH      r>   zAndroidApp.get_sha_certificatesc                 C   r?   )aw  Adds a SHA certificate to this Android app.

        Args:
            certificate_to_add: The SHA certificate to add.

        Returns:
            NoneType: None.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service. (For example, if the certificate_to_add already exists.)
        )r2   add_sha_certificater1   )r4   certificate_to_addr   r   r   rI      rD   zAndroidApp.add_sha_certificatec                 C   s   | j |S )a  Removes a SHA certificate from this Android app.

        Args:
            certificate_to_delete: The SHA certificate to delete.

        Returns:
            NoneType: None.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service. (For example, if the certificate_to_delete is not found.)
        )r2   delete_sha_certificate)r4   certificate_to_deleter   r   r   rK      s   z!AndroidApp.delete_sha_certificateN)__name__
__module____qualname____doc__r5   propertyr   r=   rC   rF   rH   rI   rK   r   r   r   r   r      s    

r   c                   @   s<   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dS )r   a  A reference to an iOS app within a Firebase project.

    Note: Unless otherwise specified, all methods defined in this class make an RPC.

    Please use the module-level function ``ios_app(app_id)`` to obtain instances of this class
    instead of instantiating it directly.
    c                 C   r/   r   r0   r3   r   r   r   r5      r6   zIOSApp.__init__c                 C   r7   )zReturns the app ID of the iOS app to which this instance refers.

        Note: This method does not make an RPC.

        Returns:
            string: The app ID of the iOS app to which this instance refers.
        r8   r9   r   r   r   r      r:   zIOSApp.app_idc                 C   r;   )a  Retrieves detailed information about this iOS app.

        Returns:
            IOSAppMetadata: An ``IOSAppMetadata`` instance.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service.
        )r2   get_ios_app_metadatar1   r9   r   r   r   r=     r>   zIOSApp.get_metadatac                 C   r?   )a`  Updates the display name attribute of this iOS app to the one given.

        Args:
            new_display_name: The new display name for this iOS app.

        Returns:
            NoneType: None.

        Raises:
            FirebaseError: If an error occurs while communicating with the Firebase Project
                Management Service.
        )r2   set_ios_app_display_namer1   rA   r   r   r   rC     rD   zIOSApp.set_display_namec                 C   r;   )zBRetrieves the configuration artifact associated with this iOS app.)r2   get_ios_app_configr1   r9   r   r   r   rF   #  rG   zIOSApp.get_configN)
rM   rN   rO   rP   r5   rQ   r   r=   rC   rF   r   r   r   r   r      s    

r   c                   @   sD   e Zd ZdZdd Zedd Zedd Zedd	 Zd
d Z	dS )_AppMetadataz9Detailed information about a Firebase Android or iOS app.c                 C   s4   t |d| _t |d| _t|d| _t |d| _d S )Nnamer   r   
project_id)r,   _namer1   r+   _display_name_project_id)r4   rV   r   r   rW   r   r   r   r5   +  s   z_AppMetadata.__init__c                 C   r7   )zThe globally unique, Firebase-assigned identifier of this Android or iOS app.

        This ID is unique even across apps of different platforms.
        r8   r9   r   r   r   r   3  s   z_AppMetadata.app_idc                 C   r7   )zThe user-assigned display name of this Android or iOS app.

        Note that the display name can be None if it has never been set by the user.)rY   r9   r   r   r   r   ;  s   z_AppMetadata.display_namec                 C   r7   )zPThe permanent, globally unique, user-assigned ID of the parent Firebase project.)rZ   r9   r   r   r   rW   B     z_AppMetadata.project_idc                 C   sB   t |t| s	dS | j|jko | j|jko | j|jko | j|jkS NF)r$   typerX   r   r   rW   r4   otherr   r   r   __eq__G  s   

z_AppMetadata.__eq__N)
rM   rN   rO   rP   r5   rQ   r   r   rW   r`   r   r   r   r   rU   (  s    


rU   c                       H   e Zd ZdZ fddZedd Z fddZdd	 Zd
d Z	  Z
S )AndroidAppMetadataz;Android-specific information about an Android Firebase app.c                    &   t t| |||| t|d| _dS )3Clients should not instantiate this class directly.r   N)superrb   r5   r,   _package_name)r4   r   rV   r   r   rW   	__class__r   r   r5   S     zAndroidAppMetadata.__init__c                 C   r7   )zTThe canonical package name of this Android app as it would appear in the Play Store.)rf   r9   r   r   r   r   X  r[   zAndroidAppMetadata.package_namec                       t t| |o| j|jkS r   )re   rb   r`   r   r^   rg   r   r   r`   ]  s   
zAndroidAppMetadata.__eq__c                 C      |  | S r   r`   r^   r   r   r   __ne__a     zAndroidAppMetadata.__ne__c                 C      t | j| j| j| j| jfS r   )hashrX   r   r   rW   r   r9   r   r   r   __hash__d  s   zAndroidAppMetadata.__hash__)rM   rN   rO   rP   r5   rQ   r   r`   rm   rq   __classcell__r   r   rg   r   rb   P  s    
rb   c                       ra   )IOSAppMetadataz3iOS-specific information about an iOS Firebase app.c                    rc   )rd   r"   N)re   rs   r5   r,   
_bundle_id)r4   r"   rV   r   r   rW   rg   r   r   r5   l  ri   zIOSAppMetadata.__init__c                 C   r7   )zOThe canonical bundle ID of this iOS app as it would appear in the iOS AppStore.)rt   r9   r   r   r   r"   q  r[   zIOSAppMetadata.bundle_idc                    rj   r   )re   rs   r`   r"   r^   rg   r   r   r`   v     zIOSAppMetadata.__eq__c                 C   rk   r   rl   r^   r   r   r   rm   y  rn   zIOSAppMetadata.__ne__c                 C   ro   r   )rp   rX   r   r   rW   r"   r9   r   r   r   rq   |  ru   zIOSAppMetadata.__hash__)rM   rN   rO   rP   r5   rQ   r"   r`   rm   rq   rr   r   r   rg   r   rs   i  s    
rs   c                   @   sr   e Zd ZdZdZdZedZedZ	dddZ
ed	d
 Zedd Zedd Zdd Zdd Zdd ZdS )SHACertificatezIRepresents a SHA-1 or SHA-256 certificate associated with an Android app.SHA_1SHA_256z^[0-9A-Fa-f]{40}$z^[0-9A-Fa-f]{64}$Nc                 C   s\   t |d t|d || _| | _tj|rtj| _	dS tj
|r*tj| _	dS td)a  Creates a new SHACertificate instance.

        Args:
            sha_hash: A string; the certificate hash for the Android app.
            name: The fully qualified resource name of this certificate; note that this field should
                be omitted if the instance is being constructed for the purpose of calling the
                add_sha_certificate() method on an ``AndroidApp``.

        Raises:
            ValueError: If the sha_hash is not a valid SHA-1 or SHA-256 certificate hash.
        sha_hashrV   zHThe supplied certificate hash is neither a valid SHA-1 nor SHA_256 hash.N)r,   r-   rX   lower	_sha_hashrv   	_SHA_1_REmatchrw   
_cert_type_SHA_256_RErx   r&   )r4   ry   rV   r   r   r   r5     s   


zSHACertificate.__init__c                 C   r7   )zReturns the fully qualified resource name of this certificate, if known.

        Returns:
            string: The fully qualified resource name of this certificate, if known; otherwise, the
            empty string.
        )rX   r9   r   r   r   rV     s   zSHACertificate.namec                 C   r7   )zbReturns the certificate hash.

        Returns:
            string: The certificate hash.
        )r{   r9   r   r   r   ry        zSHACertificate.sha_hashc                 C   r7   )zReturns the type of the SHA certificate encoded in the hash.

        Returns:
            string: One of 'SHA_1' or 'SHA_256'.
        )r~   r9   r   r   r   	cert_type  r   zSHACertificate.cert_typec                 C   s2   t |tsdS | j|jko| j|jko| j|jkS r\   )r$   rv   rV   ry   r   r^   r   r   r   r`     s
   

zSHACertificate.__eq__c                 C   rk   r   rl   r^   r   r   r   rm     rn   zSHACertificate.__ne__c                 C   s   t | j| j| jfS r   )rp   rV   ry   r   r9   r   r   r   rq     s   zSHACertificate.__hash__r   )rM   rN   rO   rP   rw   rx   recompiler|   r   r5   rQ   rV   ry   r   r`   rm   rq   r   r   r   r   rv     s     



	

rv   c                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd8d d!Zd8d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d8d4d5Z!d8d6d7Z"dS )9r	   zNProvides methods for interacting with the Firebase Project Management Service.zhttps://firebase.googleapis.comd      g      ?g      ?androidAppspackageNameiosAppsbundleIdc                 C   sX   |j }|s	td|| _dtj}|jdtj	}tj
|j tjd|i|d| _d S )NzProject ID is required to access the Firebase Project Management Service. Either set the projectId option, or use service account credentials. Alternatively, set the GOOGLE_CLOUD_PROJECT environment variable.zPython/Admin/{0}httpTimeoutzX-Client-Version)
credentialbase_urlheaderstimeout)rW   r&   rZ   r'   firebase_admin__version__optionsgetr   DEFAULT_TIMEOUT_SECONDSJsonHttpClientr   get_credentialr	   BASE_URL_client)r4   r   rW   version_headerr   r   r   r   r5     s   z"_ProjectManagementService.__init__c                 C      | j tjtjt|dS N)platform_resource_nameidentifier_namemetadata_classr   )_get_app_metadatar	   ANDROID_APPS_RESOURCE_NAMEANDROID_APP_IDENTIFIER_NAMErb   r4   r   r   r   r   r<        z2_ProjectManagementService.get_android_app_metadatac                 C   r   r   )r   r	   IOS_APPS_RESOURCE_NAMEIOS_APP_IDENTIFIER_NAMErs   r   r   r   r   rR     r   z._ProjectManagementService.get_ios_app_metadatac                 C   sN   t |d d||}| d|}||| |d |d |dp!d|d d	S )
z;Retrieves detailed information about an Android or iOS app.r   z/v1beta1/projects/-/{0}/{1}r   rV   appIddisplayNameN	projectId)rV   r   r   rW   )r,   r'   _make_requestr   )r4   r   r   r   r   pathresponser   r   r   r     s   
z+_ProjectManagementService._get_app_metadatac                 C      | j ||tjd d S N)r   rB   r   )_set_display_namer	   r   r4   r   rB   r   r   r   r@     
   
z6_ProjectManagementService.set_android_app_display_namec                 C   r   r   )r   r	   r   r   r   r   r   rS     r   z2_ProjectManagementService.set_ios_app_display_namec                 C   s(   d ||}d|i}| jd||d dS )z/Sets the display name of an Android or iOS app.z2/v1beta1/projects/-/{0}/{1}?updateMask=displayNamer   patchjsonN)r'   r   )r4   r   rB   r   r   request_bodyr   r   r   r     s
   z+_ProjectManagementService._set_display_namec                 C      | j tjtdS N)r   	app_class)
_list_appsr	   r   r   r9   r   r   r   r        z+_ProjectManagementService.list_android_appsc                 C   r   r   )r   r	   r   r   r9   r   r   r   r     r   z'_ProjectManagementService.list_ios_appsc                    s   d j|tj}d|}g }	 |d}|s	 |S | fdd|D  |d}|s3	 |S d j||tj}d|}q)	z>Lists all the Android or iOS apps within the Firebase project.z&/v1beta1/projects/{0}/{1}?pageSize={2}r   Tappsc                 3   s     | ]} |d  dV  qdS )r   r   Nr   ).0r   r   r4   r   r   	<genexpr>+  s    z7_ProjectManagementService._list_apps.<locals>.<genexpr>nextPageTokenz4/v1beta1/projects/{0}/{1}?pageToken={2}&pageSize={3})r'   rZ   r	   MAXIMUM_LIST_APPS_PAGE_SIZEr   r   extend)r4   r   r   r   r   	apps_listr   next_page_tokenr   r   r   r     s2   

z$_ProjectManagementService._list_appsNc                 C      | j tjtj||tdS N)r   r   
identifierr   r   )_create_appr	   r   r   r   )r4   r   r   r   r   r   r   8     z,_ProjectManagementService.create_android_appc                 C   r   r   )r   r	   r   r   r   )r4   r"   r   r   r   r   r!   @  r   z(_ProjectManagementService.create_ios_appc                 C   s^   t |d d| j|}||i}|r||d< | jd||d}|d }	| |	}
||
d | dS )	zCreates an Android or iOS app.r   z/v1beta1/projects/{0}/{1}r   postr   rV   r   r   )r+   r'   rZ   r   _poll_app_creation)r4   r   r   r   r   r   r   r   r   operation_namepoll_responser   r   r   r   H  s   

z%_ProjectManagementService._create_appc           
      C   s   t tjD ]8}ttj|}|tj }t| d|}| 	d|\}}|
d}|r=|
d}	|	r6|	  S tjd|dqtd)zVPolls the Long-Running Operation repeatedly until it is done with exponential backoff.z/v1/{0}r   doner   z;Polling finished, but the operation terminated in an error.)http_responsezPolling deadline exceeded.)ranger	   MAXIMUM_POLLING_ATTEMPTSpowPOLL_EXPONENTIAL_BACKOFF_FACTORPOLL_BASE_WAIT_TIME_SECONDStimesleepr'   _body_and_responser   r   UnknownErrorDeadlineExceededError)
r4   r   current_attemptdelay_factorwait_time_secondsr   r   r   r   r   r   r   r   r   Z  s&   





z,_ProjectManagementService._poll_app_creationc                 C      | j tj|dS N)r   r   )_get_app_configr	   r   r   r   r   r   rE   n  r   z0_ProjectManagementService.get_android_app_configc                 C   r   r   )r   r	   r   r   r   r   r   rT   s  s   z,_ProjectManagementService.get_ios_app_configc                 C   s.   d ||}| d|}t|d jddS )Nz"/v1beta1/projects/-/{0}/{1}/configr   configFileContentszutf-8)encoding)r'   r   base64standard_b64decodedecode)r4   r   r   r   r   r   r   r   r   w  s   z)_ProjectManagementService._get_app_configc                 C   s2   d |}| d|}|dpg }dd |D S )N'/v1beta1/projects/-/androidApps/{0}/shar   certificatesc                 S   s    g | ]}t |d  |d dqS )shaHashrV   )ry   rV   )rv   )r   certr   r   r   
<listcomp>  s     zB_ProjectManagementService.get_sha_certificates.<locals>.<listcomp>)r'   r   r   )r4   r   r   r   	cert_listr   r   r   rH   ~  s   
z._ProjectManagementService.get_sha_certificatesc                 C   s:   d |}t|dj}|j}||d}| jd||d d S )Nr   rJ   )r   certTyper   r   )r'   r.   ry   r   r   )r4   r   rJ   r   ry   r   r   r   r   r   rI     s
   

z-_ProjectManagementService.add_sha_certificatec                 C   s&   t |dj}d|}| d| d S )NrL   z/v1beta1/{0}delete)r.   rV   r'   r   )r4   rL   rV   r   r   r   r   rK     s   
z0_ProjectManagementService.delete_sha_certificatec                 C   s   |  |||\}}|S r   )r   )r4   methodurlr   body_r   r   r   r     s   z'_ProjectManagementService._make_requestc              
   C   s<   z
| j j|||dW S  tjjy } zt|d }~ww )N)r   r   r   )r   body_and_responserequestsr   RequestExceptionr   #handle_platform_error_from_requests)r4   r   r   r   errorr   r   r   r     s   
z,_ProjectManagementService._body_and_responser   )#rM   rN   rO   rP   r   r   r   r   r   r   r   r   r   r5   r<   rR   r   r@   rS   r   r   r   r   r   r!   r   r   rE   rT   r   rH   rI   rK   r   r   r   r   r   r   r	     sB    


r	   r   )NN)rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r+   r,   r-   r.   r   r   rU   rb   rs   rv   r	   r   r   r   r   <module>   s8   





b8(J