Skip to content
Open
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
12 changes: 12 additions & 0 deletions surge/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dateutil.parser
import json

from surge.errors import SurgeMissingIDError, SurgeTaskDataError
from surge.api_resource import PROJECTS_ENDPOINT, TASKS_ENDPOINT, APIResource
Expand Down Expand Up @@ -92,6 +93,11 @@ def create(cls, project_id: str, api_key: str = None, **params):
Returns:
task: new Task object
'''
try:
json.dumps(params)
except (TypeError, ValueError):
raise SurgeTaskDataError("All task data must be JSON-serializable")

endpoint = f"{PROJECTS_ENDPOINT}/{project_id}/{TASKS_ENDPOINT}"
data = {"fields": params}
response_json = cls.post(endpoint, data, api_key=api_key)
Expand All @@ -110,6 +116,7 @@ def create_many(cls,
project_id (str): ID of the project to which the tasks are added.
tasks_data (list): list of dicts that map each task field to its value.
e.g. [{"website": "surgehq.ai"}, {"website":"twitch.tv"}]
All values must be JSON-serializable.

Returns:
tasks (list): list of Task objects
Expand All @@ -119,6 +126,11 @@ def create_many(cls,

if not all(isinstance(t, dict) for t in tasks_data):
raise SurgeTaskDataError

try:
json.dumps(tasks_data)
except (TypeError, ValueError):
raise SurgeTaskDataError("All task data must be JSON-serializable")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to surface the real JSON error too in case it's informative

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any preference for how to do that?


endpoint = f"{PROJECTS_ENDPOINT}/{project_id}/{TASKS_ENDPOINT}/create_tasks"
data = {"tasks": tasks_data, "launch": launch}
Expand Down