
                             d 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      Z G d d	e      Z	 G d
 de	      Z
 G d de	      Zy)z2Generic implementations of Apigee Management APIs.    )absolute_import)division)unicode_literals)requestc                   L    e Zd ZdZdZ	 edd       Zedd       Zedd       Zy)
BaseClientz-Base class for Apigee Management API clients.Nc                     | j                   t        d| z        t        j                  |xs i | j                   d d | j                   d   |      S )N%%s class must provide an entity path.)query_params_entity_pathNotImplementedErrorr   ResponseToApiRequest)clsidentifiersextra_paramss      )lib/googlecloudsdk/api_lib/apigee/base.pyListzBaseClient.List!   s[    
 G# MNN''r"!	# #    c                 ~    | j                   t        d| z        t        j                  |xs i | j                         S )Nr
   r   r   r   s     r   DescribezBaseClient.Describe+   s=    
 G# MNN''(9r3;K;KLLr   c                     | j                   t        d| z        t        j                  |xs i | j                   d      S )Nr
   DELETE)methodr   r   s     r   DeletezBaseClient.Delete1   sD    
 G# MNN''r3++H> >r   NN)N)	__name__
__module____qualname____doc__r   classmethodr   r   r    r   r   r   r      sM    5,
 # # M M
 > >r   r   c                   $    e Zd ZdZdZed        Zy)PagedListClientzClient for `List` APIs that can only return a limited number of objects.

  Attributes:
    _list_container: the field name in the List API's response that contains the
      list of objects. None if the API returns a list directly.
  Nc                     | j                   |S 	 || j                      S # t        $ r | | j                   |f}t        d|z        t        t        f$ r | d|}t        |      w xY w)z0Returns a list of the results in `result_chunk`.zS%s specifies a _list_container %r that's not present in API responses.
Response: %rz{ specifies a _list_container, implying that the API response should be a JSON object, but received something else instead: )_list_containerKeyErrorAssertionError
IndexError	TypeError)r   result_chunkfailure_infoerrors       r   _NormalizedResultChunkz&PagedListClient._NormalizedResultChunkB   s     ""#--.. 53..=l%'345 5 	" " '*<9e 5!!	"s
    AA&)r   r    r!   r"   r(   r#   r0   r$   r   r   r&   r&   9   s      /" "r   r&   c                   >     e Zd ZdZdZdZdZdZed fd	       Z	 xZ
S )TokenPagedListClienta  Client for paged `List` APIs that identify pages using a page token.

  This is the AIP-approved way to paginate results and is preferred for new
  APIs.

  Attributes:
    _page_token_field: the field name in the List API's response that contains
      an explicit page token.
    _list_container: the field name in the List API's response that contains the
      list of objects.
    _page_token_param: the query parameter for the previous page's token.
    _max_per_page: the maximum number of items that can be returned in each List
      response.
    _limit_param: the query parameter for the number of items to be returned on
      each page.
  nextPageToken	pageTokend   pageSizec              #   n  K   | j                   d| z  }t        |      | j                  | j                  i}|r|j	                  |       	 t
        t        |   ||      }| j                  |      D ]  }|  | j                  |v r,|| j                     r|| j                     || j                  <   iy w)NzG%s does not specify a _list_container, but token pagination requires it)r(   r*   _limit_param_max_per_pageupdatesuperr2   r   r0   _page_token_field_page_token_param)r   r   r   r/   paramsresponseitem	__class__s          r   r   zTokenPagedListClient.Listk   s     
"!$&e5!! 1 12FmmL!
+S6{FKh,,X6$
 7 
		(	*x8M8M/N(01F1F(Gs$$% s   B2B5r   )r   r    r!   r"   r<   r=   r9   r8   r#   r   __classcell__rA   s   @r   r2   r2   U   s2      &!-, r   r2   c                   >     e Zd ZdZdZdZdZdZed fd	       Z	 xZ
S )FieldPagedListClienta  Client for paged `List` APIs that identify pages using a page field.

  This is the pagination method used by legacy Apigee CG APIs, and has been
  preserved for backwards compatibility in Apigee's GCP offering.

  Attributes:
    _list_container: the field name in the List API's response that contains the
      list of objects. None if the API returns a list directly.
    _page_field: the field name in each list element that can be used as a page
      identifier. PageListClient will take the value of this field in the last
      list item for a page, and use it as the  _start_at_param for the next
      page. None if each list element is a primitive which can be used for this
      purpose directly.
    _max_per_page: the maximum number of items that can be returned in each List
      response.
    _limit_param: the query parameter for the number of items to be returned on
      each page.
    _start_at_param: the query parameter for where in the available data the
      response should begin.
  Ni  countstartKeyc              #     K   || j                   }| j                  | j                  i}|r|j                  |       	 t        t
        |   ||      }|s||vry | j                  | j                  |      }|d | j                  dz
   D ]  }|  t        |      | j                  k  ry |d   }| j                  || j                     }|||<   w)N   r   )_start_at_paramr8   r9   r:   r;   rE   r   r(   r0   len_page_field)	r   r   start_at_paramr   r>   r-   r@   last_item_on_pagerA   s	           r   r   zFieldPagedListClient.List   s     **n 1 12FmmL!
/:;OlnF:				( 11,?
 5 1 1A 56$
 7 
\	S..	. 	&r*		$-coo>0f^5 s   CC)NNN)r   r    r!   r"   rL   r9   r8   rJ   r#   r   rB   rC   s   @r   rE   rE      s0    * +-,/ 1  1r   rE   N)r"   
__future__r   r   r   !googlecloudsdk.command_lib.apigeer   objectr   r&   r2   rE   r$   r   r   <module>rR      sH    9 &  ' 5> >B"j "8*? *Z<1? <1r   