U
    
ڲga                     @   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d0d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 )1zFirebase Project Management module.

This module enables management of resources in Firebase projects, such as Android and iOS apps.
    N)
exceptions)_http_client)_utilsZ_project_managementc                 C   s   t | ttS N)r   Zget_app_service_PROJECT_MANAGEMENT_ATTRIBUTE_ProjectManagementServiceapp r
   E/tmp/pip-unpacked-wheel-p0r7i5ii/firebase_admin/project_management.py_get_project_management_service#   s    r   c                 C   s   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'   s    
r   c                 C   s   t | t|dS )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   s    
r   c                 C   s   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   s   t |  S )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   s   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   s    r   c                 C   s   t || |S )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   s    r   c                 C   s(   | d kst | tr| S td|d S )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|d S )Nz{0} must be a non-empty string.r   r!   r
   r
   r   _check_is_nonempty_string|   s    r%   c                 C   s   | d krd S t | |S r   )r%   r!   r
   r
   r   !_check_is_nonempty_string_or_none   s    r&   c                 C   s   | d k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   s   || _ || _d S r   _app_id_serviceselfr   r   r
   r
   r   __init__   s    zAndroidApp.__init__c                 C   s   | 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.
        r)   r,   r
   r
   r   r      s    	zAndroidApp.app_idc                 C   s   | 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.
        )r*   get_android_app_metadatar)   r/   r
   r
   r   get_metadata   s    
zAndroidApp.get_metadatac                 C   s   | 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.
        )r*   set_android_app_display_namer)   r,   new_display_namer
   r
   r   set_display_name   s    zAndroidApp.set_display_namec                 C   s   | j | jS )zFRetrieves the configuration artifact associated with this Android app.)r*   get_android_app_configr)   r/   r
   r
   r   
get_config   s    zAndroidApp.get_configc                 C   s   | j | jS )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.
        )r*   get_sha_certificatesr)   r/   r
   r
   r   r8      s    
zAndroidApp.get_sha_certificatesc                 C   s   | j | j|S )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.)
        )r*   add_sha_certificater)   )r,   certificate_to_addr
   r
   r   r9      s    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.)
        )r*   delete_sha_certificate)r,   certificate_to_deleter
   r
   r   r;      s    z!AndroidApp.delete_sha_certificateN)__name__
__module____qualname____doc__r-   propertyr   r1   r5   r7   r8   r9   r;   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   s   || _ || _d S r   r(   r+   r
   r
   r   r-      s    zIOSApp.__init__c                 C   s   | j S )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.
        r.   r/   r
   r
   r   r      s    	zIOSApp.app_idc                 C   s   | j | jS )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.
        )r*   get_ios_app_metadatar)   r/   r
   r
   r   r1     s    
zIOSApp.get_metadatac                 C   s   | j | j|S )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.
        )r*   set_ios_app_display_namer)   r3   r
   r
   r   r5     s    zIOSApp.set_display_namec                 C   s   | j | jS )zBRetrieves the configuration artifact associated with this iOS app.)r*   get_ios_app_configr)   r/   r
   r
   r   r7   #  s    zIOSApp.get_configN)
r=   r>   r?   r@   r-   rA   r   r1   r5   r7   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%   _namer)   r$   _display_name_project_id)r,   rF   r   r   rG   r
   r
   r   r-   +  s    z_AppMetadata.__init__c                 C   s   | j S )zThe globally unique, Firebase-assigned identifier of this Android or iOS app.

        This ID is unique even across apps of different platforms.
        r.   r/   r
   r
   r   r   3  s    z_AppMetadata.app_idc                 C   s   | j S )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.)rI   r/   r
   r
   r   r   ;  s    z_AppMetadata.display_namec                 C   s   | j S )zPThe permanent, globally unique, user-assigned ID of the parent Firebase project.)rJ   r/   r
   r
   r   rG   B  s    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   typerH   r   r   rG   r,   otherr
   r
   r   __eq__G  s    

z_AppMetadata.__eq__N)
r=   r>   r?   r@   r-   rA   r   r   rG   rO   r
   r
   r
   r   rE   (  s   


rE   c                       sH   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                    s&   t t| |||| t|d| _dS )3Clients should not instantiate this class directly.r   N)superrP   r-   r%   _package_name)r,   r   rF   r   r   rG   	__class__r
   r   r-   S  s    zAndroidAppMetadata.__init__c                 C   s   | j S )zTThe canonical package name of this Android app as it would appear in the Play Store.)rS   r/   r
   r
   r   r   X  s    zAndroidAppMetadata.package_namec                    s   t t| |o| j|jkS r   )rR   rP   rO   r   rM   rT   r
   r   rO   ]  s    
zAndroidAppMetadata.__eq__c                 C   s   |  | S r   rO   rM   r
   r
   r   __ne__a  s    zAndroidAppMetadata.__ne__c                 C   s   t | j| j| j| j| jfS r   )hashrH   r   r   rG   r   r/   r
   r
   r   __hash__d  s    zAndroidAppMetadata.__hash__)r=   r>   r?   r@   r-   rA   r   rO   rW   rY   __classcell__r
   r
   rT   r   rP   P  s   
rP   c                       sH   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 )IOSAppMetadataz3iOS-specific information about an iOS Firebase app.c                    s&   t t| |||| t|d| _dS )rQ   r   N)rR   r[   r-   r%   
_bundle_id)r,   r   rF   r   r   rG   rT   r
   r   r-   l  s    zIOSAppMetadata.__init__c                 C   s   | j S )zOThe canonical bundle ID of this iOS app as it would appear in the iOS AppStore.)r\   r/   r
   r
   r   r   q  s    zIOSAppMetadata.bundle_idc                    s   t t| |o| j|jkS r   )rR   r[   rO   r   rM   rT   r
   r   rO   v  s    zIOSAppMetadata.__eq__c                 C   s   |  | S r   rV   rM   r
   r
   r   rW   y  s    zIOSAppMetadata.__ne__c                 C   s   t | j| j| j| j| jfS r   )rX   rH   r   r   rG   r   r/   r
   r
   r   rY   |  s    zIOSAppMetadata.__hash__)r=   r>   r?   r@   r-   rA   r   rO   rW   rY   rZ   r
   r
   rT   r   r[   i  s   
r[   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| _	ntj
|rPtj| _	ntddS )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_hashrF   zHThe supplied certificate hash is neither a valid SHA-1 nor SHA_256 hash.N)r%   r&   rH   lower	_sha_hashr]   	_SHA_1_REmatchr^   
_cert_type_SHA_256_REr_   r   )r,   r`   rF   r
   r
   r   r-     s    




zSHACertificate.__init__c                 C   s   | j S )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.
        )rH   r/   r
   r
   r   rF     s    zSHACertificate.namec                 C   s   | j S )zbReturns the certificate hash.

        Returns:
            string: The certificate hash.
        )rb   r/   r
   r
   r   r`     s    zSHACertificate.sha_hashc                 C   s   | j S )zReturns the type of the SHA certificate encoded in the hash.

        Returns:
            string: One of 'SHA_1' or 'SHA_256'.
        )re   r/   r
   r
   r   	cert_type  s    zSHACertificate.cert_typec                 C   s2   t |tsdS | j|jko0| j|jko0| j|jkS rK   )r   r]   rF   r`   rg   rM   r
   r
   r   rO     s
    

zSHACertificate.__eq__c                 C   s   |  | S r   rV   rM   r
   r
   r   rW     s    zSHACertificate.__ne__c                 C   s   t | j| j| jfS r   )rX   rF   r`   rg   r/   r
   r
   r   rY     s    zSHACertificate.__hash__)N)r=   r>   r?   r@   r^   r_   recompilerc   rf   r-   rA   rF   r`   rg   rO   rW   rY   r
   r
   r
   r   r]     s   



	

r]   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d9d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d:d4d5Z!d;d6d7Z"dS )<r   zNProvides methods for interacting with the Firebase Project Management Service.zhttps://firebase.googleapis.comd      g      ?g      ?ZandroidAppspackageNameZiosAppsZ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}ZhttpTimeoutzX-Client-Version)
credentialbase_urlheaderstimeout)rG   r   rJ   r    firebase_admin__version__optionsgetr   ZDEFAULT_TIMEOUT_SECONDSZJsonHttpClientrm   get_credentialr   BASE_URL_client)r,   r	   rG   Zversion_headerrp   r
   r
   r   r-     s    z"_ProjectManagementService.__init__c                 C   s   | 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_NAMErP   r,   r   r
   r
   r   r0     s    z2_ProjectManagementService.get_android_app_metadatac                 C   s   | j tjtjt|dS rx   )r|   r   IOS_APPS_RESOURCE_NAMEIOS_APP_IDENTIFIER_NAMEr[   r   r
   r
   r   rB     s    z._ProjectManagementService.get_ios_app_metadatac                 C   sN   t |d d||}| d|}||| |d |d |dpBd|d d	S )
z;Retrieves detailed information about an Android or iOS app.r   z/v1beta1/projects/-/{0}/{1}rt   rF   appIddisplayNameNZ	projectId)rF   r   r   rG   )r%   r    _make_requestrt   )r,   ry   rz   r{   r   pathresponser
   r
   r   r|     s    
z+_ProjectManagementService._get_app_metadatac                 C   s   | j ||tjd d S N)r   r4   ry   )_set_display_namer   r}   r,   r   r4   r
   r
   r   r2     s
    z6_ProjectManagementService.set_android_app_display_namec                 C   s   | j ||tjd d S r   )r   r   r   r   r
   r
   r   rC     s
    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   )r,   r   r4   ry   r   request_bodyr
   r
   r   r     s     z+_ProjectManagementService._set_display_namec                 C   s   | j tjtdS N)ry   	app_class)
_list_appsr   r}   r   r/   r
   r
   r   r     s    z+_ProjectManagementService.list_android_appsc                 C   s   | j tjtdS r   )r   r   r   r   r/   r
   r
   r   r     s    z'_ProjectManagementService.list_ios_appsc                    s   d j|tj}d|}g }|d}|s2q~| fdd|D  |d}|s\q~d j||tj}d|}q"|S )z>Lists all the Android or iOS apps within the Firebase project.z&/v1beta1/projects/{0}/{1}?pageSize={2}rt   appsc                 3   s   | ]} |d  dV  qdS )r   r   Nr
   ).0r	   r   r,   r
   r   	<genexpr>+  s     z7_ProjectManagementService._list_apps.<locals>.<genexpr>ZnextPageTokenz4/v1beta1/projects/{0}/{1}?pageToken={2}&pageSize={3})r    rJ   r   MAXIMUM_LIST_APPS_PAGE_SIZEr   rt   extend)r,   ry   r   r   r   Z	apps_listr   Znext_page_tokenr
   r   r   r     s,    

z$_ProjectManagementService._list_appsNc                 C   s   | j tjtj||tdS N)ry   rz   
identifierr   r   )_create_appr   r}   r~   r   )r,   r   r   r
   r
   r   r   8  s    z,_ProjectManagementService.create_android_appc                 C   s   | j tjtj||tdS r   )r   r   r   r   r   )r,   r   r   r
   r
   r   r   @  s    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   rF   r   r   )r$   r    rJ   r   _poll_app_creation)r,   ry   rz   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 ]p}ttj|}|tj }t| d|}| 	d|\}}|
d}|r
|
d}	|	rl|	  S tjd|dq
tddS )	zVPolls the Long-Running Operation repeatedly until it is done with exponential backoff.z/v1/{0}rt   doner   z;Polling finished, but the operation terminated in an error.)http_responsezPolling deadline exceeded.N)ranger   MAXIMUM_POLLING_ATTEMPTSpowPOLL_EXPONENTIAL_BACKOFF_FACTORPOLL_BASE_WAIT_TIME_SECONDStimesleepr    _body_and_responsert   r   ZUnknownErrorZDeadlineExceededError)
r,   r   Zcurrent_attemptZdelay_factorZwait_time_secondsr   r   r   r   r   r
   r
   r   r   Z  s&     




z,_ProjectManagementService._poll_app_creationc                 C   s   | j tj|dS N)ry   r   )_get_app_configr   r}   r   r
   r
   r   r6   n  s    z0_ProjectManagementService.get_android_app_configc                 C   s   | j tj|dS r   )r   r   r   r   r
   r
   r   rD   s  s     z,_ProjectManagementService.get_ios_app_configc                 C   s.   d ||}| d|}t|d jddS )Nz"/v1beta1/projects/-/{0}/{1}/configrt   ZconfigFileContentszutf-8)encoding)r    r   base64standard_b64decodedecode)r,   ry   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}/shart   Zcertificatesc                 S   s    g | ]}t |d  |d dqS )shaHashrF   )r`   rF   )r]   )r   certr
   r
   r   
<listcomp>  s     zB_ProjectManagementService.get_sha_certificates.<locals>.<listcomp>)r    r   rt   )r,   r   r   r   Z	cert_listr
   r
   r   r8   ~  s    
z._ProjectManagementService.get_sha_certificatesc                 C   s:   d |}t|dj}|j}||d}| jd||d d S )Nr   r:   )r   ZcertTyper   r   )r    r'   r`   rg   r   )r,   r   r:   r   r`   rg   r   r
   r
   r   r9     s
    

z-_ProjectManagementService.add_sha_certificatec                 C   s&   t |dj}d|}| d| d S )Nr<   z/v1beta1/{0}delete)r'   rF   r    r   )r,   r<   rF   r   r
   r
   r   r;     s    
z0_ProjectManagementService.delete_sha_certificatec                 C   s   |  |||\}}|S r   )r   )r,   methodurlr   body_r
   r
   r   r     s    z'_ProjectManagementService._make_requestc              
   C   sJ   z| j j|||dW S  tjjk
rD } zt|W 5 d }~X Y nX d S )N)r   r   r   )rw   Zbody_and_responserequestsr   RequestExceptionr   Z#handle_platform_error_from_requests)r,   r   r   r   errorr
   r
   r   r     s    z,_ProjectManagementService._body_and_response)N)N)N)N)#r=   r>   r?   r@   rv   r   r   r   r   r}   r~   r   r   r-   r0   rB   r|   r2   rC   r   r   r   r   r   r   r   r   r6   rD   r   r8   r9   r;   r   r   r
   r
   r
   r   r     s@   


r   )N)N)N)N)NN)NN)r@   r   rh   r   r   rq   r   r   r   r   r   r   r   r   r   r   r   r$   r%   r&   r'   r   r   rE   rP   r[   r]   r   r
   r
   r
   r   <module>   s6   





b8(J