root / kamaki / clients / commissioning_client.py @ 8741c407
History | View | Annotate | Download (2.8 kB)
1 | 54069d1b | Stavros Sachtouris | # Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 54069d1b | Stavros Sachtouris | #
|
3 | 54069d1b | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 54069d1b | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 54069d1b | Stavros Sachtouris | # conditions are met:
|
6 | 54069d1b | Stavros Sachtouris | #
|
7 | 54069d1b | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 54069d1b | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 54069d1b | Stavros Sachtouris | # disclaimer.
|
10 | 54069d1b | Stavros Sachtouris | #
|
11 | 54069d1b | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 54069d1b | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 54069d1b | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 54069d1b | Stavros Sachtouris | # provided with the distribution.
|
15 | 54069d1b | Stavros Sachtouris | #
|
16 | 54069d1b | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 54069d1b | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 54069d1b | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 54069d1b | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 54069d1b | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 54069d1b | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 54069d1b | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 54069d1b | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 54069d1b | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 54069d1b | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 54069d1b | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 54069d1b | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 54069d1b | Stavros Sachtouris | #
|
29 | 54069d1b | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 54069d1b | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 54069d1b | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 54069d1b | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 54069d1b | Stavros Sachtouris | |
34 | b4368e33 | Stavros Sachtouris | from kamaki.clients.commissioning import Callpoint, CallError |
35 | b4368e33 | Stavros Sachtouris | from kamaki.clients.commissioning.utils.debug import debug |
36 | b4368e33 | Stavros Sachtouris | from kamaki.clients import Client |
37 | 54069d1b | Stavros Sachtouris | |
38 | 54069d1b | Stavros Sachtouris | from json import loads as json_loads, dumps as json_dumps |
39 | 54069d1b | Stavros Sachtouris | |
40 | b4368e33 | Stavros Sachtouris | |
41 | 54069d1b | Stavros Sachtouris | class CommissioningClient(Callpoint): |
42 | 54069d1b | Stavros Sachtouris | |
43 | b04a1abf | Stavros Sachtouris | def __init__(self, base_url, token, poolsize): |
44 | 54069d1b | Stavros Sachtouris | super(CommissioningClient, self).__init__() |
45 | 54069d1b | Stavros Sachtouris | self._kc = Client(base_url, token)
|
46 | b04a1abf | Stavros Sachtouris | self._kc.http_client.poolsize = poolsize
|
47 | 54069d1b | Stavros Sachtouris | |
48 | 54069d1b | Stavros Sachtouris | def do_make_call(self, api_call, data): |
49 | 54069d1b | Stavros Sachtouris | |
50 | 54069d1b | Stavros Sachtouris | _kc = self._kc
|
51 | b4368e33 | Stavros Sachtouris | |
52 | 54069d1b | Stavros Sachtouris | gettable = ['list', 'get', 'read'] |
53 | 54069d1b | Stavros Sachtouris | method = (_kc.get if any(api_call.startswith(x) for x in gettable) |
54 | 54069d1b | Stavros Sachtouris | else _kc.post)
|
55 | 54069d1b | Stavros Sachtouris | |
56 | 54069d1b | Stavros Sachtouris | path = api_call |
57 | 54069d1b | Stavros Sachtouris | json_data = json_dumps(data) |
58 | 54069d1b | Stavros Sachtouris | debug("%s %s\n%s\n<<<\n", method.func_name, path, json_data)
|
59 | b4368e33 | Stavros Sachtouris | |
60 | b4368e33 | Stavros Sachtouris | resp = method(path, data=json_data, success=(200, 450, 500)) |
61 | 54069d1b | Stavros Sachtouris | debug(">>>\nStatus: %s", resp.status_code)
|
62 | b4368e33 | Stavros Sachtouris | |
63 | 54069d1b | Stavros Sachtouris | body = resp.text |
64 | 54069d1b | Stavros Sachtouris | debug("\n%s\n<<<\n", body[:128] if body else None) |
65 | 54069d1b | Stavros Sachtouris | |
66 | 54069d1b | Stavros Sachtouris | status = int(resp.status_code)
|
67 | 54069d1b | Stavros Sachtouris | if status == 200: |
68 | 54069d1b | Stavros Sachtouris | return json_loads(body)
|
69 | 54069d1b | Stavros Sachtouris | else:
|
70 | 54069d1b | Stavros Sachtouris | try:
|
71 | 54069d1b | Stavros Sachtouris | error = json_loads(body) |
72 | af9d60fe | Stavros Sachtouris | except ValueError: |
73 | 54069d1b | Stavros Sachtouris | exc = CallError(body, call_error='ValueError')
|
74 | 54069d1b | Stavros Sachtouris | else:
|
75 | 54069d1b | Stavros Sachtouris | exc = CallError.from_dict(error) |
76 | 54069d1b | Stavros Sachtouris | raise exc |