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 10
import copy
15

16 10
from botocore.utils import merge_dicts
17

18

19 10
def build_retry_config(endpoint_prefix, retry_model, definitions,
20
                       client_retry_config=None):
21 10
    service_config = retry_model.get(endpoint_prefix, {})
22 10
    resolve_references(service_config, definitions)
23
    # We want to merge the global defaults with the service specific
24
    # defaults, with the service specific defaults taking precedence.
25
    # So we use the global defaults as the base.
26
    #
27
    # A deepcopy is done on the retry defaults because it ensures the
28
    # retry model has no chance of getting mutated when the service specific
29
    # configuration or client retry config is merged in.
30 10
    final_retry_config = {
31
        '__default__': copy.deepcopy(retry_model.get('__default__', {}))
32
    }
33 10
    resolve_references(final_retry_config, definitions)
34
    # The merge the service specific config on top.
35 10
    merge_dicts(final_retry_config, service_config)
36 10
    if client_retry_config is not None:
37 10
        _merge_client_retry_config(final_retry_config, client_retry_config)
38 10
    return final_retry_config
39

40

41 10
def _merge_client_retry_config(retry_config, client_retry_config):
42 10
    max_retry_attempts_override = client_retry_config.get('max_attempts')
43 10
    if max_retry_attempts_override is not None:
44
        # In the retry config, the max_attempts refers to the maximum number
45
        # of requests in general will be made. However, for the client's
46
        # retry config it refers to how many retry attempts will be made at
47
        # most. So to translate this number from the client config, one is
48
        # added to convert it to the maximum number request that will be made
49
        # by including the initial request.
50
        #
51
        # It is also important to note that if we ever support per operation
52
        # configuration in the retry model via the client, we will need to
53
        # revisit this logic to make sure max_attempts gets applied
54
        # per operation.
55 10
        retry_config['__default__'][
56
            'max_attempts'] = max_retry_attempts_override + 1
57

58

59 10
def resolve_references(config, definitions):
60
    """Recursively replace $ref keys.
61

62
    To cut down on duplication, common definitions can be declared
63
    (and passed in via the ``definitions`` attribute) and then
64
    references as {"$ref": "name"}, when this happens the reference
65
    dict is placed with the value from the ``definition`` dict.
66

67
    This is recursively done.
68

69
    """
70 10
    for key, value in config.items():
71 10
        if isinstance(value, dict):
72 10
            if len(value) == 1 and list(value.keys())[0] == '$ref':
73
                # Then we need to resolve this reference.
74 10
                config[key] = definitions[list(value.values())[0]]
75
            else:
76 10
                resolve_references(value, definitions)

Read our documentation on viewing source code .

Loading