root / astakosclient / astakosclient / utils.py @ 26498848
History | View | Annotate | Download (3.6 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 | 19198628 | Ilias Tsitsimpis | import simplejson |
38 | 6837f014 | Ilias Tsitsimpis | from objpool.http import PooledHTTPConnection |
39 | 19198628 | Ilias Tsitsimpis | from astakosclient.errors import AstakosClientException, BadValue |
40 | f93cc364 | Ilias Tsitsimpis | |
41 | f93cc364 | Ilias Tsitsimpis | |
42 | f93cc364 | Ilias Tsitsimpis | def retry(func): |
43 | f93cc364 | Ilias Tsitsimpis | def decorator(self, *args, **kwargs): |
44 | f93cc364 | Ilias Tsitsimpis | attemps = 0
|
45 | f93cc364 | Ilias Tsitsimpis | while True: |
46 | f93cc364 | Ilias Tsitsimpis | try:
|
47 | f93cc364 | Ilias Tsitsimpis | return func(self, *args, **kwargs) |
48 | f93cc364 | Ilias Tsitsimpis | except AstakosClientException as err: |
49 | f93cc364 | Ilias Tsitsimpis | is_last_attempt = attemps == self.retry
|
50 | f93cc364 | Ilias Tsitsimpis | if is_last_attempt:
|
51 | f93cc364 | Ilias Tsitsimpis | raise err
|
52 | f93cc364 | Ilias Tsitsimpis | if err.status == 401 or err.status == 404: |
53 | f93cc364 | Ilias Tsitsimpis | # In case of Unauthorized response
|
54 | f93cc364 | Ilias Tsitsimpis | # or Not Found return immediately
|
55 | f93cc364 | Ilias Tsitsimpis | raise err
|
56 | f93cc364 | Ilias Tsitsimpis | self.logger.info("AstakosClient request failed..retrying") |
57 | f93cc364 | Ilias Tsitsimpis | attemps += 1
|
58 | f93cc364 | Ilias Tsitsimpis | return decorator
|
59 | f93cc364 | Ilias Tsitsimpis | |
60 | f93cc364 | Ilias Tsitsimpis | |
61 | f93cc364 | Ilias Tsitsimpis | def scheme_to_class(scheme, use_pool, pool_size): |
62 | f93cc364 | Ilias Tsitsimpis | """Return the appropriate conn class for given scheme"""
|
63 | f93cc364 | Ilias Tsitsimpis | def _objpool(netloc): |
64 | 6837f014 | Ilias Tsitsimpis | return PooledHTTPConnection(
|
65 | 334dc272 | Ilias Tsitsimpis | netloc=netloc, scheme=scheme, size=pool_size) |
66 | f93cc364 | Ilias Tsitsimpis | |
67 | 6837f014 | Ilias Tsitsimpis | def _http_connection(netloc): |
68 | 6837f014 | Ilias Tsitsimpis | return closing(HTTPConnection(netloc))
|
69 | 6837f014 | Ilias Tsitsimpis | |
70 | 6837f014 | Ilias Tsitsimpis | def _https_connection(netloc): |
71 | 6837f014 | Ilias Tsitsimpis | return closing(HTTPSConnection(netloc))
|
72 | 6837f014 | Ilias Tsitsimpis | |
73 | f93cc364 | Ilias Tsitsimpis | if scheme == "http": |
74 | f93cc364 | Ilias Tsitsimpis | if use_pool:
|
75 | f93cc364 | Ilias Tsitsimpis | return _objpool
|
76 | f93cc364 | Ilias Tsitsimpis | else:
|
77 | 6837f014 | Ilias Tsitsimpis | return _http_connection
|
78 | f93cc364 | Ilias Tsitsimpis | elif scheme == "https": |
79 | f93cc364 | Ilias Tsitsimpis | if use_pool:
|
80 | f93cc364 | Ilias Tsitsimpis | return _objpool
|
81 | f93cc364 | Ilias Tsitsimpis | else:
|
82 | 6837f014 | Ilias Tsitsimpis | return _https_connection
|
83 | f93cc364 | Ilias Tsitsimpis | else:
|
84 | f93cc364 | Ilias Tsitsimpis | return None |
85 | 19198628 | Ilias Tsitsimpis | |
86 | 19198628 | Ilias Tsitsimpis | |
87 | 19198628 | Ilias Tsitsimpis | def parse_request(request, logger): |
88 | 19198628 | Ilias Tsitsimpis | """Parse request with simplejson to convert it to string"""
|
89 | 19198628 | Ilias Tsitsimpis | try:
|
90 | 19198628 | Ilias Tsitsimpis | return simplejson.dumps(request)
|
91 | 19198628 | Ilias Tsitsimpis | except Exception as err: |
92 | 19198628 | Ilias Tsitsimpis | m = "Cannot parse request \"%s\" with simplejson: %s" \
|
93 | 19198628 | Ilias Tsitsimpis | % (request, str(err))
|
94 | 19198628 | Ilias Tsitsimpis | logger.error(m) |
95 | 19198628 | Ilias Tsitsimpis | raise BadValue(m)
|
96 | 10797183 | Ilias Tsitsimpis | |
97 | 10797183 | Ilias Tsitsimpis | |
98 | 10797183 | Ilias Tsitsimpis | def check_input(function_name, logger, **kwargs): |
99 | 10797183 | Ilias Tsitsimpis | """Check if given arguments are not None"""
|
100 | 10797183 | Ilias Tsitsimpis | for i in kwargs: |
101 | 214058a9 | Christos Stavrakakis | if kwargs[i] is None: |
102 | 10797183 | Ilias Tsitsimpis | m = "in " + function_name + ": " + str(i) + " parameter not given" |
103 | 10797183 | Ilias Tsitsimpis | logger.error(m) |
104 | 10797183 | Ilias Tsitsimpis | raise BadValue(m) |