Revision 2c089b77
b/api/handlers.py | ||
---|---|---|
3 | 3 |
# Copyright © 2010 Greek Research and Technology Network |
4 | 4 |
# |
5 | 5 |
|
6 |
from django.conf import settings |
|
6 | 7 |
from piston.handler import BaseHandler, AnonymousBaseHandler |
7 | 8 |
from synnefo.api.faults import fault, noContent, accepted, created |
8 |
from synnefo.api.helpers import instance_to_server |
|
9 |
from synnefo.api.helpers import instance_to_server, paginator
|
|
9 | 10 |
from synnefo.util.rapi import GanetiRapiClient |
10 |
from django.conf import settings |
|
11 | 11 |
|
12 | 12 |
rapi = GanetiRapiClient(*settings.GANETI_CLUSTER_INFO) |
13 | 13 |
|
... | ... | |
52 | 52 |
instance = rapi.GetInstance(id) |
53 | 53 |
return { "server": instance_to_server(instance) } |
54 | 54 |
|
55 |
@paginator |
|
55 | 56 |
def read_all(self, request, detail=False): |
56 | 57 |
if not detail: |
57 | 58 |
instances = rapi.GetInstances(bulk=False) |
b/api/helpers.py | ||
---|---|---|
25 | 25 |
server["status"] = "UNKNOWN" |
26 | 26 |
|
27 | 27 |
return server |
28 |
|
|
29 |
|
|
30 |
def paginator(func): |
|
31 |
""" |
|
32 |
A dummy paginator decorator that uses limit/offset query parameters to |
|
33 |
limit the result set of a view. The view must return a dict with a single |
|
34 |
key and an iterable for its value. |
|
35 |
|
|
36 |
This doesn't actually speed up the internal processing, but it's useful to |
|
37 |
easily provide compatibility for the API |
|
38 |
""" |
|
39 |
def inner_func(self, request, *args, **kwargs): |
|
40 |
resp = func(self, request, *args, **kwargs) |
|
41 |
if 'limit' not in request.GET or 'offset' not in request.GET: |
|
42 |
return resp |
|
43 |
|
|
44 |
# handle structures such as { ' |
|
45 |
if len(resp.keys()) != 1: |
|
46 |
return resp |
|
47 |
key = resp.keys()[0] |
|
48 |
full = resp.values()[0] |
|
49 |
|
|
50 |
try: |
|
51 |
limit = int(request.GET['limit']) |
|
52 |
offset = int(request.GET['offset']) |
|
53 |
if offset < 0: |
|
54 |
raise ValueError |
|
55 |
if limit < 0: |
|
56 |
raise ValueError |
|
57 |
limit = limit + offset |
|
58 |
partial = full[offset:limit] |
|
59 |
return { key: partial } |
|
60 |
except (ValueError, TypeError): |
|
61 |
return { key: [] } |
|
62 |
|
|
63 |
return inner_func |
Also available in: Unified diff