Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/rows.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,3 @@ selected via a `format` argument. The following table defined the possible value
| `xml` | XML document defining the columns and rows. |
| `csv` | CSV text with the first row defining the columns. |
| `json-seq` | A [line-delimited JSON sequence](https://datatracker.ietf.org/doc/html/rfc7464) with the first row defining the columns. |
| `mixed` | TODO Seems like we should remove this as it does the same thing as "return_response". |
19 changes: 12 additions & 7 deletions marklogic/rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class RowManager:

def __init__(self, session: Session):
self._session = session

Expand All @@ -17,15 +16,13 @@ def __init__(self, session: Session):
"xml": "application/xml",
"csv": "text/csv",
"json-seq": "application/json-seq",
"mixed": "application/xml, multipart/mixed",
}

__query_format_switch = {
"json": lambda response: response.json(),
"xml": lambda response: response.text,
"csv": lambda response: response.text,
"json-seq": lambda response: response.text,
"mixed": lambda response: response,
}

def query(
Expand All @@ -37,7 +34,7 @@ def query(
graphql: str = None,
format: str = "json",
return_response: bool = False,
**kwargs
**kwargs,
):
"""
Sends a query to an endpoint at the MarkLogic rows service defined at
Expand All @@ -56,8 +53,9 @@ def query(
:param graphql: a GraphQL query string. This is the query string
only, not the entire query JSON object. See
https://docs.marklogic.com/REST/POST/v1/rows/graphql for more information.
:param format: defines the format of the response. If a GraphQL query is
submitted, this parameter is ignored and a JSON response is always returned.
:param format: defines the format of the response. Valid values are "json",
"xml", "csv", and "json-seq". If a GraphQL query is submitted, this parameter
is ignored and a JSON response is always returned.
:param return_response: boolean specifying if the entire original response
object should be returned (True) or if only the data should be returned (False)
upon a success (2xx) response. Note that if the status code of the response is
Expand All @@ -73,7 +71,14 @@ def query(
request_info = self.__get_request_info(dsl, plan, sql, sparql)
data = request_info["data"]
headers["Content-Type"] = request_info["content-type"]
headers["Accept"] = RowManager.__accept_switch.get(format)
if format:
value = RowManager.__accept_switch.get(format)
if value is None:
msg = f"Invalid value for 'format' argument: {format}; "
msg += "must be one of 'json', 'xml', 'csv', or 'json-seq'."
raise ValueError(msg)
else:
headers["Accept"] = value

response = self._session.post(path, headers=headers, data=data, **kwargs)
if response.ok and not return_response:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def test_dsl_json_seq(client):
verify_four_musicians_are_returned_in_json_seq(data)


def test_dsl_mixed(client):
response = client.rows.query(dsl_query, format="mixed")
verify_four_musicians_are_returned_in_json(
response.json(), "test.musician.lastName"
)
def test_invalid_format(client):
with raises(
ValueError, match="Invalid value for 'format' argument: invalid; must be one of"
):
client.rows.query(dsl_query, format="invalid")


def test_serialized_default(client):
Expand Down