Skip to content

Commit

Permalink
Merge pull request #835 from Strunevskiy/master
Browse files Browse the repository at this point in the history
Add support for application/json mime type in statement output
  • Loading branch information
devstein committed Sep 13, 2023
2 parents 51ff522 + e7a354c commit 4a8ca9e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added support for http requests session adapter configuration
- Added support default spark session config settings. Any settings set in as defaults
are preserved unless explicitly overridden by the user
- Added support for application/json mime type in statement output

## 0.20.5

Expand Down
3 changes: 3 additions & 0 deletions sparkmagic/sparkmagic/livyclientlib/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MIMETYPE_TEXT_PLAIN,
COMMAND_INTERRUPTED_MSG,
COMMAND_CANCELLATION_FAILED_MSG,
MIMETYPE_APPLICATION_JSON
)
from .exceptions import (
LivyUnexpectedStatusException,
Expand Down Expand Up @@ -144,6 +145,8 @@ def _get_statement_output(self, session, statement_id):
return (True, image, MIMETYPE_IMAGE_PNG)
elif MIMETYPE_TEXT_HTML in data:
return (True, data[MIMETYPE_TEXT_HTML], MIMETYPE_TEXT_HTML)
elif MIMETYPE_APPLICATION_JSON in data:
return (True, data[MIMETYPE_APPLICATION_JSON], MIMETYPE_APPLICATION_JSON)
else:
return (True, data[MIMETYPE_TEXT_PLAIN], MIMETYPE_TEXT_PLAIN)
elif statement_output["status"] == "error":
Expand Down
18 changes: 18 additions & 0 deletions sparkmagic/sparkmagic/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MIMETYPE_TEXT_HTML,
MIMETYPE_TEXT_PLAIN,
COMMAND_INTERRUPTED_MSG,
MIMETYPE_APPLICATION_JSON
)
from sparkmagic.livyclientlib.command import Command
from sparkmagic.livyclientlib.livysession import LivySession
Expand Down Expand Up @@ -117,6 +118,23 @@ def test_execute():
assert "<p>out</p>" == result[1]
assert MIMETYPE_TEXT_HTML == result[2]

# Now try with JSON result:
http_client.get_statement.return_value = {
"id": 0,
"state": "available",
"output": {
"status": "ok",
"execution_count": 0,
"data": {"application/json": {
"key_test": "value_test"
}},
},
}
result = command.execute(session)
assert result[0]
assert {"key_test": "value_test"} == result[1]
assert MIMETYPE_APPLICATION_JSON == result[2]


def test_execute_waiting():
spark_events = MagicMock()
Expand Down
1 change: 1 addition & 0 deletions sparkmagic/sparkmagic/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@
MIMETYPE_IMAGE_PNG = "image/png"
MIMETYPE_TEXT_HTML = "text/html"
MIMETYPE_TEXT_PLAIN = "text/plain"
MIMETYPE_APPLICATION_JSON = "application/json"

0 comments on commit 4a8ca9e

Please sign in to comment.