Skip to content

Commit

Permalink
Merge pull request #42 from oceanprotocol/fix-non-serializable-responses
Browse files Browse the repository at this point in the history
Fix non serializable responses by converting Decimals to floats
  • Loading branch information
alexcos20 authored Mar 29, 2022
2 parents 71a0840 + 8a8dd54 commit 445d753
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions operator_service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from operator_service.utils import (
create_compute_job,
check_required_attributes,
decimals_to_float,
generate_new_id,
process_signature_validation,
get_compute_resources,
Expand Down Expand Up @@ -206,10 +207,10 @@ def start_compute_job():
agreement_id, str(job_id), owner, body, environment, provider_address
)
status_list = get_sql_status(agreement_id, str(job_id), owner)
# Convert every value from the list of dicts to a string
status_list = [{k: str(v) for k, v in d.items()} for d in status_list]

return Response(json.dumps(status_list), 200, headers=standard_headers)
return Response(
json.dumps(decimals_to_float(status_list)), 200, headers=standard_headers
)


@services.route("/compute", methods=["PUT"])
Expand Down Expand Up @@ -396,9 +397,10 @@ def get_compute_job_status():
return Response(json.dumps({"error": msg}), 400, headers=standard_headers)
logger.debug(f"Got status request for {agreement_id}, {job_id}, {owner}")
api_response = get_sql_status(agreement_id, job_id, owner)
api_response = [{k: str(v) for k, v in d.items()} for d in api_response]

return Response(json.dumps(api_response), 200, headers=standard_headers)
return Response(
json.dumps(decimals_to_float(api_response)), 200, headers=standard_headers
)

except ApiException as e:
msg = f"Error getting the status: {e}"
Expand Down
17 changes: 17 additions & 0 deletions operator_service/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
import json
import os
import uuid
Expand Down Expand Up @@ -180,3 +181,19 @@ def _generate(_response):
except Exception as e:
logger.error(f"Error preparing file download response: {str(e)}")
raise


def decimals_to_float(d):
"""
Recursively convert Decimal to float
in: dict | list | tuple | set | str | int | float | None
out: dict | list | tuple | set | str | int | float | None
"""
if isinstance(d, Decimal):
return float(d)
elif isinstance(d, dict):
return {decimals_to_float(k): decimals_to_float(v) for k, v in d.items()}
elif isinstance(d, list):
return [decimals_to_float(element) for element in d]
else:
return d

0 comments on commit 445d753

Please sign in to comment.