
                             d Z ddlmZ ddlmZ ddlmZ ddl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ZdZediZd ZddZd Zy)z1Utilities used by gcloud functions call commands.    )absolute_import)division)unicode_literalsN)base)parser_extensions)
properties)requests)timeszContent-Type)idsourcespecversiontype
dataschemasubjecttimeapplication/jsonc                    i }d}t        | j                               D ]B  \  }}|j                         }|dk(  r|}|t        v r	||d|z   <   /|dk(  r
||t        <   >|||<   D t        |vr	d|t        <   t        j                  |      |fS )a  Convert CloudEvents structured format to binary format.

  Args:
    request_data_json: dict, the parsed request body data

  Returns:
    cloudevent_data: str, the CloudEvent expected data with attributes in header
    cloudevent_headers: dict, the CloudEvent headers
  Ndatazce-datacontenttyper   )listitemslower_FIELDS_CONTENT_TYPEjsondumps)request_data_jsoncloudevent_headerscloudevent_datakeyvaluenormalized_keys         5lib/googlecloudsdk/command_lib/functions/call_util.py_StructuredToBinaryDatar$   +   s     /*0023jc5YY[No	7	"16~-.	,	,*/'+0( 4 ,,(:}%	O	$&8	88    c                    d}t         }|j                  r|j                  }t         }n7|j                  r+t        t	        j
                  |j                              \  }}|r*t        |j                         |j                         z        }t        j                         }|j                  | ||      }|j                          |j                  S )a.  Makes an HTTP Post Request to the specified url with data and headers from args.

  Args:
    url: The URL to send the post request to
    args: The arguments from the command line parser
    extra_headers: Extra headers to add to the HTTP post request

  Returns:
    str: The HTTP response content
  N)urlr   headers)_DEFAULT_HEADERSr   cloud_eventr$   r   loadsdictr   core_requests
GetSessionpostraise_for_statuscontent)r'   argsextra_headersrequest_datar(   requests_sessionresponses          r#   MakePostRequestr7   I   s     ,'	YY99LG3

4##$&L' 7==?]%8%8%::;G"--/""
 # ( 			r%   c                 R   |t         j                  j                  t         j                  j                  fv r|dk(  rdnd}| j                  rt        | j                        }n|dk(  rD|j                  r8t        t        j                  |j                  d      j                  dz         }nI|dk(  rD|j                  r8|j                  j                  r"t        |j                  j                        dz   }t        j                  j                  j                  j                  |       yy)	zUpdate core/http_timeout using args and function timeout.

  Args:
    args: The arguments from the command line parser
    function: function definition
    api_version: v1 or v2
    release_track: ALPHA, BETA, or GA
  v1i  i  s)default_suffix   v2N)r   ReleaseTrackALPHABETAtimeoutintr
   ParseDurationtotal_secondsserviceConfigtimeoutSecondsr   VALUEScorehttp_timeoutSet)r2   functionapi_versionrelease_trackrA   s        r#   UpdateHttpTimeoutrN   k   s     t((..0A0A0F0FGG D(cdG||DLL!g		!1!1


sMg 	t""""11H**99:R?g''++G4# Hr%   )N)__doc__
__future__r   r   r   r   googlecloudsdk.callioper   r   googlecloudsdk.corer   r	   r-   googlecloudsdk.core.utilr
   r   r   r)   r$   r7   rN    r%   r#   <module>rU      sQ    8 &  '  ( 5 * 9 *  "#56 9<D5r%   