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