Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for application/json mime type in statement output #835

Merged
merged 2 commits into from
Sep 13, 2023
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: 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"
Loading