serialize.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Modifications made by Cloudera are:
  4. # Copyright (c) 2016 Cloudera, Inc. All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"). You
  7. # may not use this file except in compliance with the License. A copy of
  8. # the License is located at
  9. #
  10. # http://aws.amazon.com/apache2.0/
  11. #
  12. # or in the "license" file accompanying this file. This file is
  13. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  14. # ANY KIND, either express or implied. See the License for the specific
  15. # language governing permissions and limitations under the License.
  16. from ccscli import validate
  17. from ccscli.compat import json
  18. from ccscli.compat import OrderedDict
  19. def create_serializer():
  20. serializer = Serializer()
  21. validator = validate.ParamValidator()
  22. return validate.ParamValidationDecorator(validator, serializer)
  23. class Serializer(object):
  24. DEFAULT_ENCODING = 'utf-8'
  25. def serialize_to_request(self, parameters, operation_model):
  26. # Don't serialize any parameter with a None value.
  27. filtered_parameters = OrderedDict(
  28. (k, v) for k, v in parameters.items() if v is not None)
  29. serialized = {}
  30. serialized['method'] = operation_model.http['method']
  31. serialized['headers'] = {'Content-Type': 'application/json'}
  32. serialized['url_path'] = operation_model.http['requestUri']
  33. serialized_body = OrderedDict()
  34. if len(filtered_parameters) != 0:
  35. self._serialize(serialized_body,
  36. filtered_parameters,
  37. operation_model.input_shape)
  38. serialized['body'] = json.dumps(serialized_body).encode(self.DEFAULT_ENCODING)
  39. return serialized
  40. def _serialize(self, serialized, value, shape, key=None):
  41. serialize_method_name = '_serialize_type_%s' % shape.type_name
  42. method = getattr(self, serialize_method_name, self._default_serialize)
  43. method(serialized, value, shape, key)
  44. def _serialize_type_object(self, serialized, value, shape, key):
  45. if key is not None:
  46. # If a key is provided, this is a result of a recursive call, so we
  47. # need to add a new child dict as the value of the passed in dict.
  48. # Below we will add all the structure members to the new serialized
  49. # dictionary we just created.
  50. serialized[key] = OrderedDict()
  51. serialized = serialized[key]
  52. for member_key, member_value in value.items():
  53. member_shape = shape.members[member_key]
  54. self._serialize(serialized, member_value, member_shape, member_key)
  55. def _serialize_type_array(self, serialized, value, shape, key):
  56. array_obj = []
  57. serialized[key] = array_obj
  58. for array_item in value:
  59. wrapper = {}
  60. # JSON list serialization is the only case where we aren't setting
  61. # a key on a dict. We handle this by using a __current__ key on a
  62. # wrapper dict to serialize each list item before appending it to
  63. # the serialized list.
  64. self._serialize(wrapper, array_item, shape.member, "__current__")
  65. array_obj.append(wrapper["__current__"])
  66. def _default_serialize(self, serialized, value, shape, key):
  67. serialized[key] = value