B ㊇c@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZmZmZdZdZd Zdd d ZGd ddZGdddeZGdddeZGdddeZGdddeZGdddeeZGdddeZeeeeedZdS)aProtocol input serializes. This module contains classes that implement input serialization for the various AWS protocol types. These classes essentially take user input, a model object that represents what the expected input should look like, and it returns a dictionary that contains the various parts of a request. A few high level design decisions: * Each protocol type maps to a separate class, all inherit from ``Serializer``. * The return value for ``serialize_to_request`` (the main entry point) returns a dictionary that represents a request. This will have keys like ``url_path``, ``query_string``, etc. This is done so that it's a) easy to test and b) not tied to a particular HTTP library. See the ``serialize_to_request`` docstring for more details. Unicode ------- The input to the serializers should be text (str/unicode), not bytes, with the exception of blob types. Those are assumed to be binary, and if a str/unicode type is passed in, it will be encoded as utf-8. N) ElementTree)validate) formatdate) has_headeris_json_value_headerparse_to_aware_datetimepercent_encodeiso8601z%Y-%m-%dT%H:%M:%SZz%Y-%m-%dT%H:%M:%S.%fZTcCs&t|}|r"t}t||}|S)N) SERIALIZERSrZParamValidatorZParamValidationDecorator)Z protocol_nameZinclude_validation serializer validatorr m/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/botocore/serialize.pycreate_serializer?s   rc@sbeZdZdZeZdZddZddZddZ d d Z d d Z dddZ ddZ ddZddZd S) SerializerPOSTzutf-8cCs tddS)aSerialize parameters into an HTTP request. This method takes user provided parameters and a shape model and serializes the parameters to an HTTP request. More specifically, this method returns information about parts of the HTTP request, it does not enforce a particular interface or standard for an HTTP request. It instead returns a dictionary of: * 'url_path' * 'host_prefix' * 'query_string' * 'headers' * 'body' * 'method' It is then up to consumers to decide how to map this to a Request object of their HTTP library of choice. Below is an example return value:: {'body': {'Action': 'OperationName', 'Bar': 'val2', 'Foo': 'val1', 'Version': '2014-01-01'}, 'headers': {}, 'method': 'POST', 'query_string': '', 'host_prefix': 'value.', 'url_path': '/'} :param parameters: The dictionary input parameters for the operation (i.e the user input). :param operation_model: The OperationModel object that describes the operation. serialize_to_requestN)NotImplementedError)self parametersoperation_modelr r rrQs$zSerializer.serialize_to_requestcCsdd|jidd}|S)N/)url_path query_stringmethodheadersbody)DEFAULT_METHOD)r serializedr r r_create_default_requestws z"Serializer._create_default_requestcCs|jdkrt}nt}||S)Nr) microsecond ISO8601_MICROISO8601strftime)rvaluetimestamp_formatr r r_timestamp_iso8601s zSerializer._timestamp_iso8601cCstt|S)N)intcalendartimegm timetuple)rr&r r r_timestamp_unixtimestampsz#Serializer._timestamp_unixtimestampcCs"t|tjr||}t|ddS)NT)usegmt) isinstancedatetimer-r)rr&r r r_timestamp_rfc822s  zSerializer._timestamp_rfc822NcCs:|dkr|j}|}t|}t|d|}||}|S)NZ _timestamp_)TIMESTAMP_FORMATlowerrgetattr)rr&r' datetime_obj converterZ final_valuer r r_convert_timestamp_to_strsz$Serializer._convert_timestamp_to_strcCs|jd|S)Nname) serializationget)rshape default_namer r r_get_serialized_nameszSerializer._get_serialized_namecCs,t|tr||j}t||jS)N)r/strencodeDEFAULT_ENCODINGbase64 b64encodestripdecode)rr&r r r _get_base64s  zSerializer._get_base64csZ|j}|dksd|krdS|d}|jj}dd|D}fdd|D}|jf|S)NZ hostPrefixcSs g|]\}}|jdr|qS)Z hostLabel)r9r:).0memberr;r r r sz2Serializer._expand_host_prefix..csi|]}||qSr r )rFr8)rr r sz2Serializer._expand_host_prefix..)Zendpoint input_shapemembersitemsformat)rrrZoperation_endpointZhost_prefix_expressionZ input_membersZ host_labelsZ format_kwargsr )rr_expand_host_prefixs zSerializer._expand_host_prefix)N)__name__ __module__ __qualname__rdictMAP_TYPEr@rr!r(r-r1r7r=rErNr r r rrHs& rc@speZdZdZddZdddZdddZdd d Zdd d Zdd dZ dddZ dddZ dddZ ddZ dS) QuerySerializerr cCs|j}|}|jd|j|d<ddi|d<|}|j|d<|jd|d<|dk rd||||||d<| ||}|dk r||d <|S) Nrz Content-Typez0application/x-www-form-urlencoded; charset=utf-8rZActionZ apiVersionVersionr host_prefix) rJr!httpr:rrSr8metadata _serializerN)rrrr;r body_paramsrVr r rrs   z$QuerySerializer.serialize_to_requestrcCs*t|d|j|j}|||||ddS)NZ_serialize_type_)prefix)r4 type_name_default_serialize)rr r&r;r[rr r rrYs  zQuerySerializer._serializec CsV|j}xJ|D]>\}}||}|||}|r>|d|}|||||qWdS)N.)rKrLr=rY) rr r&r;r[rKkey member_shapeZ member_prefixr r r_serialize_type_structures z)QuerySerializer._serialize_type_structurec Cs|sd||<dS||r\|}|jjdrz|j|jdd}d|ddd|g}n|jjdd}|d|}x:t|dD],\}} |d|} |j} ||| | | qWdS)Nrr8)r<r^rG) _is_shape_flattenedrGr9r:r=joinsplit enumeraterY) rr r&r;r[Z list_prefixr8Z list_nameielementelement_prefix element_shaper r r_serialize_type_lists  z$QuerySerializer._serialize_type_listcCs||r|}nd|}|d}|j}|j}|j|dd} ||d} xVt|dD]H\} } |j| | d} |j| | d}||| || |||| ||qRWdS)Nz%s.entryz .{i}.{suffix}r_)r<r&rc)rhsuffix)rdr_r&r=rgrMrY)rr r&r;r[Z full_prefixtemplateZ key_shapeZ value_shapeZ key_suffixZ value_suffixrhr_Z key_prefixZ value_prefixr r r_serialize_type_maps  z#QuerySerializer._serialize_type_mapcCs||||<dS)N)rE)rr r&r;r[r r r_serialize_type_blobsz$QuerySerializer._serialize_type_blobcCs|||jd||<dS)NtimestampFormat)r7r9r:)rr r&r;r[r r r_serialize_type_timestampsz)QuerySerializer._serialize_type_timestampcCs|rd||<nd||<dS)Ntruefalser )rr r&r;r[r r r_serialize_type_booleans z'QuerySerializer._serialize_type_booleancCs |||<dS)Nr )rr r&r;r[r r rr]%sz"QuerySerializer._default_serializecCs |jdS)N flattened)r9r:)rr;r r rrd(sz#QuerySerializer._is_shape_flattenedN)r)r)r)r)r)r)r)r)rOrPrQr2rrYrarlrorprrrur]rdr r r rrTs       rTc@s"eZdZdZddZdddZdS) EC2SerializeraUEC2 specific customizations to the query protocol serializers. The EC2 model is almost, but not exactly, similar to the query protocol serializer. This class encapsulates those differences. The model will have be marked with a ``protocol`` of ``ec2``, so you don't need to worry about wiring this class up correctly. cCsHd|jkr|jdSd|jkr@|jd}|d|ddS|SdS)NZ queryNamer8rrc)r9upper)rr;r<r8r r rr=6s     z"EC2Serializer._get_serialized_namerc Cs@x:t|dD],\}}|d|}|j}|||||q WdS)Nrcr^)rgrGrY) rr r&r;r[rhrirjrkr r rrlCsz"EC2Serializer._serialize_type_listN)r)rOrPrQ__doc__r=rlr r r rrw,s rwc@sReZdZdZddZdddZddZd d Zd d Zd dZ ddZ ddZ dS)JSONSerializerZ unixtimestampc Csd|jd|j}|jd}|}|jd|j|d<|d|d|d<|}|j}|dk rp| |||t | |j |d<|||}|dk r||d <|S) Nz{}.{}Z targetPrefixZ jsonVersionrzapplication/x-amz-json-%s)z X-Amz-Targetz Content-TyperrrV)rMrXr8r!rWr:rrSrJrYjsondumpsr?r@rN) rrrtargetZ json_versionr rrJrVr r rrMs$  z#JSONSerializer.serialize_to_requestNcCs&t|d|j|j}|||||dS)Nz_serialize_type_%s)r4r\r])rr r&r;r_rr r rrYgs zJSONSerializer._serializec Csx|jr|||<nd|dk r,|}|||<|}|j}x@|D]4\}}||} d| jkr`| jd}|||| |qt||dddd||<qt||||<qW|jf|S)Nz{(.*?)}+rbz/~)safe)refindallendswithrrM)rZ uri_templateparamsZencoded_paramsZtemplate_paramr r rrs z'BaseRestSerializer._render_uri_templatec Cs|jd}|||r8||d}||}||d<nr|dk rv||}|dk rh|||||d<q||d<n4|dr||d||d<n||r||d<dS)Npayloadrrr)r9r:_has_streaming_payload_encode_payload_serialize_body_params_serialize_empty_body_requires_empty_body) rrrr r;rZpayload_memberZ body_payloadrZr r rrs        z%BaseRestSerializer._serialize_payloadcCsdS)Nrr )rr r rr#sz(BaseRestSerializer._serialize_empty_bodycCsdS)z Some protocols require varied Content-Type headers depending on user input. This allows subclasses to apply this conditionally. Nr )rr r;rr r rr&sz*BaseRestSerializer._serialize_content_typecCsdS)z Some protocols require a specific body to represent an empty payload. This allows subclasses to apply this conditionally. Fr )rr;r r rr.sz'BaseRestSerializer._requires_empty_bodycCs|dk o||jdkS)z5Determine if payload is streaming (a blob or string).N)blobstring)r\)rrrr r rr5sz)BaseRestSerializer._has_streaming_payloadcCst|tr||jS|S)N)r/r>r?r@)rrr r rr<s  z"BaseRestSerializer._encode_payloadcCs4||}|jd}|jd|}|dkr8||d|<n|dkrt|trZ|d|ndt|tr~t|}||d|<n@|jdkr|jd|j } | || } | |d|<n ||d|<np|d kr||} |s| jd krdS| | |} t| |d |<n.|d kr$|} | | |d |n ||d |<dS) Nlocationr8rrrr timestamprqrlistrr) r9r:r/rRupdateboolr>r3r\QUERY_STRING_TIMESTAMP_FORMATr7_convert_header_value_do_serialize_header_map)rrrrrrGrkey_nameZbool_strr'rr;r& header_prefixr r rrAs:         z(BaseRestSerializer._partition_parameterscCs*x$|D]\}}||}|||<q WdS)N)rL)rrrZ user_inputr_valZfull_keyr r rrrsz+BaseRestSerializer._do_serialize_header_mapcCs tddS)Nr)r)rrr;r r rrwsz)BaseRestSerializer._serialize_body_paramscsjdkr.,)r:) separators)r\rr*r+ utctimetupler9r:HEADER_TIMESTAMP_FORMATr7rerrEr{r|)rr;r&r5rr'Zconverted_valuer )rr;rrzs      z(BaseRestSerializer._convert_header_valueN)rOrPrQryrrZKNOWN_LOCATIONSrrrrrrrrrrrrr r r rrs   ; 1rc@s,eZdZddZddZddZddZd S) RestJSONSerializercCsdS)Ns{}r )rr r rrsz(RestJSONSerializer._serialize_empty_bodycCs*x$|jD]\}}d|jkr dSq WdS)zq Serialize an empty JSON object whenever the shape has members not targeting a location. rTF)rKrLr9)rr;rGrr r rrs z'RestJSONSerializer._requires_empty_bodycCsN|jd}|||rdS|ddk}td|d}|rJ|sJd|dd<dS)z?Set Content-Type to application/json for all structured bodies.rNrrz Content-Typerzapplication/json)r9r:rr)rr r;rrZhas_bodyZhas_content_typer r rrs   z*RestJSONSerializer._serialize_content_typecCs(|}||||t||jS)N)rSrYr{r|r?r@)rrr;Zserialized_bodyr r rrsz)RestJSONSerializer._serialize_body_paramsN)rOrPrQrrrrr r r rrs  rc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)RestXMLSerializerr cCs@|jd}td}|||||t|d}tj||jdS)Nr8rr)encoding)r9rZElementrYrtostringr@)rrr;Z root_nameZ pseudo_rootZ real_rootr r rrs    z(RestXMLSerializer._serialize_body_paramscCs&t|d|j|j}|||||dS)Nz_serialize_type_%s)r4r\r])rr;rxmlnoder8rr r rrYs zRestXMLSerializer._serializec Cst||}d|jkrL|jd}d}|dr>|d|d7}|d|j|<xj|D]^\}} |j|} | jd|} | dkrdS| jdr| jd} | |j| <qV|| | || qVWdS)NZ xmlNamespaceZxmlnsr[z:%srr8Z xmlAttribute)r SubElementr9r:attribrLrKrY) rrrr;r8Zstructure_nodeZnamespace_metadataZattribute_namer_r&r` member_nameZxml_attribute_namer r rras"        z+RestXMLSerializer._serialize_type_structurec CsX|j}|jdr|}|}n|jdd}t||}x|D]}|||||qr)rrrr;r8rr r rr]s z$RestXMLSerializer._default_serializeN) rOrPrQr2rrYrarlrorurprrr]r r r rrs  r)Zec2queryr{z rest-jsonzrest-xml)T)ryrAr*r0r{rZ xml.etreerZbotocorerZbotocore.compatrZbotocore.utilsrrrrZDEFAULT_TIMESTAMP_FORMATr$r#rrrTrwrzrrrr r r r r's4    wmYo f