Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-client / astakosclient / utils.py @ f6ff4b40

History | View | Annotate | Download (3 kB)

1 f93cc364 Ilias Tsitsimpis
# Copyright (C) 2012, 2013 GRNET S.A. All rights reserved.
2 f93cc364 Ilias Tsitsimpis
#
3 f93cc364 Ilias Tsitsimpis
# Redistribution and use in source and binary forms, with or
4 f93cc364 Ilias Tsitsimpis
# without modification, are permitted provided that the following
5 f93cc364 Ilias Tsitsimpis
# conditions are met:
6 f93cc364 Ilias Tsitsimpis
#
7 f93cc364 Ilias Tsitsimpis
#   1. Redistributions of source code must retain the above
8 f93cc364 Ilias Tsitsimpis
#      copyright notice, this list of conditions and the following
9 f93cc364 Ilias Tsitsimpis
#      disclaimer.
10 f93cc364 Ilias Tsitsimpis
#
11 f93cc364 Ilias Tsitsimpis
#   2. Redistributions in binary form must reproduce the above
12 f93cc364 Ilias Tsitsimpis
#      copyright notice, this list of conditions and the following
13 f93cc364 Ilias Tsitsimpis
#      disclaimer in the documentation and/or other materials
14 f93cc364 Ilias Tsitsimpis
#      provided with the distribution.
15 f93cc364 Ilias Tsitsimpis
#
16 f93cc364 Ilias Tsitsimpis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 f93cc364 Ilias Tsitsimpis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 f93cc364 Ilias Tsitsimpis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 f93cc364 Ilias Tsitsimpis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 f93cc364 Ilias Tsitsimpis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 f93cc364 Ilias Tsitsimpis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 f93cc364 Ilias Tsitsimpis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 f93cc364 Ilias Tsitsimpis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 f93cc364 Ilias Tsitsimpis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f93cc364 Ilias Tsitsimpis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 f93cc364 Ilias Tsitsimpis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 f93cc364 Ilias Tsitsimpis
# POSSIBILITY OF SUCH DAMAGE.
28 f93cc364 Ilias Tsitsimpis
#
29 f93cc364 Ilias Tsitsimpis
# The views and conclusions contained in the software and
30 f93cc364 Ilias Tsitsimpis
# documentation are those of the authors and should not be
31 f93cc364 Ilias Tsitsimpis
# interpreted as representing official policies, either expressed
32 f93cc364 Ilias Tsitsimpis
# or implied, of GRNET S.A.
33 f93cc364 Ilias Tsitsimpis
34 6837f014 Ilias Tsitsimpis
from httplib import HTTPConnection, HTTPSConnection
35 6837f014 Ilias Tsitsimpis
from contextlib import closing
36 f93cc364 Ilias Tsitsimpis
37 6837f014 Ilias Tsitsimpis
from objpool.http import PooledHTTPConnection
38 f93cc364 Ilias Tsitsimpis
from astakosclient.errors import AstakosClientException
39 f93cc364 Ilias Tsitsimpis
40 f93cc364 Ilias Tsitsimpis
41 f93cc364 Ilias Tsitsimpis
def retry(func):
42 f93cc364 Ilias Tsitsimpis
    def decorator(self, *args, **kwargs):
43 f93cc364 Ilias Tsitsimpis
        attemps = 0
44 f93cc364 Ilias Tsitsimpis
        while True:
45 f93cc364 Ilias Tsitsimpis
            try:
46 f93cc364 Ilias Tsitsimpis
                return func(self, *args, **kwargs)
47 f93cc364 Ilias Tsitsimpis
            except AstakosClientException as err:
48 f93cc364 Ilias Tsitsimpis
                is_last_attempt = attemps == self.retry
49 f93cc364 Ilias Tsitsimpis
                if is_last_attempt:
50 f93cc364 Ilias Tsitsimpis
                    raise err
51 f93cc364 Ilias Tsitsimpis
                if err.status == 401 or err.status == 404:
52 f93cc364 Ilias Tsitsimpis
                    # In case of Unauthorized response
53 f93cc364 Ilias Tsitsimpis
                    # or Not Found return immediately
54 f93cc364 Ilias Tsitsimpis
                    raise err
55 f93cc364 Ilias Tsitsimpis
                self.logger.info("AstakosClient request failed..retrying")
56 f93cc364 Ilias Tsitsimpis
                attemps += 1
57 f93cc364 Ilias Tsitsimpis
    return decorator
58 f93cc364 Ilias Tsitsimpis
59 f93cc364 Ilias Tsitsimpis
60 f93cc364 Ilias Tsitsimpis
def scheme_to_class(scheme, use_pool, pool_size):
61 f93cc364 Ilias Tsitsimpis
    """Return the appropriate conn class for given scheme"""
62 f93cc364 Ilias Tsitsimpis
    def _objpool(netloc):
63 6837f014 Ilias Tsitsimpis
        return PooledHTTPConnection(
64 f93cc364 Ilias Tsitsimpis
            netloc=netloc, scheme=scheme, pool_size=pool_size)
65 f93cc364 Ilias Tsitsimpis
66 6837f014 Ilias Tsitsimpis
    def _http_connection(netloc):
67 6837f014 Ilias Tsitsimpis
        return closing(HTTPConnection(netloc))
68 6837f014 Ilias Tsitsimpis
69 6837f014 Ilias Tsitsimpis
    def _https_connection(netloc):
70 6837f014 Ilias Tsitsimpis
        return closing(HTTPSConnection(netloc))
71 6837f014 Ilias Tsitsimpis
72 f93cc364 Ilias Tsitsimpis
    if scheme == "http":
73 f93cc364 Ilias Tsitsimpis
        if use_pool:
74 f93cc364 Ilias Tsitsimpis
            return _objpool
75 f93cc364 Ilias Tsitsimpis
        else:
76 6837f014 Ilias Tsitsimpis
            return _http_connection
77 f93cc364 Ilias Tsitsimpis
    elif scheme == "https":
78 f93cc364 Ilias Tsitsimpis
        if use_pool:
79 f93cc364 Ilias Tsitsimpis
            return _objpool
80 f93cc364 Ilias Tsitsimpis
        else:
81 6837f014 Ilias Tsitsimpis
            return _https_connection
82 f93cc364 Ilias Tsitsimpis
    else:
83 f93cc364 Ilias Tsitsimpis
        return None