
    Vh                        d Z ddlmZ ddl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
c mc mZ ddZddZddZddZddZdd	Zd
 ZddZy)a  
This module adds shared support for generic api modules

In order to use this module, include it as part of a custom
module as shown below.

The 'api' module provides the following common argument specs:

    * rate limit spec
        - rate: number of requests per time unit (int)
        - rate_limit: time window in which the limit is applied in seconds

    * retry spec
        - retries: number of attempts
        - retry_pause: delay between attempts in seconds
    )annotationsNc                n    t        t        d      t        d            }| r|j                  |        |S )z7Creates an argument spec for working with rate limitinginttype)rate
rate_limitdictupdatespecarg_specs     H/home/dcms/DCMS/lib/python3.12/site-packages/ansible/module_utils/api.pyrate_limit_argument_specr   &   s2    uU#H O    c                p    t        t        d      t        dd            }| r|j                  |        |S )z2Creates an argument spec for working with retryingr   r   float   r   default)retriesretry_pauser
   r   s     r   retry_argument_specr   1   s4    % gq1H Or   c           	         t        t        d      t        dd      t        d      t        dd            }| r|j                  |        |S )Nstrr   T)r   no_logboolr   )api_usernameapi_passwordapi_urlvalidate_certsr
   r   s     r   basic_auth_argument_specr#   <   sF    u%uT2% 6	H Or   c                L    d| |t        |      t        |       z  fd}|S )zrate limiting decoratorNc                      dg fd}|S )Ng        c                     t         j                  dk\  rt        j                  }nt        j                  }6 |       d   z
  }|z
  }|dkD  rt        j
                  |        |       d<    | i |}|S )N)      r   )sysversion_infotimeprocess_timeclocksleep)	argskwargs	real_timeelapsedleftretflastminrates	         r   ratelimitedz0rate_limit.<locals>.wrapper.<locals>.ratelimitedQ   sw    6) --	 JJ	"#+Q/(!8JJt$#+QT$V$CJr    )r5   r8   r6   r7   s   ` @r   wrapperzrate_limit.<locals>.wrapperN   s    u	 r   )r   )r   r	   r:   r7   s      @r   r	   r	   H   s2    GJ2
#eDk1$ Nr   c                      fd}|S )zRetry decoratorc                      fd}|S )Nc                     d}?d }	 |dz  }|k\  rt        dz        	  | i |}|r	 |S t        j                         <y # t         $ r Y 'w xY w)Nr   r   zRetry limit exceeded: %d)	Exceptionr+   r.   )r/   r0   retry_countr4   r5   r   r   s       r   retriedz'retry.<locals>.wrapper.<locals>.retriedg   s    K"1$K"g-'(BW(LMM00 
 JJ{+  # % s   A 	AAr9   )r5   r@   r   r   s   ` r   r:   zretry.<locals>.wrappere   s    	" r   r9   )r   r   r:   s   `` r   retryrA   c   s    ( Nr   c           	   #  |   K   t        d|       D ])  }t        j                  t        ||d|z  z               + yw)aD  The "Full Jitter" backoff strategy.

    Ref: https://www.awsarchitectureblog.com/2015/03/backoff.html

    :param retries: The number of delays to generate.
    :param delay_base: The base time in seconds used to calculate the exponential backoff.
    :param delay_threshold: The maximum time in seconds for any delay.
    r      N)rangesecrets	randbelowmin)r   
delay_basedelay_thresholdrA   s       r   generate_jittered_backoffrJ   |   sA      q'" OOZ!u*5L MNNOs   :<c                     y)NFr9   )exception_or_results    r   retry_neverrM      s    r   c                >    d } ||       ~ t         fd}|S )zGeneric retry decorator.

    :param backoff_iterator: An iterable of delays in seconds.
    :param should_retry_error: A callable that takes an exception of the decorated function and decides whether to retry or not (returns a bool).
    c              3  t   K   t        j                  |       \  }}| 	 t        j                  |       wN)	itertoolsteecopy)original_iterator_copiable_iterator_first_iterator_copys      r   _emit_isolated_iterator_copieszGretry_with_delays_and_condition.<locals>._emit_isolated_iterator_copies   s:     3<==AR3S00"")).// s   68c                H     t        j                          fd       }|S )Nc                     t        j                  g| i |}t              D ]  }	  |       c S   |       S # t        $ r} |      s Y d}~nd}~ww xY wt	        j
                  |       J)zThis assumes the function has not already been called.
            If backoff_iterator is empty, we should still run the function a single time with no delay.
            N)	functoolspartialnextr>   r+   r.   )r/   r0   call_retryable_functiondelayebackoff_iterator_generatorfunctionshould_retry_errors        r   run_functionzOretry_with_delays_and_condition.<locals>.function_wrapper.<locals>.run_function   s~    
 '0&7&7&R4&R6&R#89 "244" +,, ! -a0 1 

5!s   ;	A	AA)rZ   wraps)ra   rc   r`   rb   s   ` r   function_wrapperz9retry_with_delays_and_condition.<locals>.function_wrapper   s&    		"	- 
#	-  r   )rM   )backoff_iteratorrb   rW   re   r`   s    `  @r   retry_with_delays_and_conditionrg      s3    0 "@@P!Q!(& r   rP   )NN)Nr   )
   r'   <   )__doc__
__future__r   rS   rZ   rQ   rE   r)   r+   "ansible.module_utils.compat.typingmodule_utilscompattypingtr   r   r#   r	   rA   rJ   rM   rg   r9   r   r   <module>rq      sP     #     
  . .	62
O%r   