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

Commit ec369d5

Browse files
committed
Moved duplicate code to get_docs function in common util module
- Changed visibility of encoder attribute in document module for use in View's callable
1 parent 9f9fb70 commit ec369d5

File tree

4 files changed

+38
-26
lines changed

4 files changed

+38
-26
lines changed

src/cloudant/_common_util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,31 @@ def codify(code_or_str):
234234
return _Code(code_or_str)
235235
return code_or_str
236236

237+
def get_docs(r_session, url, encoder=None, **params):
238+
"""
239+
Provides a helper for functions that require GET or POST requests
240+
with a JSON, text, or raw response containing documents.
241+
242+
:param r_session: Authentication session from the client
243+
:param str url: URL containing the endpoint
244+
:param JSONEncoder encoder: Custom encoder from the client
245+
246+
:returns: Raw response content from the specified endpoint
247+
"""
248+
keys_list = params.pop('keys', None)
249+
keys = None
250+
if keys_list:
251+
keys = json.dumps({'keys': keys_list}, cls=encoder)
252+
f_params = python_to_couch(params)
253+
254+
resp = None
255+
if keys:
256+
resp = r_session.post(url, params=f_params, data=keys)
257+
else:
258+
resp = r_session.get(url, params=f_params)
259+
resp.raise_for_status()
260+
return resp
261+
237262
# Classes
238263

239264
class _Code(str):

src/cloudant/database.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
SEARCH_INDEX_ARGS,
2828
SPECIAL_INDEX_TYPE,
2929
TEXT_INDEX_TYPE,
30-
python_to_couch
31-
)
30+
get_docs)
3231
from .document import Document
3332
from .design_document import DesignDocument
3433
from .view import View
@@ -363,16 +362,10 @@ def all_docs(self, **kwargs):
363362
:returns: Raw JSON response content from ``_all_docs`` endpoint
364363
365364
"""
366-
all_docs_url = posixpath.join(self.database_url, '_all_docs')
367-
params = python_to_couch(kwargs)
368-
keys_list = params.pop('keys', None)
369-
resp = None
370-
if keys_list:
371-
keys = json.dumps({'keys': keys_list})
372-
resp = self.r_session.post(all_docs_url, params=params, data=keys)
373-
else:
374-
resp = self.r_session.get(all_docs_url, params=params)
375-
resp.raise_for_status()
365+
resp = get_docs(self.r_session,
366+
'/'.join([self.database_url, '_all_docs']),
367+
self.client.encoder,
368+
**kwargs)
376369
return resp.json()
377370

378371
@contextlib.contextmanager

src/cloudant/document.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self, database, document_id=None):
6464
self._document_id = document_id
6565
if self._document_id is not None:
6666
self['_id'] = self._document_id
67-
self._encoder = self._client.encoder
67+
self.encoder = self._client.encoder
6868

6969
@property
7070
def document_url(self):
@@ -112,7 +112,7 @@ def json(self):
112112
113113
:returns: Encoded JSON string containing the document data
114114
"""
115-
return json.dumps(dict(self), cls=self._encoder)
115+
return json.dumps(dict(self), cls=self.encoder)
116116

117117
def create(self):
118118
"""
@@ -132,7 +132,7 @@ def create(self):
132132
resp = self.r_session.post(
133133
self._database.database_url,
134134
headers=headers,
135-
data=json.dumps(doc, cls=self._encoder)
135+
data=json.dumps(doc, cls=self.encoder)
136136
)
137137
resp.raise_for_status()
138138
data = resp.json()

src/cloudant/view.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
"""
1818
import contextlib
1919
import posixpath
20-
import json
2120

2221
from ._2to3 import STRTYPE
23-
from ._common_util import python_to_couch, codify
22+
from ._common_util import codify, get_docs
2423
from .result import Result
2524
from .error import CloudantArgumentError, CloudantException
2625

@@ -225,15 +224,10 @@ def __call__(self, **kwargs):
225224
226225
:returns: View result data in JSON format
227226
"""
228-
params = python_to_couch(kwargs)
229-
keys_list = params.pop('keys', None)
230-
resp = None
231-
if keys_list:
232-
keys = json.dumps({'keys': keys_list})
233-
resp = self._r_session.post(self.url, params=params, data=keys)
234-
else:
235-
resp = self._r_session.get(self.url, params=params)
236-
resp.raise_for_status()
227+
resp = get_docs(self._r_session,
228+
self.url,
229+
self.design_doc.encoder,
230+
**kwargs)
237231
return resp.json()
238232

239233
@contextlib.contextmanager

0 commit comments

Comments
 (0)