translate.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) 2012-2013 Mitch Garnaat http://garnaat.org/
  2. # Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"). You
  5. # may not use this file except in compliance with the License. A copy of
  6. # the License is located at
  7. #
  8. # http://aws.amazon.com/apache2.0/
  9. #
  10. # or in the "license" file accompanying this file. This file is
  11. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. # ANY KIND, either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the License.
  14. import copy
  15. from botocore.utils import merge_dicts
  16. def build_retry_config(endpoint_prefix, retry_model, definitions,
  17. client_retry_config=None):
  18. service_config = retry_model.get(endpoint_prefix, {})
  19. resolve_references(service_config, definitions)
  20. # We want to merge the global defaults with the service specific
  21. # defaults, with the service specific defaults taking precedence.
  22. # So we use the global defaults as the base.
  23. #
  24. # A deepcopy is done on the retry defaults because it ensures the
  25. # retry model has no chance of getting mutated when the service specific
  26. # configuration or client retry config is merged in.
  27. final_retry_config = {
  28. '__default__': copy.deepcopy(retry_model.get('__default__', {}))
  29. }
  30. resolve_references(final_retry_config, definitions)
  31. # The merge the service specific config on top.
  32. merge_dicts(final_retry_config, service_config)
  33. if client_retry_config is not None:
  34. _merge_client_retry_config(final_retry_config, client_retry_config)
  35. return final_retry_config
  36. def _merge_client_retry_config(retry_config, client_retry_config):
  37. max_retry_attempts_override = client_retry_config.get('max_attempts')
  38. if max_retry_attempts_override is not None:
  39. # In the retry config, the max_attempts refers to the maximum number
  40. # of requests in general will be made. However, for the client's
  41. # retry config it refers to how many retry attempts will be made at
  42. # most. So to translate this number from the client config, one is
  43. # added to convert it to the maximum number request that will be made
  44. # by including the initial request.
  45. #
  46. # It is also important to note that if we ever support per operation
  47. # configuration in the retry model via the client, we will need to
  48. # revisit this logic to make sure max_attempts gets applied
  49. # per operation.
  50. retry_config['__default__'][
  51. 'max_attempts'] = max_retry_attempts_override + 1
  52. def resolve_references(config, definitions):
  53. """Recursively replace $ref keys.
  54. To cut down on duplication, common definitions can be declared
  55. (and passed in via the ``definitions`` attribute) and then
  56. references as {"$ref": "name"}, when this happens the reference
  57. dict is placed with the value from the ``definition`` dict.
  58. This is recursively done.
  59. """
  60. for key, value in config.items():
  61. if isinstance(value, dict):
  62. if len(value) == 1 and list(value.keys())[0] == '$ref':
  63. # Then we need to resolve this reference.
  64. config[key] = definitions[list(value.values())[0]]
  65. else:
  66. resolve_references(value, definitions)