
    ZTh"                        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	m
Z
mZ d dlmZ erd dlZd dlmZ d d	lmZ  G d
 de      Zy)    )annotations)TYPE_CHECKING)RulesLowLevelClient)ResourceBase)Rule
RuleAction
RuleUpdate)	cel_utilsN)
SiftClient)ChannelReferencec                  H    e Zd ZdZd fdZddd	 	 	 	 	 ddZddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdd	Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z	 d	 	 	 	 	 	 	 ddZ	ddddd	 	 	 	 	 	 	 	 	 ddZ
ddd	 	 	 	 	 	 	 ddZddd	 	 	 	 	 ddZddd	 	 	 	 	 ddZ xZS )RulesAPIAsynca  High-level API for interacting with rules.

    This class provides a Pythonic, notebook-friendly interface for interacting with the RulesAPI.
    It handles automatic handling of gRPC services, seamless type conversion, and clear error handling.

    All methods in this class use the Rule class from the low-level wrapper, which is a user-friendly
    representation of a rule using standard Python data structures and types.
    c                n    t         |   |       t        | j                  j                        | _        y)zaInitialize the RulesAPI.

        Args:
            sift_client: The Sift client to use.
        )grpc_clientN)super__init__r   clientr   _low_level_client)selfsift_client	__class__s     `/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_client/resources/rules.pyr   zRulesAPIAsync.__init__   s)     	%!4AXAX!Y    Nrule_id
client_keyc               z   K   | j                   j                  ||       d{   }| j                  |      S 7 w)zGet a Rule.

        Args:
            rule_id: The ID of the rule.
            client_key: The client key of the rule.

        Returns:
            The Rule.
        r   N)r   get_rule_apply_client_to_instance)r   r   r   rules       r   getzRulesAPIAsync.get$   s>      ++44WQ[4\\--d33 ]   !;9;F)namename_contains
name_regexorder_bylimitinclude_deletedc               P  K   t        |du      t        |du      z   t        |du      z   dkD  rt        d      g }|r%|j                  t        j                  d|             |r%|j                  t        j
                  d|             |r%|j                  t        j                  d|             |s$|j                  t        j                  d             |rdj                  |      nd}| j                  j                  ||||       d{   }	| j                  |	      S 7 w)	a  List rules with optional filtering.

        Args:
            name: Exact name of the rule.
            name_contains: Partial name of the rule.
            name_regex: Regular expression string to filter rules by name.
            order_by: How to order the retrieved rules.
            limit: How many rules to retrieve. If None, retrieves all matches.
            include_deleted: Include deleted rules.

        Returns:
            A list of Rules that matches the filter.
        N   z@Must use EITHER name, name_contains, or name_regex, not multipler#   deleted_datez &&  )filter_queryr&   max_results	page_size)int
ValueErrorappendcelequalscontainsmatchequals_nulljoinr   list_all_rules_apply_client_to_instances)
r   r#   r$   r%   r&   r'   r(   filters
filter_strruless
             r   list_zRulesAPIAsync.list_6   s    . t4 3}D'@#AAC
Z^H^D__bcc_``NN3::fd34NN3<<>?NN399VZ89NN3??>:;-4V[[)"
,,;;#	 < 
 
 ..u55
s   DD&D$D&c                   K    | j                   di | d{   }t        |      dkD  rt        d      t        |      dk(  r|d   S y7 1w)a  Find a single rule matching the given query. Takes the same arguments as `list`. If more than one rule is found,
        raises an error.

        Args:
            **kwargs: Keyword arguments to pass to `list`.

        Returns:
            The Rule found or None.
        Nr*   zMultiple rules found for queryr    )r>   lenr1   )r   kwargsr=   s      r   findzRulesAPIAsync.findb   sQ      !djj*6**u:>=>>Z1_8O +s   AA
2Ac                   K   | j                   j                  |||||||||	|

       d{   }| j                  |      S 7 w)zCreate a new rule.)
r#   descriptionorganization_id
expressionactionchannel_referencesr   	asset_idscontextual_channelsis_externalN)r   create_ruler   )r   r#   rE   rG   rI   rH   rF   r   rJ   rK   rL   created_rules               r   createzRulesAPIAsync.creates   s_      "33??#+!1! 3# @ 
 
 --l;;
s   )AAAc                  K   t        |t              r| j                  |       d{   }t        |t              rt	        j
                  |      }| j                  j                  |||       d{   }| j                  |      S 7 _7 w)zUpdate a Rule.

        Args:
            rule: The Rule or rule ID to update.
            update: Updates to apply to the Rule.
            version_notes: Notes to include in the rule version.

        Returns:
            The updated Rule.
        r   N)	
isinstancestrr!   dictr	   model_validater   update_ruler   )r   r    updateversion_notesupdated_rules        r   rW   zRulesAPIAsync.update   sy      dC $//Dfd#..v6F!33??fm\\--l;; 0
 ]s"   &BBA	B2B
3B
B)r    r=   rule_idsclient_keysc                 K   |rdt        |t              r/| j                  j                  |j                         d{    y| j                  j                  |       d{    y|r}t        |      dk(  r2| j                  j                  |d   j                         d{    y| j                  j                  |D cg c]  }|j                   c}       d{    y|r[t        |      dk(  r(| j                  j                  |d          d{    y| j                  j                  |       d{    y|r%| j                  j                  |       d{    yt        d      7 97 7 c c}w 7 7 k7 H7 #w)zArchive a rule or multiple.

        Args:
            rule: The Rule to archive.
            rules: The Rules to archive.
            rule_ids: The rule IDs to archive.
            client_keys: The client keys to archive.
        rQ   Nr*   r   )rZ   )r[   z7Either rules, rule_ids, or client_keys must be provided)rR   r   r   archive_ruleid_rA   batch_archive_rulesr1   )r   r    r=   rZ   r[   rs         r   archivezRulesAPIAsync.archive   sa      $%,,99$((9KKK,,99$9GGG5zQ,,99%(,,9OOO,,@@-23aee3 A    8}!,,99(1+9NNN,,@@(@SSS((<<<UUUVWW% LG P 4
 OSUs   <FE7$F#E:$AF%E=&FE?F"F#7FF$F?F &F&F
'F:F=F?FFF
Fc                 K   |s|r&| j                   j                  ||       d{   }nBt        |t              r|j                  n|}| j                   j                  |       d{   }| j                  |      S 7 X7 w)a*  Restore a rule.

        Args:
            rule: The Rule or rule ID to restore.
            rule_id: The rule ID to restore (alternative to rule parameter).
            client_key: The client key to restore (alternative to rule parameter).

        Returns:
            The restored Rule.
        r   NrQ   )r   restore_rulerR   r   r^   r   )r   r    r   r   restored_rules        r   restorezRulesAPIAsync.restore   s     " j"&"8"8"E"EJ #F # M #-T4"8dhhdG"&"8"8"E"Eg"E"VVM--m<<
 Ws"   %BB AB*B+BBrZ   r[   c               Z   K   | j                   j                  ||       d{    y7 w)zBatch restore rules.

        Args:
            rule_ids: List of rule IDs to restore.
            client_keys: List of client keys to undelete.
        rf   N)r   batch_restore_rules)r   rZ   r[   s      r   batch_restorezRulesAPIAsync.batch_restore   s(      $$88(Xc8ddds   !+)+c               z   K   | j                   j                  ||       d{   }| j                  |      S 7 w)zGet multiple rules by rule IDs or client keys.

        Args:
            rule_ids: List of rule IDs to get.
            client_keys: List of client keys to get.

        Returns:
            List of Rules.
        rf   N)r   batch_get_rulesr:   )r   rZ   r[   r=   s       r   	batch_getzRulesAPIAsync.batch_get   sE      ,,<<; = 
 
 ..u55
r"   )r   r   )r   
str | Noner   rm   returnr   )r#   rm   r$   rm   r%   zstr | re.Pattern | Noner&   rm   r'   z
int | Noner(   boolrn   
list[Rule])rn   zRule | None)NNNNF)r#   rS   rE   rS   rG   rS   rI   zlist[ChannelReference]rH   r   rF   rm   r   rm   rJ   list[str] | NonerK   rq   rL   ro   rn   r   )N)r    
str | RulerW   zRuleUpdate | dictrX   rm   rn   r   )
r    zstr | Rule | Noner=   zlist[Rule] | NonerZ   rq   r[   rq   rn   None)r    rr   r   rm   r   rm   rn   r   )rZ   rq   r[   rq   rn   rs   )rZ   rq   r[   rq   rn   rp   )__name__
__module____qualname____doc__r   r!   r>   rC   rO   rW   ra   re   ri   rl   __classcell__)r   s   @r   r   r      s'   Z #!%	4 4 	4
 
4*  $(.2#  %*6 *6 "	*6
 ,*6 *6 *6 *6 
*6X0 '+!%&*04!<< < 	<
 3< < $< < $< .< < 
<: X\<<(9<JT<	<2 #'#'%)(,$X  $X !	$X
 #$X &$X 
$XT #!%= = 	=
 = 
=< &*(,	e #e &	e
 
e" &*(,	6 #6 &	6
 
6r   r   )
__future__r   typingr   .sift_client._internal.low_level_wrappers.rulesr   sift_client.resources._baser   sift_client.sift_types.ruler   r   r	   sift_client.utilr
   r3   resift_client.clientr   sift_client.sift_types.channelr   r   r@   r   r   <module>r      s4    "   N 4 D D --?u6L u6r   