diff options
author | Mikhail Lelyakin <mlelyakin@mirantis.com> | 2016-07-08 16:48:06 +0300 |
---|---|---|
committer | Mikhail Lelyakin <mlelyakin@mirantis.com> | 2016-07-28 13:03:23 +0300 |
commit | 75c8db6d23f383f40b7b4ac3df28f683bcaaf68c (patch) | |
tree | 8ad9ea28e4fdbd73271566781686f086356114fd /saharaclient/api/base.py | |
parent | 5cf912636b3e55facb924f8ecb8ec871d1be557a (diff) | |
download | python-saharaclient-75c8db6d23f383f40b7b4ac3df28f683bcaaf68c.tar.gz |
Add pagination ability to Python-saharaclient
Now we are working on pagination ability in Sahara API.
This changes support work with new abilities.
Change-Id: Idf96a82b0f49bd288eb3fd9bfa82f92074ea4e7c
bp: pagination
Depends-on: I50fa225361617e835c314e873035704b9fe811d0
Diffstat (limited to 'saharaclient/api/base.py')
-rw-r--r-- | saharaclient/api/base.py | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/saharaclient/api/base.py b/saharaclient/api/base.py index a5323cd..aae3211 100644 --- a/saharaclient/api/base.py +++ b/saharaclient/api/base.py @@ -178,6 +178,26 @@ class ResourceManager(object): else: self._raise_api_exception(resp) + def _page(self, url, response_key, limit=None): + resp = self.api.get(url) + if resp.status_code == 200: + result = get_json(resp) + data = result[response_key] + meta = result.get('markers') + + next, prev = None, None + + if meta: + prev = meta.get('prev') + next = meta.get('next') + + l = [self.resource_class(self, res) + for res in data] + + return Page(l, prev, next, limit) + else: + self._raise_api_exception(resp) + def _get(self, url, response_key=None): resp = self.api.get(url) @@ -231,10 +251,25 @@ class APIException(Exception): self.error_message = error_message -def get_query_string(search_opts): - if search_opts: - qparams = sorted(search_opts.items(), key=lambda x: x[0]) +def get_query_string(search_opts, limit=None, marker=None): + opts = {} + if marker is not None: + opts['marker'] = marker + if limit is not None: + opts['limit'] = limit + if search_opts is not None: + opts.update(search_opts) + if opts: + qparams = sorted(opts.items(), key=lambda x: x[0]) query_string = "?%s" % parse.urlencode(qparams, doseq=True) else: query_string = "" return query_string + + +class Page(list): + def __init__(self, l, prev, next, limit): + super(Page, self).__init__(l) + self.prev = prev + self.next = next + self.limit = limit |