o
     i`                     @   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   /var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/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 must be a string.
isinstancestr
ValueErrorobj
field_namer   r   r   _check_is_string_or_nonev   s   r*   c                 C   s    t | tr	| r	| S t| d)Nz 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 cannot be None.)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.
        r0   r3   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.
        )r1   get_android_app_metadatar0   r8   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.
        )r1   set_android_app_display_namer0   r3   new_display_namer   r   r   set_display_name      zAndroidApp.set_display_namec                 C   r:   )zFRetrieves the configuration artifact associated with this Android app.)r1   get_android_app_configr0   r8   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.
        )r1   get_sha_certificatesr0   r8   r   r   r   rG      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.)
        )r1   add_sha_certificater0   )r3   certificate_to_addr   r   r   rH      rC   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.)
        )r1   delete_sha_certificate)r3   certificate_to_deleter   r   r   rJ      s   z!AndroidApp.delete_sha_certificateN)__name__
__module____qualname____doc__r4   propertyr   r<   rB   rE   rG   rH   rJ   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   r/   r2   r   r   r   r4      r5   zIOSApp.__init__c                 C   r6   )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.
        r7   r8   r   r   r   r      r9   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.
        )r1   get_ios_app_metadatar0   r8   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.
        )r1   set_ios_app_display_namer0   r@   r   r   r   rB     rC   zIOSApp.set_display_namec                 C   r:   )zBRetrieves the configuration artifact associated with this iOS app.)r1   get_ios_app_configr0   r8   r   r   r   rE   #  rF   zIOSApp.get_configN)
rL   rM   rN   rO   r4   rP   r   r<   rB   rE   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+   _namer0   r*   _display_name_project_id)r3   rU   r   r   rV   r   r   r   r4   +  s   z_AppMetadata.__init__c                 C   r6   )zThe globally unique, Firebase-assigned identifier of this Android or iOS app.

        This ID is unique even across apps of different platforms.
        r7   r8   r   r   r   r   3  s   z_AppMetadata.app_idc                 C   r6   )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.)rX   r8   r   r   r   r   ;  s   z_AppMetadata.display_namec                 C   r6   )zPThe permanent, globally unique, user-assigned ID of the parent Firebase project.)rY   r8   r   r   r   rV   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$   typerW   r   r   rV   r3   otherr   r   r   __eq__G  s   

z_AppMetadata.__eq__N)
rL   rM   rN   rO   r4   rP   r   r   rV   r_   r   r   r   r   rT   (  s    


rT   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|d| _dS )3Clients should not instantiate this class directly.r   N)superr4   r+   _package_name)r3   r   rU   r   r   rV   	__class__r   r   r4   S     zAndroidAppMetadata.__init__c                 C   r6   )zTThe canonical package name of this Android app as it would appear in the Play Store.)re   r8   r   r   r   r   X  rZ   zAndroidAppMetadata.package_namec                       t  |o| j|jkS r   )rd   r_   r   r]   rf   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   )hashrW   r   r   rV   r   r8   r   r   r   __hash__d  s   zAndroidAppMetadata.__hash__)rL   rM   rN   rO   r4   rP   r   r_   rl   rp   __classcell__r   r   rf   r   ra   P  s    
ra   c                       r`   )IOSAppMetadataz3iOS-specific information about an iOS Firebase app.c                    rb   )rc   r"   N)rd   r4   r+   
_bundle_id)r3   r"   rU   r   r   rV   rf   r   r   r4   l  rh   zIOSAppMetadata.__init__c                 C   r6   )zOThe canonical bundle ID of this iOS app as it would appear in the iOS AppStore.)rs   r8   r   r   r   r"   q  rZ   zIOSAppMetadata.bundle_idc                    ri   r   )rd   r_   r"   r]   rf   r   r   r_   v  s   zIOSAppMetadata.__eq__c                 C   rj   r   rk   r]   r   r   r   rl   y  rm   zIOSAppMetadata.__ne__c                 C   rn   r   )ro   rW   r   r   rV   r"   r8   r   r   r   rp   |  s   zIOSAppMetadata.__hash__)rL   rM   rN   rO   r4   rP   r"   r_   rl   rp   rq   r   r   rf   r   rr   i  s    
rr   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_hashrU   zHThe supplied certificate hash is neither a valid SHA-1 nor SHA_256 hash.N)r+   r,   rW   lower	_sha_hashrt   	_SHA_1_REmatchru   
_cert_type_SHA_256_RErv   r&   )r3   rw   rU   r   r   r   r4     s   


zSHACertificate.__init__c                 C   r6   )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.
        )rW   r8   r   r   r   rU     s   zSHACertificate.namec                 C   r6   )zbReturns the certificate hash.

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

        Returns:
            string: One of 'SHA_1' or 'SHA_256'.
        )r|   r8   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$   rt   rU   rw   r   r]   r   r   r   r_     s
   

zSHACertificate.__eq__c                 C   rj   r   rk   r]   r   r   r   rl     rm   zSHACertificate.__ne__c                 C   s   t | j| j| jfS r   )ro   rU   rw   r   r8   r   r   r   rp     s   zSHACertificate.__hash__r   )rL   rM   rN   rO   ru   rv   recompilerz   r}   r4   rP   rU   rw   r   r_   rl   rp   r   r   r   r   rt     s     



	

rt   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/httpTimeoutzX-Client-Version)
credentialbase_urlheaderstimeout)rV   r&   rY   firebase_admin__version__optionsgetr   DEFAULT_TIMEOUT_SECONDSJsonHttpClientr   get_credentialr	   BASE_URL_client)r3   r   rV   version_headerr   r   r   r   r4     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_NAMEra   r3   r   r   r   r   r;        z2_ProjectManagementService.get_android_app_metadatac                 C   r   r   )r   r	   IOS_APPS_RESOURCE_NAMEIOS_APP_IDENTIFIER_NAMErr   r   r   r   r   rQ     r   z._ProjectManagementService.get_ios_app_metadatac                 C   sR   t |d d| d| }| d|}||| |d |d |dp#d|d	 d
S )z;Retrieves detailed information about an Android or iOS app.r   /v1beta1/projects/-//r   rU   appIddisplayNameN	projectId)rU   r   r   rV   )r+   _make_requestr   )r3   r   r   r   r   pathresponser   r   r   r     s   
z+_ProjectManagementService._get_app_metadatac                 C      | j ||tjd d S N)r   rA   r   )_set_display_namer	   r   r3   r   rA   r   r   r   r?     
   
z6_ProjectManagementService.set_android_app_display_namec                 C   r   r   )r   r	   r   r   r   r   r   rR     r   z2_ProjectManagementService.set_ios_app_display_namec                 C   s.   d| d| d}d|i}| j d||d dS )z/Sets the display name of an Android or iOS app.r   r   z?updateMask=displayNamer   patchjsonN)r   )r3   r   rA   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   r8   r   r   r   r        z+_ProjectManagementService.list_android_appsc                 C   r   r   )r   r	   r   r   r8   r   r   r   r     r   z'_ProjectManagementService.list_ios_appsc                    s   dj  d| dtj }d|}g }	 |d}|s 	 |S | fdd|D  |d	}|s7	 |S dj  d| d
| dtj }d|}q)z>Lists all the Android or iOS apps within the Firebase project./v1beta1/projects/r   z
?pageSize=r   Tappsc                 3   s     | ]} |d  dV  qdS )r   r   Nr   ).0r   r   r3   r   r   	<genexpr>*  s    z7_ProjectManagementService._list_apps.<locals>.<genexpr>nextPageTokenz?pageToken=z
&pageSize=)rY   r	   MAXIMUM_LIST_APPS_PAGE_SIZEr   r   extend)r3   r   r   r   r   	apps_listr   next_page_tokenr   r   r   r     s0   

z$_ProjectManagementService._list_appsNc                 C      | j tjtj||tdS N)r   r   
identifierr   r   )_create_appr	   r   r   r   )r3   r   r   r   r   r   r   7     z,_ProjectManagementService.create_android_appc                 C   r   r   )r   r	   r   r   r   )r3   r"   r   r   r   r   r!   ?  r   z(_ProjectManagementService.create_ios_appc                 C   sb   t |d d| j d| }||i}|r||d< | jd||d}|d }	| |	}
||
d | d	S )
zCreates an Android or iOS app.r   r   r   r   postr   rU   r   r   )r*   rY   r   _poll_app_creation)r3   r   r   r   r   r   r   r   r   operation_namepoll_responser   r   r   r   G  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/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sleep_body_and_responser   r   UnknownErrorDeadlineExceededError)
r3   r   current_attemptdelay_factorwait_time_secondsr   r   r   r   r   r   r   r   r   Y  s&   





z,_ProjectManagementService._poll_app_creationc                 C      | j tj|dS N)r   r   )_get_app_configr	   r   r   r   r   r   rD   m  r   z0_ProjectManagementService.get_android_app_configc                 C   r   r   )r   r	   r   r   r   r   r   rS   r  s   z,_ProjectManagementService.get_ios_app_configc                 C   s4   d| d| d}|  d|}t|d jddS )Nr   r   z/configr   configFileContentszutf-8)encoding)r   base64standard_b64decodedecode)r3   r   r   r   r   r   r   r   r   v  s   z)_ProjectManagementService._get_app_configc                 C   s4   d| d}|  d|}|dpg }dd |D S )N /v1beta1/projects/-/androidApps//shar   certificatesc                 S   s    g | ]}t |d  |d dqS )shaHashrU   )rw   rU   )rt   )r   certr   r   r   
<listcomp>  s     zB_ProjectManagementService.get_sha_certificates.<locals>.<listcomp>)r   r   )r3   r   r   r   	cert_listr   r   r   rG   }  s   z._ProjectManagementService.get_sha_certificatesc                 C   s<   d| d}t |dj}|j}||d}| jd||d d S )Nr   r   rI   )r   certTyper   r   )r-   rw   r   r   )r3   r   rI   r   rw   r   r   r   r   r   rH     s
   
z-_ProjectManagementService.add_sha_certificatec                 C   s&   t |dj}d| }| d| d S )NrK   z	/v1beta1/delete)r-   rU   r   )r3   rK   rU   r   r   r   r   rJ     s   
z0_ProjectManagementService.delete_sha_certificatec                 C   s   |  |||\}}|S r   )r   )r3   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)r3   r   r   r   errorr   r   r   r     s   
z,_ProjectManagementService._body_and_responser   )#rL   rM   rN   rO   r   r   r   r   r   r   r   r   r   r4   r;   rQ   r   r?   rR   r   r   r   r   r   r!   r   r   rD   rS   r   rG   rH   rJ   r   r   r   r   r   r   r	     sB    


r	   r   )NN)rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r*   r+   r,   r-   r   r   rT   ra   rr   rt   r	   r   r   r   r   <module>   s8   





b8(J