Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / astakos.py @ 5207c784

History | View | Annotate | Download (2.4 kB)

1 43ca98ee Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 43ca98ee Giorgos Verigakis
#
3 43ca98ee Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 43ca98ee Giorgos Verigakis
# without modification, are permitted provided that the following
5 43ca98ee Giorgos Verigakis
# conditions are met:
6 43ca98ee Giorgos Verigakis
#
7 43ca98ee Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 43ca98ee Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 43ca98ee Giorgos Verigakis
#      disclaimer.
10 43ca98ee Giorgos Verigakis
#
11 43ca98ee Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 43ca98ee Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 43ca98ee Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 43ca98ee Giorgos Verigakis
#      provided with the distribution.
15 43ca98ee Giorgos Verigakis
#
16 43ca98ee Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 43ca98ee Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 43ca98ee Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 43ca98ee Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 43ca98ee Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 43ca98ee Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 43ca98ee Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 43ca98ee Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 43ca98ee Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 43ca98ee Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 43ca98ee Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 43ca98ee Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 43ca98ee Giorgos Verigakis
#
29 43ca98ee Giorgos Verigakis
# The views and conclusions contained in the software and
30 43ca98ee Giorgos Verigakis
# documentation are those of the authors and should not be
31 43ca98ee Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 43ca98ee Giorgos Verigakis
# or implied, of GRNET S.A.
33 43ca98ee Giorgos Verigakis
34 c270fe96 Stavros Sachtouris
from kamaki.clients import Client, ClientError
35 43ca98ee Giorgos Verigakis
36 3dabe5d2 Stavros Sachtouris
37 43ca98ee Giorgos Verigakis
class AstakosClient(Client):
38 43ca98ee Giorgos Verigakis
    """GRNet Astakos API client"""
39 44b8928d Giorgos Verigakis
40 3f61753d Stavros Sachtouris
    _cache = {}
41 3f61753d Stavros Sachtouris
42 2f749e6e Stavros Sachtouris
    def __init__(self, base_url, token):
43 33dc6317 Stavros Sachtouris
        super(AstakosClient, self).__init__(base_url, token)
44 2f749e6e Stavros Sachtouris
45 0238c167 Stavros Sachtouris
    def authenticate(self, token=None):
46 0d79eb23 Stavros Sachtouris
        """
47 0d79eb23 Stavros Sachtouris
        :param token: (str) custom token to authenticate
48 0d79eb23 Stavros Sachtouris

49 0d79eb23 Stavros Sachtouris
        :returns: (dict) authentication information
50 0d79eb23 Stavros Sachtouris
        """
51 3f61753d Stavros Sachtouris
        self.token = token or self.token
52 5207c784 Stavros Sachtouris
        self._cache[token] = self.get('/im/authenticate').json
53 5207c784 Stavros Sachtouris
        return self._cache[token]
54 5207c784 Stavros Sachtouris
55 5207c784 Stavros Sachtouris
    def list(self):
56 5207c784 Stavros Sachtouris
        return self._cache.values()
57 76d3b2d7 Stavros Sachtouris
58 3f61753d Stavros Sachtouris
    def _user_info(self, token=None):
59 3f61753d Stavros Sachtouris
        token = token or self.token
60 3f61753d Stavros Sachtouris
        try:
61 3f61753d Stavros Sachtouris
            return self._cache[token]
62 3f61753d Stavros Sachtouris
        except KeyError:
63 3f61753d Stavros Sachtouris
            return self.authenticate(token)
64 76d3b2d7 Stavros Sachtouris
65 5207c784 Stavros Sachtouris
    def term(self, key, token=None):
66 5207c784 Stavros Sachtouris
        """Get (cached) term, from user credentials"""
67 5207c784 Stavros Sachtouris
        return self._user_info(token)[key]