Revision 52c4fcef

b/snf-astakos-client/astakosclient/__init__.py
111 111
    if status < 200 or status >= 300:
112 112
        raise Exception(status, data)
113 113
    return simplejson.loads(unicode(data))
114

  
115

  
116
# ----------------------------
117
# A simple retry decorator
118
def retry(howmany):
119
    def decorator(func):
120
        def f(*args, **kwargs):
121
            attemps = 0
122
            while True:
123
                try:
124
                    return func(*args, **kwargs)
125
                except Exception as e:
126
                    is_last_attempt = attemps == howmany - 1
127
                    if is_last_attempt:
128
                        raise e
129
                    if e.args:
130
                        status = e[0]
131
                        # In case of Unauthorized response
132
                        # or Not Found return immediately
133
                        if status == 401 or status == 404:
134
                            raise e
135
                    attemps += 1
136
        return f
137
    return decorator

Also available in: Unified diff