
    1                         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	 ddlm
Z
  G d	 d
ej                        Zy)z*Download workflow used by GCS gRPC client.    )absolute_import)division)unicode_literals)	cloud_api)gcs_download)	grpc_util)
retry_utilc                   \     e Zd ZdZ fdZd Zd Zej                  d        Z	 fdZ
 xZS )GrpcDownloadz$Perform GCS Download using gRPC API.c
                 |    t         
|   |||       || _        || _        || _        || _        || _        |	| _        y)a!  Initializes a GrpcDownload instance.

    Args:
      gapic_client (StorageClient): The GAPIC API client to interact with
        GCS using gRPC.
      cloud_resource (resource_reference.ObjectResource): See
        cloud_api.CloudApi.download_object.
      download_stream (stream): Stream to send the object data to.
      start_byte (int): See super class.
      end_byte (int): See super class.
      digesters (dict): See cloud_api.CloudApi.download_object.
      progress_callback (function): See cloud_api.CloudApi.download_object.
      download_strategy (cloud_api.DownloadStrategy): Download strategy used to
        perform the download.
      decryption_key (encryption_util.EncryptionKey|None): The decryption key
        to be used to download the object if the object is encrypted.
    N)super__init___gapic_client_cloud_resource
_digesters_progress_callback_download_strategy_decryption_key)selfgapic_clientcloud_resourcedownload_stream
start_byteend_byte	digestersprogress_callbackdownload_strategydecryption_key	__class__s             7lib/googlecloudsdk/api_lib/storage/gcs_grpc/download.pyr   zGrpcDownload.__init__   sG    6 
)T#OZJ%D)DDO/D/D)D    c                 0    t        j                  |||      S )See super class.)r	   is_retriable)r   exc_type	exc_valueexc_tracebacks       r    should_retryzGrpcDownload.should_retryA   s    ""8YFFr!   c                     t        j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  	      S )r#   )	r   r   r   r   r   r   r   r   r   )r   download_objectr   r   _download_streamr   r   _start_byte	_end_byter   r   r   s    r    launchzGrpcDownload.launchE   sb    $$''++--//11##11++
 
r!   c                 "    | j                         S )a  Downloads the object.

    On retriable errors, the entire download will be re-performed instead of
    resuming from a particular byte. This is useful for streaming download
    cases.

    Unlike Apitools, the GAPIC client doesn't retry the request by
    default, hence we are using the decorator.

    Returns:
      Encoding string for object if requested. Otherwise, None.
    )r/   r.   s    r    simple_downloadzGrpcDownload.simple_downloadS   s     ;;=r!   c                     | j                   t        j                  j                  k(  r| j	                         S t
        |   d      S )r#   T)retriable_in_flight)r   r   DownloadStrategyONE_SHOTr1   r   run)r   r   s    r    r6   zGrpcDownload.runc   sA    )"<"<"E"EE!!##9d'D'AAr!   )__name__
__module____qualname____doc__r   r(   r/   r	   grpc_default_retryerr1   r6   __classcell__)r   s   @r    r   r      s=    ,!*FG "" #B Br!   r   N)r:   
__future__r   r   r   googlecloudsdk.api_lib.storager   r   'googlecloudsdk.api_lib.storage.gcs_grpcr   r	   GcsDownloadr    r!   r    <module>rB      s5    1 &  ' 4 7 = >MB<++ MBr!   