Skip to content

Commit

Permalink
Merge pull request #43 from oceanprotocol/fix-non-serializable-responses
Browse files Browse the repository at this point in the history
Sanitize responses to provider when needed
  • Loading branch information
alexcos20 authored Mar 29, 2022
2 parents 445d753 + 16608a3 commit 1b2eb08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 7 additions & 3 deletions operator_service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from operator_service.utils import (
create_compute_job,
check_required_attributes,
decimals_to_float,
sanitize_response_for_provider,
generate_new_id,
process_signature_validation,
get_compute_resources,
Expand Down Expand Up @@ -209,7 +209,9 @@ def start_compute_job():
status_list = get_sql_status(agreement_id, str(job_id), owner)

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


Expand Down Expand Up @@ -399,7 +401,9 @@ def get_compute_job_status():
api_response = get_sql_status(agreement_id, job_id, owner)

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

except ApiException as e:
Expand Down
15 changes: 10 additions & 5 deletions operator_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,22 @@ def _generate(_response):
raise


def decimals_to_float(d):
def sanitize_response_for_provider(d):
"""
Recursively convert Decimal to float
Sanitize objects to send them to provider by recursively convert Decimal and float values to string
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)
return str(d)
elif isinstance(d, float):
return str(d)
elif isinstance(d, dict):
return {decimals_to_float(k): decimals_to_float(v) for k, v in d.items()}
return {
sanitize_response_for_provider(k): sanitize_response_for_provider(v)
for k, v in d.items()
}
elif isinstance(d, list):
return [decimals_to_float(element) for element in d]
return [sanitize_response_for_provider(element) for element in d]
else:
return d

0 comments on commit 1b2eb08

Please sign in to comment.