o
    h	                     @   sv   d dl Z d dlmZmZ d dlmZ ddlmZ e dZ	g dZ
G dd	 d	eZG d
d deZG dd deZdS )    Nurlsplit
urlunsplit)ImproperlyConfigured   )BaseBackendzwagtail.frontendcache)AzureBaseBackendAzureFrontDoorBackendAzureCdnBackendc                       st   e Zd Z f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dd Zdd Z  ZS )r   c                    sb   t  | |dd | _|dd | _z|d| _W n ty'   tdw |dd | _d S )NCREDENTIALSSUBSCRIPTION_IDRESOURCE_GROUP_NAMEzRThe setting 'WAGTAILFRONTENDCACHE' requires 'RESOURCE_GROUP_NAME' to be specified.CUSTOM_HEADERS)	super__init__pop_credentials_subscription_id_resource_group_nameKeyErrorr   _custom_headersselfparams	__class__ m/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/contrib/frontend_cache/backends/azure.pyr      s   zAzureBaseBackend.__init__c                    s      fdd|D  d S )Nc                    s   g | ]}  |qS r   )	_get_path).0urlr   r   r   
<listcomp>   s    z0AzureBaseBackend.purge_batch.<locals>.<listcomp>)_purge_content)r   urlsr   r!   r   purge_batch   s   zAzureBaseBackend.purge_batchc                 C   s   |  |g d S )N)r%   )r   r    r   r   r   purge   s   zAzureBaseBackend.purgec                 C   s*   z
ddl m} W | S  ty   Y d S w )Nr   )DefaultAzureCredential)azure.identityr'   ImportError)r   r'   r   r   r   _get_default_credentials!   s   z)AzureBaseBackend._get_default_credentialsc                 C      | j }|r|S |  S )z|
        Use credentials object set by user. If not set, use the one configured
        in the current environment.
        )r   r*   )r   user_credentialsr   r   r   _get_credentials(      z!AzureBaseBackend._get_credentialsc                 C   sJ   zddl m} W n
 ty   Y dS w |  }||}t|j }|jS )z=
        Obtain subscription ID directly from Azure.
        r   )SubscriptionClient )azure.mgmt.resourcer/   r)   r-   nextsubscriptionslistsubscription_id)r   r/   
credentialsubscription_clientsubscriptionr   r   r   _get_default_subscription_id2   s   z-AzureBaseBackend._get_default_subscription_idc                 C   r+   )z
        Use subscription ID set in the user configuration. If not set, try to
        retrieve one from Azure directly.
        )r   r9   )r   user_subscription_idr   r   r   _get_subscription_id?   r.   z%AzureBaseBackend._get_subscription_idc                 C   s   |   |  dS )N)r6   r5   )r-   r;   r!   r   r   r   _get_client_kwargsI   s   z#AzureBaseBackend._get_client_kwargsc                 C   s   dt |dd  }t|S )zA
        Split netloc from the URL and return path only.
        )r0   r0      Nr   )r   r    	url_partsr   r   r   r   O   s   zAzureBaseBackend._get_pathc                 C   s   |   }|  }|di |S )z,
        Get Azure client instance.
        Nr   )_get_client_classr<   )r   klasskwargsr   r   r   _get_clientX   s   zAzureBaseBackend._get_clientc                 C   s   | j | jt|dS )zL
        Get keyword arguments passes to Azure purge content calls.
        )resource_group_namecustom_headerscontent_paths)r   r   set)r   pathsr   r   r   _get_purge_kwargs`   s   z"AzureBaseBackend._get_purge_kwargsc              
   C   sp   ddl m} |  }z	| || W d S  |y7 } z|D ]}td|t| j|j qW Y d }~d S d }~ww )Nr   )HttpOperationErrorz9Couldn't purge '%s' from %s cache. HttpOperationError: %r)	msrest.exceptionsrI   rB   _make_purge_calllogger	exceptiontype__name__response)r   rG   rI   clientrM   pathr   r   r   r#   j   s   zAzureBaseBackend._purge_content)rO   
__module____qualname__r   r%   r&   r*   r-   r9   r;   r<   r   rB   rH   r#   __classcell__r   r   r   r   r      s    

	
r   c                       8   e Zd Z fddZdd Z fddZdd Z  ZS )	r	   c                    sF   t  | z|d| _W n ty   tdw |dd | _d S )NFRONT_DOOR_NAMEzNThe setting 'WAGTAILFRONTENDCACHE' requires 'FRONT_DOOR_NAME' to be specified.FRONT_DOOR_SERVICE_URL)r   r   r   _front_door_namer   r   _front_door_service_urlr   r   r   r   r   {   s   zAzureFrontDoorBackend.__init__c                 C      ddl m} |S )Nr   )FrontDoorManagementClient)azure.mgmt.frontdoorr\   )r   r\   r   r   r   r?         z'AzureFrontDoorBackend._get_client_classc                       t   }|d| j |S Nbase_url)r   r<   
setdefaultrZ   r   rA   r   r   r   r<         
z(AzureFrontDoorBackend._get_client_kwargsc                 C   s"   |j jdi | |d| jiS )Nfront_door_namer   )	endpointspurge_contentrH   rY   r   rQ   rG   r   r   r   rK      s
   
z&AzureFrontDoorBackend._make_purge_callrO   rS   rT   r   r?   r<   rK   rU   r   r   r   r   r	   z   s
    
r	   c                       rV   )	r
   c                    sR   t  | z|d| _|d| _W n ty   tdw |dd | _d S )NCDN_PROFILE_NAMECDN_ENDPOINT_NAMEzgThe setting 'WAGTAILFRONTENDCACHE' requires 'CDN_PROFILE_NAME' and 'CDN_ENDPOINT_NAME' to be specified.CDN_SERVICE_URL)r   r   r   _cdn_profile_name_cdn_endpoint_namer   r   _cdn_service_urlr   r   r   r   r      s   zAzureCdnBackend.__init__c                 C   r[   )Nr   )CdnManagementClient)azure.mgmt.cdnrp   )r   rp   r   r   r   r?      r^   z!AzureCdnBackend._get_client_classc                    r_   r`   )r   r<   rb   ro   rc   r   r   r   r<      rd   z"AzureCdnBackend._get_client_kwargsc                 C   s&   |j jdi | || j| jdS )N)profile_nameendpoint_namer   )rf   rg   rH   rm   rn   rh   r   r   r   rK      s   

z AzureCdnBackend._make_purge_callri   r   r   r   r   r
      s
    r
   )loggingurllib.parser   r   django.core.exceptionsr   baser   	getLoggerrL   __all__r   r	   r
   r   r   r   r   <module>   s    
l