Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / astakos.py @ 03493855

History | View | Annotate | Download (2.9 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 409371de Stavros Sachtouris
        """Get authentication information and store it in this client
47 409371de Stavros Sachtouris
        As long as the AstakosClient instance is alive, the latest
48 409371de Stavros Sachtouris
        authentication information for this token will be available
49 409371de Stavros Sachtouris

50 0d79eb23 Stavros Sachtouris
        :param token: (str) custom token to authenticate
51 0d79eb23 Stavros Sachtouris

52 0d79eb23 Stavros Sachtouris
        :returns: (dict) authentication information
53 0d79eb23 Stavros Sachtouris
        """
54 3f61753d Stavros Sachtouris
        self.token = token or self.token
55 409371de Stavros Sachtouris
        self._cache[self.token] = self.get('/im/authenticate').json
56 409371de Stavros Sachtouris
        return self._cache[self.token]
57 5207c784 Stavros Sachtouris
58 5207c784 Stavros Sachtouris
    def list(self):
59 29040fef Stavros Sachtouris
        """list cached user information"""
60 2182231b Stavros Sachtouris
        r = []
61 2182231b Stavros Sachtouris
        for k, v in self._cache.items():
62 2182231b Stavros Sachtouris
            r.append(dict(v))
63 2182231b Stavros Sachtouris
            r[-1].update(dict(auth_token=k))
64 2182231b Stavros Sachtouris
        return r
65 76d3b2d7 Stavros Sachtouris
66 2182231b Stavros Sachtouris
    def info(self, token=None):
67 2182231b Stavros Sachtouris
        """Get (cached) user information"""
68 409371de Stavros Sachtouris
        token_bu = self.token
69 3f61753d Stavros Sachtouris
        token = token or self.token
70 3f61753d Stavros Sachtouris
        try:
71 409371de Stavros Sachtouris
            r = self._cache[token]
72 3f61753d Stavros Sachtouris
        except KeyError:
73 409371de Stavros Sachtouris
            r = self.authenticate(token)
74 409371de Stavros Sachtouris
        self.token = token_bu
75 409371de Stavros Sachtouris
        return r
76 76d3b2d7 Stavros Sachtouris
77 5207c784 Stavros Sachtouris
    def term(self, key, token=None):
78 5207c784 Stavros Sachtouris
        """Get (cached) term, from user credentials"""
79 7ae842c2 Stavros Sachtouris
        return self.info(token).get(key, None)