o
    tBh4                     @   s   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
Z
 dZdZdZdZd	ZeeZd
d ZefddZefddZd'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G d%d& d&e Z!dS )(z4Shared utilities used by both downloads and uploads.    )absolute_importN)commonrangezcontent-rangezCurrently using crcmod in pure python form. This is a slow implementation. Python 3 has a faster implementation, `google-crc32c`, which will be used if it is installed.zx-goog-hashzNo {checksum_type} checksum was returned from the service while downloading {}
(which happens for composite objects), so client-side content integrity
checking is not being performed.c                   C      dS )zSimple default callback.N r   r   r   v/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/google/resumable_media/_helpers.py
do_nothing-       r   c                 C   s,   || }||vr|  t | d||| S )a  Checks that a specific header is in a headers dictionary.

    Args:
        response (object): An HTTP response object, expected to have a
            ``headers`` attribute that is a ``Mapping[str, str]``.
        name (str): The name of a required header.
        get_headers (Callable[Any, Mapping[str, str]]): Helper to get headers
            from an HTTP response.
        callback (Optional[Callable]): A callback that takes no arguments,
            to be executed when an exception is being raised.

    Returns:
        str: The desired header.

    Raises:
        ~google.resumable_media.common.InvalidResponse: If the header
            is missing.
    z$Response headers must contain headerr   InvalidResponse)responsenameget_headerscallbackheadersr   r   r   header_required1   s   r   c                 C   s2   || }||vr|  t j| d|dg|R  |S )a`  Require a response has a status code among a list.

    Args:
        response (object): The HTTP response object.
        status_codes (tuple): The acceptable status codes.
        get_status_code (Callable[Any, int]): Helper to get a status code
            from a response.
        callback (Optional[Callable]): A callback that takes no arguments,
            to be executed when an exception is being raised.

    Returns:
        int: The status code.

    Raises:
        ~google.resumable_media.common.InvalidResponse: If the status code
            is not one of the values in ``status_codes``.
    zRequest failed with status codezExpected one ofr
   )r   status_codesget_status_coder   status_coder   r   r   require_status_codeN   s   r          @c                 C   s0   ||  }||kr
|}t dd}||d|  fS )a  Calculate the amount of time to wait before a retry attempt.

    Wait time grows exponentially with the number of attempts, until
    ``max_sleep``.

    A random amount of jitter (between 0 and 1 seconds) is added to spread out
    retry attempts from different clients.

    Args:
        base_wait (float): The "base" wait time (i.e. without any jitter)
            that will be multiplied until it reaches the maximum sleep.
        max_sleep (float): Maximum value that a sleep time is allowed to be.
        multiplier (float): Multiplier to apply to the base wait.

    Returns:
        Tuple[float, float]: The new base wait time as well as the wait time
        to be applied (with a random amount of jitter between 0 and 1 seconds
        added).
    r   i  gMbP?)randomrandint)	base_wait	max_sleep
multipliernew_base_wait	jitter_msr   r   r   calculate_retry_waitm   s
   r   c              
   C   s   d}d}t |j|j }zt }W n ty   d}Y nw 	 d}z|  }W n |y9 }	 z|	}W Y d}	~	nd}	~	ww ||tjvrC|S |||sO|rM||S t||j	|j\}}
|d7 }||
7 }t
|
 q)a[  Attempts to retry a call to ``func`` until success.

    Expects ``func`` to return an HTTP response and uses ``get_status_code``
    to check if the response is retry-able.

    Will retry until :meth:`~.RetryStrategy.retry_allowed` (on the current
    ``retry_strategy``) returns :data:`False`. Uses
    :func:`calculate_retry_wait` to double the wait time (with jitter) after
    each attempt.

    Args:
        func (Callable): A callable that takes no arguments and produces
            an HTTP response which will be checked as retry-able.
        get_status_code (Callable[Any, int]): Helper to get a status code
            from a response.
        retry_strategy (~google.resumable_media.common.RetryStrategy): The
            strategy to use if the request fails and must be retried.

    Returns:
        object: The return value of ``func``.
    g        r   r   TN   )floatinitial_delayr   _get_connection_error_classesImportErrorr   	RETRYABLEretry_allowedr   r   timesleep)funcr   retry_strategytotal_sleepnum_retriesr   connection_error_exceptionserrorr   e	wait_timer   r   r   wait_and_retry   s:   



r0   c                  C   s`   zddl } |  }W |S  ty/   zddl}|jd}t  W Y |S  ty.   tdw w )zGet crc32c object
    Attempt to use the Google-CRC32c package. If it isn't available, try
    to use CRCMod. CRCMod might be using a 'slow' varietal. If so, warn...
    r   Nzcrc-32cz3Failed to import either `google-crc32c` or `crcmod`)google_crc32cChecksumr#   crcmod
predefinedCrc_is_fast_crcmod)r1   crc_objr3   r   r   r   _get_crc32c_object   s   

r8   c                  C   s:   t dt t dgd} t| dd}|stjttdd |S )Nzcrcmod.crcmod_usingExtensionr   F   )
stacklevel)
__import__globalslocalsgetattrwarningswarn_SLOW_CRC32C_WARNINGRuntimeWarning)nested_crcmodfast_crcr   r   r   r6      s   r6   c                 C   s   | dkrdS | S )Nmd5md5Hashr   checksum_typer   r   r   _get_metadata_key   s   rJ   c                 C   s   t | }|dS )zConvert a checksum object into a digest encoded for an HTTP header.

    Args:
        bytes: A checksum digest bytestring.

    Returns:
        str: A base64 string representation of the input.
    zutf-8)base64	b64encodedecode)digest_bytestringencoded_digestr   r   r   prepare_checksum_digest   s   
	
rP   c                 C   s   |dvrt d|dv rF|| }t|t| |d}|du r3tj|| d}t| t	 }||fS |dkr?t
 }||fS t }||fS d}t	 }||fS )a  Get the expected checksum and checksum object for the download response.

    Args:
        response (~requests.Response): The HTTP response object.
        get_headers (callable: response->dict): returns response headers.
        media_url (str): The URL containing the media to be downloaded.
        checksum_type Optional(str): The checksum type to read from the headers,
            exactly as it will appear in the headers (case-sensitive). Must be
            "md5", "crc32c" or None.

    Returns:
        Tuple (Optional[str], object): The expected checksum of the response,
        if it can be detected from the ``X-Goog-Hash`` header, and the
        appropriate checksum object for the expected checksum.
    )rF   crc32cN4checksum must be ``'md5'``, ``'crc32c'`` or ``None``)rF   rQ   )checksum_labelNrH   rF   )
ValueError_parse_checksum_headerget_HASH_HEADER_MISSING_CHECKSUMformatupper_LOGGERinfo_DoNothingHashhashlibrF   r8   )r   r   	media_urlrI   r   expected_checksummsgchecksum_objectr   r   r   _get_expected_checksum  s,   

rc   c                 C   s   | du rdS g }|  dD ]}| dd\}}| |kr"|| qt|dkr+dS t|dkr5|d S t|d|| |)aD  Parses the checksum header from an ``X-Goog-Hash`` value.

    .. _header reference: https://cloud.google.com/storage/docs/                          xml-api/reference-headers#xgooghash

    Expects ``header_value`` (if not :data:`None`) to be in one of the three
    following formats:

    * ``crc32c=n03x6A==``
    * ``md5=Ojk9c3dhfxgoKVVHYwFbHQ==``
    * ``crc32c=n03x6A==,md5=Ojk9c3dhfxgoKVVHYwFbHQ==``

    See the `header reference`_ for more information.

    Args:
        header_value (Optional[str]): The ``X-Goog-Hash`` header from
            a download response.
        response (~requests.Response): The HTTP response object.
        checksum_label (str): The label of the header value to read, as in the
            examples above. Typically "md5" or "crc32c"

    Returns:
        Optional[str]: The expected checksum of the response, if it
        can be detected from the ``X-Goog-Hash`` header; otherwise, None.

    Raises:
        ~google.resumable_media.common.InvalidResponse: If there are
            multiple checksums of the requested type in ``header_value``.
    N,=r   r   z.X-Goog-Hash header had multiple ``{}`` values.)splitlstripappendlenr   r   rY   )header_valuer   rS   matcheschecksumr   valuer   r   r   rU   -  s$   
rU   c                 C   s2   | dkrt  S | dkrt S | du rdS td)z~Respond with a checksum object for a supported type, if not None.

    Raises ValueError if checksum_type is unsupported.
    rF   rQ   NrR   )r^   rF   r8   rT   rH   r   r   r   _get_checksum_objectb  s   rn   c                  C   s   ddl } | jj| jjfS )a  Get the exception error classes.

    Requests is a soft dependency here so that multiple transport layers can be
    added in the future. This code is in a separate function here so that the
    test framework can override its behavior to simulate requests being
    unavailable.r   N)requests.exceptions
exceptionsConnectionErrorChunkedEncodingError)requestsr   r   r   r"   q  s   r"   c                   @   s   e Zd ZdZdd ZdS )r]   zDo-nothing hash object.

    Intended as a stand-in for ``hashlib.md5`` or a crc32c checksum
    implementation in cases where it isn't necessary to compute the hash.
    c                 C   r   )zDo-nothing ``update`` method.

        Intended to match the interface of ``hashlib.md5`` and other checksums.

        Args:
            unused_chunk (bytes): A chunk of data.
        Nr   )selfunused_chunkr   r   r   update  r	   z_DoNothingHash.updateN)__name__
__module____qualname____doc__rv   r   r   r   r   r]     s    r]   )r   )"rz   
__future__r   rK   r^   loggingr   r&   r@   google.resumable_mediar   RANGE_HEADERCONTENT_RANGE_HEADERrB   rW   rX   	getLoggerrw   r[   r   r   r   r   r0   r8   r6   rJ   rP   rc   rU   rn   r"   objectr]   r   r   r   r   <module>   s<   

@*5