o
    Dfy                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ z"dd	lZdd
lmZ ddlmZmZmZ ddlmZ ddlmZ W n ey_   d	 Z Z Z Z Z ZZY nw dZdZdZe
eZG dd deZd	S )z3The CosmosDB/SQL backend for Celery (experimental).    )cached_property)bytes_to_str)
_parse_url)ImproperlyConfigured)
get_logger   )KeyValueStoreBackendN)DocumentClient)ConnectionPolicyConsistencyLevelPartitionKind)HTTPFailure)RetryOptions)CosmosDBSQLBackendi  i  c                       s   e Zd ZdZ						d fdd	Zedd Zedd Zd	d
 Z	dd Z
edd Zedd Zdd Zedd Zdd Zdd Zdd Zdd Z  ZS )r   z CosmosDB/SQL backend for Celery.Nc           
         s   t  j|i | td u rtd| jj}	| |\| _| _|p#|	d | _	|p*|	d | _
ztt|p4|	d | _W n tyC   tdw |pI|	d | _|pP|	d | _d S )NzIYou need to install the pydocumentdb library to use the CosmosDB backend.cosmosdbsql_database_namecosmosdbsql_collection_namecosmosdbsql_consistency_levelz"Unknown CosmosDB consistency levelcosmosdbsql_max_retry_attemptscosmosdbsql_max_retry_wait_time)super__init__pydocumentdbr   appconfr   	_endpoint_key_database_name_collection_namegetattrr   _consistency_levelAttributeError_max_retry_attempts_max_retry_wait_time)
selfurldatabase_namecollection_nameconsistency_levelmax_retry_attemptsmax_retry_wait_timeargskwargsr   	__class__ T/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/backends/cosmosdbsql.pyr   !   s8   	
zCosmosDBSQLBackend.__init__c                 C   sZ   t |\}}}}}}}|r|std|sd}|dkrdnd}| d| d| }||fS )NzInvalid URLi  httpshttpz://:)r   r   )clsr$   _hostportpasswordschemeendpointr.   r.   r/   r   M   s   zCosmosDBSQLBackend._parse_urlc                 C   sJ   t  }t| j| jd|_t| jd| ji|| jd}| | | 	| |S )zReturn the CosmosDB/SQL client.

        If this is the first call to the property, the client is created and
        the database and collection are initialized if they don't yet exist.

        )max_retry_attempt_countmax_wait_time_in_seconds	masterKey)connection_policyr'   )
r
   r   r!   r"   r	   r   r   r   _create_database_if_not_exists _create_collection_if_not_exists)r#   r=   clientr.   r.   r/   _client[   s   

zCosmosDBSQLBackend._clientc              
   C   sZ   z
| d| ji W n ty# } z|jtkr W Y d }~d S d }~ww td| j d S )NidzCreated CosmosDB database %s)CreateDatabaser   r   status_codeERROR_EXISTSLOGGERinfor#   r@   exr.   r.   r/   r>   s   s   
z1CosmosDBSQLBackend._create_database_if_not_existsc              
   C   sn   z| | j| jdgtjdd W n ty+ } z|jtkr  W Y d }~d S d }~ww t	d| j
| j d S )Nz/id)pathskind)rB   partitionKeyz!Created CosmosDB collection %s/%s)CreateCollection_database_linkr   r   Hashr   rD   rE   rF   rG   r   rH   r.   r.   r/   r?   }   s$   
z3CosmosDBSQLBackend._create_collection_if_not_existsc                 C   s
   d| j  S )Nzdbs/)r   r#   r.   r.   r/   rN      s   
z!CosmosDBSQLBackend._database_linkc                 C   s   | j d | j S )Nz/colls/)rN   r   rP   r.   r.   r/   _collection_link   s   z#CosmosDBSQLBackend._collection_linkc                 C   s   | j d | S )Nz/docs/)rQ   r#   keyr.   r.   r/   _get_document_link   s   z%CosmosDBSQLBackend._get_document_linkc                 C   s   |r|  r
tdd|iS )Nz(Key cannot be none, empty or whitespace.rL   )isspace
ValueError)r3   rS   r.   r.   r/   _get_partition_key   s   z%CosmosDBSQLBackend._get_partition_keyc              
   C   sx   t |}td| j| j| z| j| || |}W n t	y6 } z|j
tkr+ W Y d}~dS d}~ww |dS )zxRead the value stored at the given key.

        Args:
              key: The key for which to read the value.

        z"Getting CosmosDB document %s/%s/%sNvalue)r   rF   debugr   r   rA   ReadDocumentrT   rW   r   rD   ERROR_NOT_FOUNDget)r#   rS   documentrI   r.   r.   r/   r\      s    


zCosmosDBSQLBackend.getc                 C   s>   t |}td| j| j| | j| j||d| | dS )zStore a value for a given key.

        Args:
              key: The key at which to store the value.
              value: The value to store.

        z#Creating CosmosDB document %s/%s/%s)rB   rX   N)	r   rF   rY   r   r   rA   CreateDocumentrQ   rW   )r#   rS   rX   r.   r.   r/   set   s   
zCosmosDBSQLBackend.setc                    s    fdd|D S )zqRead all the values for the provided keys.

        Args:
              keys: The list of keys to read.

        c                    s   g | ]}  |qS r.   )r\   ).0rS   rP   r.   r/   
<listcomp>   s    z+CosmosDBSQLBackend.mget.<locals>.<listcomp>r.   )r#   keysr.   rP   r/   mget   s   zCosmosDBSQLBackend.mgetc                 C   s:   t |}td| j| j| | j| || | dS )zlDelete the value at a given key.

        Args:
              key: The key of the value to delete.

        z#Deleting CosmosDB document %s/%s/%sN)	r   rF   rY   r   r   rA   DeleteDocumentrT   rW   rR   r.   r.   r/   delete   s   
zCosmosDBSQLBackend.delete)NNNNNN)__name__
__module____qualname____doc__r   classmethodr   r   rA   r>   r?   rN   rQ   rT   rW   r\   r_   rc   re   __classcell__r.   r.   r,   r/   r      s4    ,





	r   )ri   kombu.utilsr   kombu.utils.encodingr   kombu.utils.urlr   celery.exceptionsr   celery.utils.logr   baser   r   pydocumentdb.document_clientr	   pydocumentdb.documentsr
   r   r   pydocumentdb.errorsr   pydocumentdb.retry_optionsr   ImportError__all__r[   rE   rf   rF   r   r.   r.   r.   r/   <module>   s2    