cloudant-labs / cloudant-python

Asynchronous Cloudant / CouchDB interface for Python
http://cloudant-labs.github.io/cloudant-python/
37 stars 17 forks source link

post to view breaks with multi-key fetches + parameters #45

Closed mgmarino closed 10 years ago

mgmarino commented 10 years ago

It is possible to do a multi-key fetch to a view using post, e.g.:

lthe_view = db.design("log").view("log")

# Grab all the data
keys=[ "key_1", "key_2", ... ] # my list of keys
r = lthe_view.post(data=json.dumps(dict(keys=keys))).json()

In principle, post to view can also take query parameters (e.g. reduce=false, etc.), but trying to call this as expected:

r = lthe_view.post(params=dict(reduce=False), data=json.dumps(dict(keys=keys))).json()

results in them not being used. This is because Resource._make_request appends the params to data so that they appear in the posted data (and not as query parameters), but these are not recognized by couchdb and so are ignored. (This may or may not be a bug in couchdb.)

A work around is:

r = lthe_view.post("?reduce=false", data=json.dumps(dict(keys=keys))).json()

but I think this is not ideal.

I'm not sure the best solution to this problem, but I believe it may be to remove the special handling of 'post' and 'put' in _make_request. That is, this would allow for query params to be sent in addition to data instead of lumping them all together. (Is there a part of the CouchDB API that requires this? I would suggest it should be up to the user to specify what gets submitted as a parameter and what in the body.)

mgmarino commented 10 years ago

I have opted for a slightly less intrusive fix to this issue, namely simply to not overwrite 'data' if both 'data' and 'params' are passed in on a post/put method. This solution doesn't break previous functionality, but still allows querying views using the post-to-view method.

mgmarino commented 10 years ago

Already merged.