Skip to content

Commit 01c71ac

Browse files
committed
Add support for the scroll API in channelfinder. Closes ChannelFinder#30
1 parent e03429f commit 01c71ac

File tree

1 file changed

+89
-25
lines changed

1 file changed

+89
-25
lines changed

channelfinder/ChannelFinderClient.py

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ChannelFinderClient(object):
2727
__channelsResource = '/resources/channels'
2828
__propertiesResource = '/resources/properties'
2929
__tagsResource = '/resources/tags'
30+
__scrollResource = '/resources/scroll'
3031

3132
def __init__(self, BaseURL=None, username=None, password=None):
3233
"""
@@ -240,6 +241,75 @@ def __handleMultipleAddParameters(self, **kwds):
240241
auth=self.__auth).raise_for_status()
241242
else:
242243
raise RuntimeError('Incorrect Usage: unknown keys')
244+
245+
def __generate_find_args(self, **kwds):
246+
"""
247+
Generate find arguments for find and find by scroll functions. See find() docstring for examples.
248+
249+
:param kwds:
250+
"""
251+
if not self.__baseURL:
252+
raise RuntimeError('Connection not created')
253+
if not len(kwds) > 0:
254+
raise RuntimeError('Incorrect usage: at least one parameter must be specified')
255+
args = []
256+
for key in kwds:
257+
if key == 'name':
258+
patterns = kwds[key].split(',')
259+
for eachPattern in patterns:
260+
args.append(('~name', eachPattern.strip()))
261+
elif key == 'tagName':
262+
patterns = kwds[key].split(',')
263+
for eachPattern in patterns:
264+
args.append(('~tag', eachPattern.strip()))
265+
elif key == 'property':
266+
for prop in kwds[key]:
267+
patterns = prop[1].split(',')
268+
for eachPattern in patterns:
269+
args.append((prop[0], eachPattern.strip()))
270+
elif key == 'size':
271+
args.append(('~size', '{0:d}'.format(int(kwds[key]))))
272+
elif key == 'ifrom':
273+
args.append(('~from', '{0:d}'.format(int(kwds[key]))))
274+
else:
275+
raise RuntimeError('unknown find argument ' + key)
276+
277+
return args
278+
279+
def findAllByScroll(self, **kwds):
280+
"""
281+
Use scroll API to fetch complete list of results.
282+
283+
See find() docstring for kwds examples.
284+
285+
:param kwds:
286+
:returns: Result list of channels.
287+
"""
288+
scrollResults = [None]
289+
scrollId = None
290+
results = []
291+
292+
while scrollResults:
293+
scrollResults = self.findByScroll(scrollid=scrollId, **kwds)
294+
scrollId = scrollResults['id']
295+
if scrollId:
296+
results = results + scrollResults['channels']
297+
else:
298+
scrollResults = None
299+
300+
return results
301+
302+
def findByScroll(self, scrollid=None, **kwds):
303+
"""
304+
Use scroll API to fetch a single result set.
305+
306+
See find() docstring for kwds examples.
307+
308+
:param kwds:
309+
:returns: scroll object with a scoll id and list of channels.
310+
"""
311+
args = self.__generate_find_args(**kwds)
312+
return self.scrollByArgs(args, scrollid=scrollid)
243313

244314
def find(self, **kwds):
245315
"""
@@ -293,32 +363,26 @@ def find(self, **kwds):
293363
294364
To query for the existance of a tag or property use findTag and findProperty.
295365
"""
296-
if not self.__baseURL:
297-
raise RuntimeError('Connection not created')
298-
if not len(kwds) > 0:
299-
raise RuntimeError('Incorrect usage: at least one parameter must be specified')
300-
args = []
301-
for key in kwds:
302-
if key == 'name':
303-
patterns = kwds[key].split(',')
304-
for eachPattern in patterns:
305-
args.append(('~name', eachPattern.strip()))
306-
elif key == 'tagName':
307-
patterns = kwds[key].split(',')
308-
for eachPattern in patterns:
309-
args.append(('~tag', eachPattern.strip()))
310-
elif key == 'property':
311-
for prop in kwds[key]:
312-
patterns = prop[1].split(',')
313-
for eachPattern in patterns:
314-
args.append((prop[0], eachPattern.strip()))
315-
elif key == 'size':
316-
args.append(('~size', '{0:d}'.format(int(kwds[key]))))
317-
elif key == 'ifrom':
318-
args.append(('~from', '{0:d}'.format(int(kwds[key]))))
319-
else:
320-
raise RuntimeError('unknown find argument ' + key)
366+
args = self.__generate_find_args(**kwds)
321367
return self.findByArgs(args)
368+
369+
def scrollByArgs(self, args, scrollid=None):
370+
url = self.__baseURL + self.__scrollResource
371+
if scrollid:
372+
url = "%s/%s" % (url, scrollid)
373+
r = self.__session.get(url,
374+
params=args,
375+
headers=copy(self.__jsonheader),
376+
verify=False,
377+
auth=self.__auth)
378+
try:
379+
r.raise_for_status()
380+
return r.json()
381+
except HTTPError:
382+
if r.status_code == 404:
383+
return None
384+
else:
385+
r.raise_for_status()
322386

323387
def findByArgs(self, args):
324388
url = self.__baseURL + self.__channelsResource

0 commit comments

Comments
 (0)