| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import base64 | 5 import base64 |
| 6 import json | 6 import json |
| 7 import uuid | 7 import uuid |
| 8 | 8 |
| 9 class ClovisTask(object): | 9 class ClovisTask(object): |
| 10 """Generic task, generated by the AppEngine frontend and consumed by the | 10 """Generic task, generated by the AppEngine frontend and consumed by the |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 'tag' key, a unique tag will be generated. | 23 'tag' key, a unique tag will be generated. |
| 24 """ | 24 """ |
| 25 self._action = action | 25 self._action = action |
| 26 self._action_params = action_params or {} | 26 self._action_params = action_params or {} |
| 27 self._backend_params = backend_params or {} | 27 self._backend_params = backend_params or {} |
| 28 # If no tag is specified, generate a unique tag. | 28 # If no tag is specified, generate a unique tag. |
| 29 if not self._backend_params.get('tag'): | 29 if not self._backend_params.get('tag'): |
| 30 self._backend_params.update({'tag': str(uuid.uuid1())}) | 30 self._backend_params.update({'tag': str(uuid.uuid1())}) |
| 31 | 31 |
| 32 @classmethod | 32 @classmethod |
| 33 def FromJsonString(cls, json_dict): | 33 def FromJsonDict(cls, json_dict): |
| 34 """Loads a ClovisTask from a JSON string. | 34 """Loads a ClovisTask from a JSON dictionary. |
| 35 | 35 |
| 36 Returns: | 36 Returns: |
| 37 ClovisTask: The task, or None if the string is invalid. | 37 ClovisTask: The task, or None if the string is invalid. |
| 38 """ | 38 """ |
| 39 try: | 39 try: |
| 40 data = json.loads(json_dict) | 40 action = json_dict['action'] |
| 41 action = data['action'] | 41 action_params = json_dict['action_params'] |
| 42 action_params = data['action_params'] | |
| 43 # Vaidate the format. | 42 # Vaidate the format. |
| 44 if action == 'trace': | 43 if action == 'trace': |
| 45 urls = action_params['urls'] | 44 urls = action_params['urls'] |
| 46 if (type(urls) is not list) or (len(urls) == 0): | 45 if (type(urls) is not list) or (len(urls) == 0): |
| 47 return None | 46 return None |
| 48 elif action == 'report': | 47 elif action == 'report': |
| 49 if not action_params.get('trace_bucket'): | 48 if not action_params.get('trace_bucket'): |
| 50 return None | 49 return None |
| 51 else: | 50 else: |
| 52 # When more actions are supported, check that they are valid here. | 51 # When more actions are supported, check that they are valid here. |
| 53 return None | 52 return None |
| 54 return cls(action, action_params, data.get('backend_params')) | 53 return cls(action, action_params, json_dict.get('backend_params')) |
| 55 except Exception: | 54 except Exception: |
| 56 return None | 55 return None |
| 57 | 56 |
| 57 @classmethod |
| 58 def FromJsonString(cls, json_string): |
| 59 """Loads a ClovisTask from a JSON string. |
| 60 |
| 61 Returns: |
| 62 ClovisTask: The task, or None if the string is invalid. |
| 63 """ |
| 64 try: |
| 65 return cls.FromJsonDict(json.loads(json_string)) |
| 66 except Exception: |
| 67 return None |
| 68 |
| 58 @classmethod | 69 @classmethod |
| 59 def FromBase64(cls, base64_string): | 70 def FromBase64(cls, base64_string): |
| 60 """Loads a ClovisTask from a base 64 string.""" | 71 """Loads a ClovisTask from a base 64 string.""" |
| 61 return ClovisTask.FromJsonString(base64.b64decode(base64_string)) | 72 return ClovisTask.FromJsonString(base64.b64decode(base64_string)) |
| 62 | 73 |
| 74 def ToJsonDict(self): |
| 75 """Returns the JSON representation of the task as a dictionary.""" |
| 76 return {'action': self._action, 'action_params': self._action_params, |
| 77 'backend_params': self._backend_params} |
| 78 |
| 63 def ToJsonString(self): | 79 def ToJsonString(self): |
| 64 """Returns the JSON representation of the task.""" | 80 """Returns the JSON representation of the task as a string.""" |
| 65 task_dict = {'action': self._action, 'action_params': self._action_params, | 81 return json.dumps(self.ToJsonDict()) |
| 66 'backend_params': self._backend_params} | |
| 67 return json.dumps(task_dict) | |
| 68 | 82 |
| 69 def Action(self): | 83 def Action(self): |
| 70 return self._action | 84 return self._action |
| 71 | 85 |
| 72 def ActionParams(self): | 86 def ActionParams(self): |
| 73 return self._action_params | 87 return self._action_params |
| 74 | 88 |
| 75 def BackendParams(self): | 89 def BackendParams(self): |
| 76 return self._backend_params | 90 return self._backend_params |
| 77 | 91 |
| OLD | NEW |