diff --git a/mistralclient/api/executions.py b/mistralclient/api/executions.py index be8037e1..abd4ab23 100644 --- a/mistralclient/api/executions.py +++ b/mistralclient/api/executions.py @@ -27,32 +27,27 @@ class Execution(base.Resource): class ExecutionManager(base.ResourceManager): resource_class = Execution - def _validate_context_str(self, context): + def _get_context_as_str(self, context): msg = 'Context must be a dictionary or json compatible string.' - - if not isinstance(context, str): - raise ex.IllegalArgumentException(msg) + context_as_str = str(context) try: - json.loads(context) + json.loads(context_as_str) except Exception as e: raise ex.IllegalArgumentException(msg + e) + return context_as_str def create(self, workbook_name, task, context=None): self._ensure_not_empty(workbook_name=workbook_name, task=task) - if isinstance(context, dict): - context_str = str(context) - else: - self._validate_context_str(context) - context_str = context - data = { 'workbook_name': workbook_name, - 'task': task, - 'context': context_str + 'task': task } + if context is not None: + data['context'] = self._get_context_as_str(context) + return self._create('/workbooks/%s/executions' % workbook_name, data) def update(self, workbook_name, id, state): diff --git a/mistralclient/tests/test_executions.py b/mistralclient/tests/test_executions.py index 2643925d..01ad09d6 100644 --- a/mistralclient/tests/test_executions.py +++ b/mistralclient/tests/test_executions.py @@ -53,6 +53,15 @@ class TestExecutions(base.BaseClientTest): self.assertEqual(EXECS[0]['state'], ex.state) self.assertEqual(EXECS[0]['context'], ex.context) + def test_create_with_empty_context(self): + execs = EXECS[0].copy() + execs.pop('context') + self.mock_http_post(json=execs) + ex = self.executions.create(execs['workbook_name'], + execs['target_task']) + with self.assertRaises(AttributeError): + ex.context + @unittest2.expectedFailure def test_create_failure1(self): self.executions.create(EXECS[0]['workbook_name'],