serialize.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. # Copyright 2014 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. """Protocol input serializes.
  14. This module contains classes that implement input serialization
  15. for the various KSYUN protocol types.
  16. These classes essentially take user input, a model object that
  17. represents what the expected input should look like, and it returns
  18. a dictionary that contains the various parts of a request. A few
  19. high level design decisions:
  20. * Each protocol type maps to a separate class, all inherit from
  21. ``Serializer``.
  22. * The return value for ``serialize_to_request`` (the main entry
  23. point) returns a dictionary that represents a request. This
  24. will have keys like ``url_path``, ``query_string``, etc. This
  25. is done so that it's a) easy to test and b) not tied to a
  26. particular HTTP library. See the ``serialize_to_request`` docstring
  27. for more details.
  28. Unicode
  29. -------
  30. The input to the serializers should be text (str/unicode), not bytes,
  31. with the exception of blob types. Those are assumed to be binary,
  32. and if a str/unicode type is passed in, it will be encoded as utf-8.
  33. """
  34. import re
  35. import base64
  36. from xml.etree import ElementTree
  37. import calendar
  38. from kscore.compat import six
  39. from kscore.compat import json, formatdate
  40. from kscore.utils import parse_to_aware_datetime
  41. from kscore.utils import percent_encode
  42. from kscore import validate
  43. # From the spec, the default timestamp format if not specified is iso8601.
  44. DEFAULT_TIMESTAMP_FORMAT = 'iso8601'
  45. ISO8601 = '%Y-%m-%dT%H:%M:%SZ'
  46. # Same as ISO8601, but with microsecond precision.
  47. ISO8601_MICRO = '%Y-%m-%dT%H:%M:%S.%fZ'
  48. def create_serializer(protocol_name, include_validation=True):
  49. # TODO: Unknown protocols.
  50. serializer = SERIALIZERS[protocol_name]()
  51. if include_validation:
  52. validator = validate.ParamValidator()
  53. serializer = validate.ParamValidationDecorator(validator, serializer)
  54. return serializer
  55. class Serializer(object):
  56. DEFAULT_METHOD = 'POST'
  57. # Clients can change this to a different MutableMapping
  58. # (i.e OrderedDict) if they want. This is used in the
  59. # compliance test to match the hash ordering used in the
  60. # tests.
  61. MAP_TYPE = dict
  62. DEFAULT_ENCODING = 'utf-8'
  63. def serialize_to_request(self, parameters, operation_model):
  64. """Serialize parameters into an HTTP request.
  65. This method takes user provided parameters and a shape
  66. model and serializes the parameters to an HTTP request.
  67. More specifically, this method returns information about
  68. parts of the HTTP request, it does not enforce a particular
  69. interface or standard for an HTTP request. It instead returns
  70. a dictionary of:
  71. * 'url_path'
  72. * 'query_string'
  73. * 'headers'
  74. * 'body'
  75. * 'method'
  76. It is then up to consumers to decide how to map this to a Request
  77. object of their HTTP library of choice. Below is an example
  78. return value::
  79. {'body': {'Action': 'OperationName',
  80. 'Bar': 'val2',
  81. 'Foo': 'val1',
  82. 'Version': '2014-01-01'},
  83. 'headers': {},
  84. 'method': 'POST',
  85. 'query_string': '',
  86. 'url_path': '/'}
  87. :param parameters: The dictionary input parameters for the
  88. operation (i.e the user input).
  89. :param operation_model: The OperationModel object that describes
  90. the operation.
  91. """
  92. raise NotImplementedError("serialize_to_request")
  93. def _create_default_request(self):
  94. # Creates a boilerplate default request dict that subclasses
  95. # can use as a starting point.
  96. serialized = {
  97. 'url_path': '/',
  98. 'query_string': '',
  99. 'method': self.DEFAULT_METHOD,
  100. 'headers': self.headers,
  101. # An empty body is represented as an empty byte string.
  102. 'body': b''
  103. }
  104. return serialized
  105. def _serialize_not_shape(self, data, parameters):
  106. pass
  107. def _serialize_data(self, serialized, data):
  108. serialized['body'] = data
  109. return serialized
  110. @property
  111. def headers(self):
  112. return {}
  113. # Some extra utility methods subclasses can use.
  114. def _timestamp_iso8601(self, value):
  115. if value.microsecond > 0:
  116. timestamp_format = ISO8601_MICRO
  117. else:
  118. timestamp_format = ISO8601
  119. return value.strftime(timestamp_format)
  120. def _timestamp_unixtimestamp(self, value):
  121. return int(calendar.timegm(value.timetuple()))
  122. def _timestamp_rfc822(self, value):
  123. return formatdate(value, usegmt=True)
  124. def _convert_timestamp_to_str(self, value):
  125. datetime_obj = parse_to_aware_datetime(value)
  126. converter = getattr(
  127. self, '_timestamp_%s' % self.TIMESTAMP_FORMAT.lower())
  128. final_value = converter(datetime_obj)
  129. return final_value
  130. def _get_serialized_name(self, shape, default_name):
  131. # Returns the serialized name for the shape if it exists.
  132. # Otherwise it will return the passed in default_name.
  133. return shape.serialization.get('name', default_name)
  134. def _get_base64(self, value):
  135. # Returns the base64-encoded version of value, handling
  136. # both strings and bytes. The returned value is a string
  137. # via the default encoding.
  138. if isinstance(value, six.text_type):
  139. value = value.encode(self.DEFAULT_ENCODING)
  140. return base64.b64encode(value).strip().decode(
  141. self.DEFAULT_ENCODING)
  142. class QuerySerializer(Serializer):
  143. """
  144. BASE HTTP QUERY REQUEST
  145. """
  146. TIMESTAMP_FORMAT = 'iso8601'
  147. def serialize_to_request(self, parameters, operation_model):
  148. shape = operation_model.input_shape
  149. serialized = self._create_default_request()
  150. serialized['method'] = operation_model.http.get('method',
  151. self.DEFAULT_METHOD)
  152. # The query serializer only deals with body params so
  153. # that's what we hand off the _serialize_* methods.
  154. serialized['headers'].update(
  155. {
  156. 'X-Action': operation_model.name,
  157. 'X-Version': operation_model.metadata['apiVersion'],
  158. }
  159. )
  160. if 'requestUri' in operation_model.http:
  161. serialized['url_path'] = operation_model.http['requestUri']
  162. body_params = self.MAP_TYPE()
  163. body_params['Action'] = operation_model.name
  164. body_params['Version'] = operation_model.metadata['apiVersion']
  165. if shape is not None:
  166. self._serialize(body_params, parameters, shape)
  167. else:
  168. self._serialize_not_shape(body_params, parameters)
  169. return self._serialize_data(serialized, body_params)
  170. def _serialize_not_shape(self, data, parameters):
  171. pass
  172. def _serialize_data(self, serialized, data):
  173. serialized['body'] = data
  174. return serialized
  175. def _serialize(self, serialized, value, shape, prefix=''):
  176. # serialized: The dict that is incrementally added to with the
  177. # final serialized parameters.
  178. # value: The current user input value.
  179. # shape: The shape object that describes the structure of the
  180. # input.
  181. # prefix: The incrementally built up prefix for the serialized
  182. # key (i.e Foo.bar.members.1).
  183. method = getattr(self, '_serialize_type_%s' % shape.type_name,
  184. self._default_serialize)
  185. method(serialized, value, shape, prefix=prefix)
  186. def _serialize_type_structure(self, serialized, value, shape, prefix=''):
  187. members = shape.members
  188. for key, value in value.items():
  189. member_shape = members[key]
  190. member_prefix = self._get_serialized_name(member_shape, key)
  191. if prefix:
  192. member_prefix = '%s.%s' % (prefix, member_prefix)
  193. self._serialize(serialized, value, member_shape, member_prefix)
  194. def _serialize_type_list(self, serialized, value, shape, prefix=''):
  195. if not value:
  196. # The query protocol serializes empty lists.
  197. serialized[prefix] = ''
  198. return
  199. if self._is_shape_flattened(shape):
  200. list_prefix = prefix
  201. if shape.member.serialization.get('name'):
  202. name = self._get_serialized_name(shape.member, default_name='')
  203. # Replace '.Original' with '.{name}'.
  204. list_prefix = '.'.join(prefix.split('.')[:-1] + [name])
  205. else:
  206. list_name = shape.member.serialization.get('name', 'member')
  207. list_prefix = '%s.%s' % (prefix, list_name)
  208. for i, element in enumerate(value, 1):
  209. element_prefix = '%s.%s' % (list_prefix, i)
  210. element_shape = shape.member
  211. self._serialize(serialized, element, element_shape, element_prefix)
  212. def _serialize_type_map(self, serialized, value, shape, prefix=''):
  213. if self._is_shape_flattened(shape):
  214. full_prefix = prefix
  215. else:
  216. full_prefix = '%s.entry' % prefix
  217. template = full_prefix + '.{i}.{suffix}'
  218. key_shape = shape.key
  219. value_shape = shape.value
  220. key_suffix = self._get_serialized_name(key_shape, default_name='key')
  221. value_suffix = self._get_serialized_name(value_shape, 'value')
  222. for i, key in enumerate(value, 1):
  223. key_prefix = template.format(i=i, suffix=key_suffix)
  224. value_prefix = template.format(i=i, suffix=value_suffix)
  225. self._serialize(serialized, key, key_shape, key_prefix)
  226. self._serialize(serialized, value[key], value_shape, value_prefix)
  227. def _serialize_type_blob(self, serialized, value, shape, prefix=''):
  228. # Blob args must be base64 encoded.
  229. serialized[prefix] = self._get_base64(value)
  230. def _serialize_type_timestamp(self, serialized, value, shape, prefix=''):
  231. serialized[prefix] = self._convert_timestamp_to_str(value)
  232. def _serialize_type_boolean(self, serialized, value, shape, prefix=''):
  233. if value:
  234. serialized[prefix] = 'true'
  235. else:
  236. serialized[prefix] = 'false'
  237. def _default_serialize(self, serialized, value, shape, prefix=''):
  238. serialized[prefix] = value
  239. def _is_shape_flattened(self, shape):
  240. return shape.serialization.get('flattened')
  241. class EC2Serializer(QuerySerializer):
  242. """EC2 specific customizations to the query protocol serializers.
  243. The EC2 model is almost, but not exactly, similar to the query protocol
  244. serializer. This class encapsulates those differences. The model
  245. will have be marked with a ``protocol`` of ``ec2``, so you don't need
  246. to worry about wiring this class up correctly.
  247. """
  248. def _get_serialized_name(self, shape, default_name):
  249. # Returns the serialized name for the shape if it exists.
  250. # Otherwise it will return the passed in default_name.
  251. if 'queryName' in shape.serialization:
  252. return shape.serialization['queryName']
  253. elif 'name' in shape.serialization:
  254. # A locationName is always capitalized
  255. # on input for the ec2 protocol.
  256. name = shape.serialization['name']
  257. return name[0].upper() + name[1:]
  258. else:
  259. return default_name
  260. def _serialize_type_list(self, serialized, value, shape, prefix=''):
  261. for i, element in enumerate(value, 1):
  262. element_prefix = '%s.%s' % (prefix, i)
  263. element_shape = shape.member
  264. self._serialize(serialized, element, element_shape, element_prefix)
  265. class QueryAcceptJsonSerializer(QuerySerializer):
  266. @property
  267. def headers(self):
  268. return {"Accept": 'application/json'}
  269. def _serialize_not_shape(self, data, parameters):
  270. data.update(parameters)
  271. def _serialize_data(self, serialized, data):
  272. if serialized['method'].lower() == "get":
  273. serialized['body'] = {}
  274. serialized['query_string'] = data
  275. else:
  276. serialized['body'] = data
  277. return serialized
  278. class KCSSerializer(QueryAcceptJsonSerializer):
  279. def _serialize_data(self, serialized, data):
  280. serialized['body'] = {}
  281. serialized['query_string'] = data
  282. return serialized
  283. class CustomBodySerializer(QueryAcceptJsonSerializer):
  284. def serialize_to_request(self, parameters, operation_model):
  285. shape = operation_model.input_shape
  286. serialized = self._create_default_request()
  287. serialized['method'] = operation_model.http.get('method',
  288. self.DEFAULT_METHOD)
  289. # The query serializer only deals with body params so
  290. # that's what we hand off the _serialize_* methods.
  291. serialized['headers'].update(
  292. {
  293. 'X-Action': operation_model.name,
  294. 'X-Version': operation_model.metadata['apiVersion'],
  295. }
  296. )
  297. if 'requestUri' in operation_model.http:
  298. serialized['url_path'] = operation_model.http['requestUri']
  299. body_params = self.MAP_TYPE()
  300. custom_body = None
  301. if 'Body' in parameters:
  302. custom_body = parameters.pop('Body')
  303. if shape is not None:
  304. self._serialize(body_params, parameters, shape)
  305. else:
  306. self._serialize_not_shape(body_params, parameters)
  307. return self._serialize_data(serialized, body_params, custom_body)
  308. def _serialize_data(self, serialized, data, body=None):
  309. if body is not None:
  310. serialized['body'] = json.dumps(body).encode(self.DEFAULT_ENCODING)
  311. serialized['query_string'] = data
  312. return serialized
  313. class JSONSerializer(Serializer):
  314. """
  315. BASE JSON REQUEST all method with json body
  316. """
  317. TIMESTAMP_FORMAT = 'unixtimestamp'
  318. def serialize_to_request(self, parameters, operation_model):
  319. target = '%s.%s' % (operation_model.metadata['targetPrefix'],
  320. operation_model.name)
  321. serialized = self._create_default_request()
  322. serialized['method'] = operation_model.http.get('method',
  323. self.DEFAULT_METHOD)
  324. if 'requestUri' in operation_model.http:
  325. serialized['url_path'] = operation_model.http['requestUri']
  326. serialized['query_string'] = self.MAP_TYPE()
  327. serialized['headers'] = {
  328. 'X-Amz-Target': target,
  329. 'Content-Type': 'application/json',
  330. 'Accept': 'application/json',
  331. 'X-Action': operation_model.name,
  332. 'X-Version': operation_model.metadata['apiVersion']
  333. }
  334. body = self.MAP_TYPE()
  335. input_shape = operation_model.input_shape
  336. if input_shape is not None:
  337. self._serialize(body, parameters, input_shape)
  338. else:
  339. self._serialize_not_shape(body, parameters)
  340. return self._serialize_data(serialized, body)
  341. def _serialize_not_shape(self, data, parameters):
  342. data.update(parameters)
  343. def _serialize_data(self, serialized, data):
  344. serialized['body'] = json.dumps(data).encode(self.DEFAULT_ENCODING)
  345. return serialized
  346. def _serialize(self, serialized, value, shape, key=None):
  347. method = getattr(self, '_serialize_type_%s' % shape.type_name,
  348. self._default_serialize)
  349. method(serialized, value, shape, key)
  350. def _serialize_type_structure(self, serialized, value, shape, key):
  351. if key is not None:
  352. # If a key is provided, this is a result of a recursive
  353. # call so we need to add a new child dict as the value
  354. # of the passed in serialized dict. We'll then add
  355. # all the structure members as key/vals in the new serialized
  356. # dictionary we just created.
  357. new_serialized = self.MAP_TYPE()
  358. serialized[key] = new_serialized
  359. serialized = new_serialized
  360. members = shape.members
  361. for member_key, member_value in value.items():
  362. member_shape = members[member_key]
  363. if 'name' in member_shape.serialization:
  364. member_key = member_shape.serialization['name']
  365. self._serialize(serialized, member_value, member_shape, member_key)
  366. def _serialize_type_map(self, serialized, value, shape, key):
  367. map_obj = self.MAP_TYPE()
  368. serialized[key] = map_obj
  369. for sub_key, sub_value in value.items():
  370. self._serialize(map_obj, sub_value, shape.value, sub_key)
  371. def _serialize_type_list(self, serialized, value, shape, key):
  372. list_obj = []
  373. serialized[key] = list_obj
  374. for list_item in value:
  375. wrapper = {}
  376. # The JSON list serialization is the only case where we aren't
  377. # setting a key on a dict. We handle this by using
  378. # a __current__ key on a wrapper dict to serialize each
  379. # list item before appending it to the serialized list.
  380. self._serialize(wrapper, list_item, shape.member, "__current__")
  381. list_obj.append(wrapper["__current__"])
  382. def _default_serialize(self, serialized, value, shape, key):
  383. serialized[key] = value
  384. def _serialize_type_timestamp(self, serialized, value, shape, key):
  385. serialized[key] = self._convert_timestamp_to_str(value)
  386. def _serialize_type_blob(self, serialized, value, shape, key):
  387. serialized[key] = self._get_base64(value)
  388. class NotGetJsonSerializer(JSONSerializer):
  389. def _serialize_data(self, serialized, data):
  390. if serialized['method'].lower() == "get":
  391. serialized['body'] = {}
  392. serialized['query_string'].update(data)
  393. else:
  394. serialized['body'] = json.dumps(data).encode(self.DEFAULT_ENCODING)
  395. return serialized
  396. class BaseRestSerializer(Serializer):
  397. """Base class for rest protocols.
  398. The only variance between the various rest protocols is the
  399. way that the body is serialized. All other aspects (headers, uri, etc.)
  400. are the same and logic for serializing those aspects lives here.
  401. Subclasses must implement the ``_serialize_body_params`` method.
  402. """
  403. # This is a list of known values for the "location" key in the
  404. # serialization dict. The location key tells us where on the request
  405. # to put the serialized value.
  406. KNOWN_LOCATIONS = ['uri', 'querystring', 'header', 'headers']
  407. def serialize_to_request(self, parameters, operation_model):
  408. serialized = self._create_default_request()
  409. serialized['headers'] = {
  410. 'X-Action': operation_model.name,
  411. 'X-Version': operation_model.metadata['apiVersion']
  412. }
  413. serialized['method'] = operation_model.http.get('method',
  414. self.DEFAULT_METHOD)
  415. shape = operation_model.input_shape
  416. if shape is None:
  417. serialized['url_path'] = operation_model.http['requestUri']
  418. return serialized
  419. shape_members = shape.members
  420. # While the ``serialized`` key holds the final serialized request
  421. # data, we need interim dicts for the various locations of the
  422. # request. We need this for the uri_path_kwargs and the
  423. # query_string_kwargs because they are templated, so we need
  424. # to gather all the needed data for the string template,
  425. # then we render the template. The body_kwargs is needed
  426. # because once we've collected them all, we run them through
  427. # _serialize_body_params, which for rest-json, creates JSON,
  428. # and for rest-xml, will create XML. This is what the
  429. # ``partitioned`` dict below is for.
  430. partitioned = {
  431. 'uri_path_kwargs': self.MAP_TYPE(),
  432. 'query_string_kwargs': self.MAP_TYPE(),
  433. 'body_kwargs': self.MAP_TYPE(),
  434. 'headers': self.MAP_TYPE(),
  435. }
  436. for param_name, param_value in parameters.items():
  437. if param_value is None:
  438. # Don't serialize any parameter with a None value.
  439. continue
  440. self._partition_parameters(partitioned, param_name, param_value,
  441. shape_members)
  442. serialized['url_path'] = self._render_uri_template(
  443. operation_model.http['requestUri'],
  444. partitioned['uri_path_kwargs'])
  445. # Note that we lean on the http implementation to handle the case
  446. # where the requestUri path already has query parameters.
  447. # The bundled http client, requests, already supports this.
  448. serialized['query_string'] = partitioned['query_string_kwargs']
  449. if partitioned['headers']:
  450. serialized['headers'] = partitioned['headers']
  451. self._serialize_payload(partitioned, parameters,
  452. serialized, shape, shape_members)
  453. return serialized
  454. def _render_uri_template(self, uri_template, params):
  455. # We need to handle two cases::
  456. #
  457. # /{Bucket}/foo
  458. # /{Key+}/bar
  459. # A label ending with '+' is greedy. There can only
  460. # be one greedy key.
  461. encoded_params = {}
  462. for template_param in re.findall(r'{(.*?)}', uri_template):
  463. if template_param.endswith('+'):
  464. encoded_params[template_param] = percent_encode(
  465. params[template_param[:-1]], safe='/~')
  466. else:
  467. encoded_params[template_param] = percent_encode(
  468. params[template_param])
  469. return uri_template.format(**encoded_params)
  470. def _serialize_payload(self, partitioned, parameters,
  471. serialized, shape, shape_members):
  472. # partitioned - The user input params partitioned by location.
  473. # parameters - The user input params.
  474. # serialized - The final serialized request dict.
  475. # shape - Describes the expected input shape
  476. # shape_members - The members of the input struct shape
  477. payload_member = shape.serialization.get('payload')
  478. if payload_member is not None and \
  479. shape_members[payload_member].type_name in ['blob', 'string']:
  480. # If it's streaming, then the body is just the
  481. # value of the payload.
  482. body_payload = parameters.get(payload_member, b'')
  483. body_payload = self._encode_payload(body_payload)
  484. serialized['body'] = body_payload
  485. elif payload_member is not None:
  486. # If there's a payload member, we serialized that
  487. # member to they body.
  488. body_params = parameters.get(payload_member)
  489. if body_params is not None:
  490. serialized['body'] = self._serialize_body_params(
  491. body_params,
  492. shape_members[payload_member])
  493. elif partitioned['body_kwargs']:
  494. serialized['body'] = self._serialize_body_params(
  495. partitioned['body_kwargs'], shape)
  496. def _encode_payload(self, body):
  497. if isinstance(body, six.text_type):
  498. return body.encode(self.DEFAULT_ENCODING)
  499. return body
  500. def _partition_parameters(self, partitioned, param_name,
  501. param_value, shape_members):
  502. # This takes the user provided input parameter (``param``)
  503. # and figures out where they go in the request dict.
  504. # Some params are HTTP headers, some are used in the URI, some
  505. # are in the request body. This method deals with this.
  506. member = shape_members[param_name]
  507. location = member.serialization.get('location')
  508. key_name = member.serialization.get('name', param_name)
  509. if location == 'uri':
  510. partitioned['uri_path_kwargs'][key_name] = param_value
  511. elif location == 'querystring':
  512. if isinstance(param_value, dict):
  513. partitioned['query_string_kwargs'].update(param_value)
  514. else:
  515. partitioned['query_string_kwargs'][key_name] = param_value
  516. elif location == 'header':
  517. shape = shape_members[param_name]
  518. value = self._convert_header_value(shape, param_value)
  519. partitioned['headers'][key_name] = str(value)
  520. elif location == 'headers':
  521. # 'headers' is a bit of an oddball. The ``key_name``
  522. # is actually really a prefix for the header names:
  523. header_prefix = key_name
  524. # The value provided by the user is a dict so we'll be
  525. # creating multiple header key/val pairs. The key
  526. # name to use for each header is the header_prefix (``key_name``)
  527. # plus the key provided by the user.
  528. self._do_serialize_header_map(header_prefix,
  529. partitioned['headers'],
  530. param_value)
  531. else:
  532. partitioned['body_kwargs'][param_name] = param_value
  533. def _do_serialize_header_map(self, header_prefix, headers, user_input):
  534. for key, val in user_input.items():
  535. full_key = header_prefix + key
  536. headers[full_key] = val
  537. def _serialize_body_params(self, params, shape):
  538. raise NotImplementedError('_serialize_body_params')
  539. def _convert_header_value(self, shape, value):
  540. if shape.type_name == 'timestamp':
  541. datetime_obj = parse_to_aware_datetime(value)
  542. timestamp = calendar.timegm(datetime_obj.utctimetuple())
  543. return self._timestamp_rfc822(timestamp)
  544. else:
  545. return value
  546. class RestJSONSerializer(BaseRestSerializer, JSONSerializer):
  547. def _serialize_body_params(self, params, shape):
  548. serialized_body = self.MAP_TYPE()
  549. self._serialize(serialized_body, params, shape)
  550. return json.dumps(serialized_body).encode(self.DEFAULT_ENCODING)
  551. class RestXMLSerializer(BaseRestSerializer):
  552. TIMESTAMP_FORMAT = 'iso8601'
  553. def _serialize_body_params(self, params, shape):
  554. root_name = shape.serialization['name']
  555. pseudo_root = ElementTree.Element('')
  556. self._serialize(shape, params, pseudo_root, root_name)
  557. real_root = list(pseudo_root)[0]
  558. return ElementTree.tostring(real_root, encoding=self.DEFAULT_ENCODING)
  559. def _serialize(self, shape, params, xmlnode, name):
  560. method = getattr(self, '_serialize_type_%s' % shape.type_name,
  561. self._default_serialize)
  562. method(xmlnode, params, shape, name)
  563. def _serialize_type_structure(self, xmlnode, params, shape, name):
  564. structure_node = ElementTree.SubElement(xmlnode, name)
  565. if 'xmlNamespace' in shape.serialization:
  566. namespace_metadata = shape.serialization['xmlNamespace']
  567. attribute_name = 'xmlns'
  568. if namespace_metadata.get('prefix'):
  569. attribute_name += ':%s' % namespace_metadata['prefix']
  570. structure_node.attrib[attribute_name] = namespace_metadata['uri']
  571. for key, value in params.items():
  572. member_shape = shape.members[key]
  573. member_name = member_shape.serialization.get('name', key)
  574. # We need to special case member shapes that are marked as an
  575. # xmlAttribute. Rather than serializing into an XML child node,
  576. # we instead serialize the shape to an XML attribute of the
  577. # *current* node.
  578. if value is None:
  579. # Don't serialize any param whose value is None.
  580. return
  581. if member_shape.serialization.get('xmlAttribute'):
  582. # xmlAttributes must have a serialization name.
  583. xml_attribute_name = member_shape.serialization['name']
  584. structure_node.attrib[xml_attribute_name] = value
  585. continue
  586. self._serialize(member_shape, value, structure_node, member_name)
  587. def _serialize_type_list(self, xmlnode, params, shape, name):
  588. member_shape = shape.member
  589. if shape.serialization.get('flattened'):
  590. element_name = name
  591. list_node = xmlnode
  592. else:
  593. element_name = member_shape.serialization.get('name', 'member')
  594. list_node = ElementTree.SubElement(xmlnode, name)
  595. for item in params:
  596. self._serialize(member_shape, item, list_node, element_name)
  597. def _serialize_type_map(self, xmlnode, params, shape, name):
  598. # Given the ``name`` of MyMap, and input of {"key1": "val1"}
  599. # we serialize this as:
  600. # <MyMap>
  601. # <entry>
  602. # <key>key1</key>
  603. # <value>val1</value>
  604. # </entry>
  605. # </MyMap>
  606. node = ElementTree.SubElement(xmlnode, name)
  607. # TODO: handle flattened maps.
  608. for key, value in params.items():
  609. entry_node = ElementTree.SubElement(node, 'entry')
  610. key_name = self._get_serialized_name(shape.key, default_name='key')
  611. val_name = self._get_serialized_name(shape.value,
  612. default_name='value')
  613. self._serialize(shape.key, key, entry_node, key_name)
  614. self._serialize(shape.value, value, entry_node, val_name)
  615. def _serialize_type_boolean(self, xmlnode, params, shape, name):
  616. # For scalar types, the 'params' attr is actually just a scalar
  617. # value representing the data we need to serialize as a boolean.
  618. # It will either be 'true' or 'false'
  619. node = ElementTree.SubElement(xmlnode, name)
  620. if params:
  621. str_value = 'true'
  622. else:
  623. str_value = 'false'
  624. node.text = str_value
  625. def _serialize_type_blob(self, xmlnode, params, shape, name):
  626. node = ElementTree.SubElement(xmlnode, name)
  627. node.text = self._get_base64(params)
  628. def _serialize_type_timestamp(self, xmlnode, params, shape, name):
  629. node = ElementTree.SubElement(xmlnode, name)
  630. node.text = self._convert_timestamp_to_str(params)
  631. def _default_serialize(self, xmlnode, params, shape, name):
  632. node = ElementTree.SubElement(xmlnode, name)
  633. node.text = str(params)
  634. SERIALIZERS = {
  635. 'kcs': KCSSerializer,
  636. 'ec2': EC2Serializer,
  637. 'query': QuerySerializer,
  638. 'query-json': QueryAcceptJsonSerializer,
  639. 'json': JSONSerializer,
  640. 'json2': NotGetJsonSerializer,
  641. 'rest-json': RestJSONSerializer,
  642. 'rest-xml': RestXMLSerializer,
  643. 'custom-body': CustomBodySerializer,
  644. }