Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 3b1ff13

Browse files
authored
Merge pull request #405 from cloudant/402-pass-params-to-custom-changes-filter
Allow query parameters to be passed to custom changes filters
2 parents 195a17d + c74b7d8 commit 3b1ff13

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [NEW] Add custom JSON encoder/decoder option to `Document` constructor.
44
- [NEW] Add new view parameters, `stable` and `update`, as keyword arguments to `get_view_result`.
5+
- [NEW] Allow arbitrary query parameters to be passed to custom changes filters.
56
- [FIXED] Case where an exception was raised after successful retry when using `doc.update_field`.
67
- [FIXED] Removed unnecessary request when retrieving a Result collection that is less than the 'page_size' value
78

src/cloudant/_common_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747

4848
# Argument Types
4949

50+
ANY_ARG = object()
51+
ANY_TYPE = object()
52+
5053
RESULT_ARG_TYPES = {
5154
'descending': (bool,),
5255
'endkey': (int, LONGTYPE, STRTYPE, Sequence,),
@@ -101,6 +104,7 @@
101104
'filter': (STRTYPE,),
102105
'include_docs': (bool,),
103106
'style': (STRTYPE,),
107+
ANY_ARG: ANY_TYPE # pass arbitrary query parameters to a custom filter
104108
}
105109
_CHANGES_ARG_TYPES.update(_DB_UPDATES_ARG_TYPES)
106110

src/cloudant/feed.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright (c) 2015, 2016 IBM. All rights reserved.
2+
# Copyright (c) 2015, 2018 IBM. All rights reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121

2222
from ._2to3 import iteritems_, next_, unicode_, STRTYPE, NONETYPE
2323
from .error import CloudantArgumentError, CloudantFeedException
24-
from ._common_util import feed_arg_types, TYPE_CONVERTERS
24+
from ._common_util import ANY_ARG, ANY_TYPE, feed_arg_types, TYPE_CONVERTERS
2525

2626
class Feed(object):
2727
"""
@@ -100,7 +100,7 @@ def _translate(self, options):
100100
if isinstance(val, STRTYPE):
101101
translation[key] = val
102102
elif not isinstance(val, NONETYPE):
103-
arg_converter = TYPE_CONVERTERS.get(type(val))
103+
arg_converter = TYPE_CONVERTERS.get(type(val), json.dumps)
104104
translation[key] = arg_converter(val)
105105
except Exception as ex:
106106
raise CloudantArgumentError(115, key, ex)
@@ -111,11 +111,18 @@ def _validate(self, key, val, arg_types):
111111
Ensures that the key and the value are valid arguments to be used with
112112
the feed.
113113
"""
114-
if key not in arg_types:
115-
raise CloudantArgumentError(116, key)
116-
if (not isinstance(val, arg_types[key]) or
117-
(isinstance(val, bool) and int in arg_types[key])):
118-
raise CloudantArgumentError(117, key, arg_types[key])
114+
if key in arg_types:
115+
arg_type = arg_types[key]
116+
else:
117+
if ANY_ARG not in arg_types:
118+
raise CloudantArgumentError(116, key)
119+
arg_type = arg_types[ANY_ARG]
120+
121+
if arg_type == ANY_TYPE:
122+
return
123+
if (not isinstance(val, arg_type) or
124+
(isinstance(val, bool) and int in arg_type)):
125+
raise CloudantArgumentError(117, key, arg_type)
119126
if isinstance(val, int) and val < 0 and not isinstance(val, bool):
120127
raise CloudantArgumentError(118, key, val)
121128
if key == 'feed':

tests/unit/changes_tests.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,20 @@ def test_get_feed_using_doc_ids(self):
465465
self.assertSetEqual(set([x['id'] for x in changes]), expected)
466466
self.assertTrue(str(feed.last_seq).startswith('100'))
467467

468-
def test_invalid_argument(self):
468+
def test_get_feed_with_custom_filter_query_params(self):
469469
"""
470-
Test that an invalid argument is caught and an exception is raised
470+
Test using feed with custom filter query parameters.
471471
"""
472-
feed = Feed(self.db, foo='bar')
473-
with self.assertRaises(CloudantArgumentError) as cm:
474-
invalid_feed = [x for x in feed]
475-
self.assertEqual(str(cm.exception), 'Invalid argument foo')
472+
feed = Feed(
473+
self.db,
474+
filter='mailbox/new_mail',
475+
foo='bar', # query parameters to a custom filter
476+
include_docs=False
477+
)
478+
params = feed._translate(feed._options)
479+
self.assertEquals(params['filter'], 'mailbox/new_mail')
480+
self.assertEquals(params['foo'], 'bar')
481+
self.assertEquals(params['include_docs'], 'false')
476482

477483
def test_invalid_argument_type(self):
478484
"""

0 commit comments

Comments
 (0)