
    g                     b    d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZ  G d de      Zy)    N)call_json_api)uniquebuild_distribution_domainbase64url_encodejson_encodecompute_hex_hashSIGNATURE_SHA256build_arrayc                       e Zd ZdZeZd ddddZdZ	 d Zd Zd Z	d	 Z
dd
Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zy)Search	resourcesc                 *    t        t        |             S N)nextiter)xs    &/opt/Tautulli/lib/cloudinary/search.py<lambda>zSearch.<lambda>   s    T$q']     N)sort_by	aggregate
with_fieldfieldsi,  c                     i | _         y r   queryselfs    r   __init__zSearch.__init__   s	    
r   c                 $    || j                   d<   | S )z$Specify the search query expression.
expressionr   r   values     r   r!   zSearch.expression   s    #(

< r   c                 $    || j                   d<   | S )zSet the max results to returnmax_resultsr   r"   s     r   r%   zSearch.max_results"       $)

=!r   c                 $    || j                   d<   | S )zVGet next page in the query using the ``next_cursor`` value from a previous invocation.next_cursorr   r"   s     r   r(   zSearch.next_cursor'   r&   r   c                 6    |d}| j                  d||i       | S )zGAdd a field to sort results by. If not provided, direction is ``desc``.descr   _add)r   
field_name	directions      r   r   zSearch.sort_by,   s%    I		)j)45r   c                 *    | j                  d|       | S )zAggregate field.r   r+   r"   s     r   r   zSearch.aggregate3   s    		+u%r   c                 *    | j                  d|       | S )z.Request an additional field in the result set.r   r+   r"   s     r   r   zSearch.with_field8   s    		,&r   c                 *    | j                  d|       | S )z1Request which fields to return in the result set.r   r+   r"   s     r   r   zSearch.fields=   s    		(E"r   c                     || _         | S )z
        Sets the time to live of the search URL.

        :param ttl: The time to live in seconds.
        :return: self
        )_ttl)r   ttls     r   r4   z
Search.ttlB   s     	r   c                 H    t        j                  | j                               S r   )jsondumpsas_dictr   s    r   to_jsonzSearch.to_jsonL   s    zz$,,.))r   c                 `    d|d<   | j                   dg}t        d|| j                         fi |S )z&Execute the search and return results.zapplication/jsoncontent_typesearchpost)	_endpointr   r8   )r   optionsuris      r   executezSearch.executeO   s4    "4~~x(VS$,,.DGDDr   c                     i }| j                   j                         D ]1  \  }}|| j                  v rt        || j                  |         }|||<   3 |S r   )r   items_KEYS_WITH_UNIQUE_VALUESr   )r   	to_returnkeyr#   s       r   r8   zSearch.as_dictU   s]    	****, 	#JCd333ud&C&CC&HI"IcN		# r   c           	         |j                  dt        j                         j                  xs d      }|st	        d      || j
                  }| j                         }|j                  dd      }||}t        t        |d            }t        |      }t        dj                  |||      t        	      }	d
j                  ||	|||rdj                  |            S d      S )a  
        Creates a signed Search URL that can be used on the client side.

        :param ttl: The time to live in seconds.
        :param next_cursor: Starting position.
        :param options: Additional url delivery options.
        :return: The resulting search URL.
        
api_secretNzMust supply api_secretr(   T)	sort_keysz{ttl}{b64query}{api_secret})r4   b64queryrH   )	algorithmz9{prefix}/search/{signature}/{ttl}/{b64query}{next_cursor}z/{} )prefix	signaturer4   rJ   r(   )get
cloudinaryconfigrH   
ValueErrorr3   r8   popr   r   r   r   formatr	   )
r   r4   r(   r?   rH   r   _next_cursorrJ   rM   rN   s
             r   to_urlzSearch.to_url`   s    [[z/@/@/B/M/M/UQUV
566;))Cyy5&K#K$FG*73$%B%I%I! &J &
 &	'	 KQQ5@[1 R J 	J
 GI R J 	Jr   c                     || _         | S r   )r>   )r   endpoints     r   rX   zSearch.endpoint   s    !r   c                     || j                   vrg | j                   |<   | j                   |   j                  t        |             | S r   )r   extendr
   )r   namer#   s      r   r,   zSearch._add   s=    tzz!!DJJt

4E 23r   r   )NN)__name__
__module____qualname__ASSETSr>   rD   r3   r   r!   r%   r(   r   r   r   r   r4   r9   rA   r8   rV   rX   r,    r   r   r   r   
   sz    FI +	  D+





*E	%JNr   r   )base64r6   rP   cloudinary.api_client.call_apir   cloudinary.utilsr   r   r   r   r   r	   r
   objectr   r`   r   r   <module>re      s.       8= = =EV Er   