Skip to content

Commit baed0c9

Browse files
authored
Merge pull request #41 from BillFarber/feature/addResponseOption
DEVEXP-591: Return response when reading and searching documents
2 parents f3ecca1 + 6f8cfae commit baed0c9

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

docs/managing-documents/reading.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ print(docs)
114114
Please see [the application developer's guide](https://docs.marklogic.com/guide/rest-dev/documents#id_80116)
115115
for more information on reading documents.
116116

117+
## Returning the original HTTP response
118+
119+
The `client.documents.read` method also accepts a `return_response` argument. When that
120+
argument is set to `True`, the original response is returned. This can be useful for custom
121+
processing of the response or debugging requests.
122+
117123
## Error handling
118124

119125
If the `client.documents.read` method receives an HTTP response with a status code of 200, then the client will return

docs/managing-documents/searching.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ assert len(docs) == 2
155155
Please see [the application developer's guide](https://docs.marklogic.com/guide/rest-dev/search#id_49329)
156156
for more information on searching documents.
157157

158+
## Returning the original HTTP response
159+
160+
The `client.documents.search` method also accepts a `return_response` argument. When
161+
that argument is set to `True`, the original response is returned. This can be useful for
162+
custom processing of the response or debugging requests.
163+
158164
## Error handling
159165

160166
If the `client.documents.read` method receives an HTTP response with a status code of 200, then the client will return

marklogic/documents.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def read(
399399
uris: Union[str, list[str]],
400400
categories: list[str] = None,
401401
tx: Transaction = None,
402+
return_response: bool = False,
402403
**kwargs,
403404
) -> Union[list[Document], Response]:
404405
"""
@@ -428,7 +429,7 @@ def read(
428429

429430
return (
430431
multipart_response_to_documents(response)
431-
if response.status_code == 200
432+
if response.status_code == 200 and not return_response
432433
else response
433434
)
434435

@@ -442,6 +443,7 @@ def search(
442443
options: str = None,
443444
collections: list[str] = None,
444445
tx: Transaction = None,
446+
return_response: bool = False,
445447
**kwargs,
446448
) -> Union[list[Document], Response]:
447449
"""
@@ -512,6 +514,6 @@ def search(
512514

513515
return (
514516
multipart_response_to_documents(response)
515-
if response.status_code == 200
517+
if response.status_code == 200 and not return_response
516518
else response
517519
)

tests/test_read_documents.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ def test_read_with_basic_client(basic_client: Client):
137137
assert {"hello": "world"} == doc.content
138138

139139

140+
def test_read_with_original_response(basic_client: Client):
141+
response = basic_client.documents.read("/doc1.json", return_response=True)
142+
assert b'--ML_BOUNDARY' in response.content
143+
assert b'filename="/doc1.json"' in response.content
144+
assert b'{"hello":"world"}' in response.content
145+
146+
140147
def test_not_rest_user(not_rest_user_client: Client):
141148
response: Response = not_rest_user_client.documents.read(
142149
["/doc1.json", "/doc2.xml"]

tests/test_search_docs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from requests import Response
44

55
from marklogic import Client
6+
from marklogic.documents import multipart_response_to_documents
67

78

89
def test_structured_json_string_query(client: Client):
@@ -73,6 +74,15 @@ def test_search_options(client: Client):
7374
assert len(docs) == 0
7475

7576

77+
def test_search_with_original_response(client: Client):
78+
response = client.documents.search(
79+
q="hello:world", options="test-options", return_response=True
80+
)
81+
docs = multipart_response_to_documents(response)
82+
assert len(docs) == 1
83+
assert docs[0].uri == "/doc2.xml"
84+
85+
7686
def test_collection(client: Client):
7787
docs = client.documents.search(
7888
categories=["content", "collections"], collections=["search-test"]

0 commit comments

Comments
 (0)