sws-api-client 0.0.7.dev0__tar.gz → 0.0.9.dev0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sws_api_client
3
- Version: 0.0.7.dev0
3
+ Version: 0.0.9.dev0
4
4
  Summary: A Python client to easily interact with the FAO SWS REST APIs
5
5
  Home-page: https://bitbucket.org/cioapps/sws-it-python-api-client/src/main/
6
6
  Author: Mansillo, Daniele (CSI)
@@ -7,7 +7,7 @@ here = os.path.abspath(os.path.dirname(__file__))
7
7
  with open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
8
8
  long_description = "\n" + fh.read()
9
9
 
10
- VERSION = "0.0.7-dev"
10
+ VERSION = "0.0.9-dev"
11
11
 
12
12
  # Setting up
13
13
  setup(
@@ -47,6 +47,10 @@ class TaskResponse:
47
47
  task_id: str
48
48
  info: TaskInfo
49
49
 
50
+ @dataclass
51
+ class TaskCreateResponse:
52
+ task_id: str
53
+
50
54
  class TaskManager:
51
55
 
52
56
  def __init__(self, sws_client: SwsApiClient, endpoint: str = 'task_manager_api') -> None:
@@ -75,7 +79,7 @@ class TaskManager:
75
79
  group: Optional[str] = None,
76
80
  parentTaskId: Optional[str] = None,
77
81
  user:Optional[str] = None
78
- ) -> TaskResponse:
82
+ ) -> TaskCreateResponse:
79
83
 
80
84
  path = 'task/create'
81
85
 
@@ -100,7 +104,7 @@ class TaskManager:
100
104
 
101
105
  response = self.sws_client.discoverable.post(self.endpoint, path, data=data)
102
106
  if response:
103
- return self.get_task_response(response)
107
+ return self.get_task_create_response(response)
104
108
  else:
105
109
  return None
106
110
 
@@ -108,24 +112,29 @@ class TaskManager:
108
112
  return TaskResponse(
109
113
  task_id=task['taskId'],
110
114
  info=TaskInfo(
111
- detail_status=task['info'].get('detailStatus'),
112
- ended_on=task['info'].get('endedOn'),
113
- description=task['info'].get('description'),
114
- updated_on=task['info'].get('updatedOn'),
115
- created_on=task['info'].get('createdOn'),
116
- service_user=task['info'].get('serviceUser'),
117
- tags=task['info'].get('tags'),
118
- output=task['info'].get('output'),
119
- input=json.loads(task['info'].get('input', '{}')), # Default to an empty dictionary if 'input' is missing
120
- task_type=task['info'].get('taskType'),
121
- context=task['info'].get('context'),
122
- progress=task['info'].get('progress'),
123
- user=task['info'].get('user'),
124
- outcome=task['info'].get('outcome'),
125
- status=task['info'].get('status'),
126
- group=task['info'].get('group')
115
+ detail_status=task.get('info').get('detailStatus'),
116
+ ended_on=task.get('info').get('endedOn'),
117
+ description=task.get('info').get('description'),
118
+ updated_on=task.get('info').get('updatedOn'),
119
+ created_on=task.get('info').get('createdOn'),
120
+ service_user=task.get('info').get('serviceUser'),
121
+ tags=task.get('info').get('tags'),
122
+ output=task.get('info').get('output'),
123
+ input=json.loads(task.get('info').get('input', '{}')), # Default to an empty dictionary if 'input' is missing
124
+ task_type=task.get('info').get('taskType'),
125
+ context=task.get('info').get('context'),
126
+ progress=task.get('info').get('progress'),
127
+ user=task.get('info').get('user'),
128
+ outcome=task.get('info').get('outcome'),
129
+ status=task.get('info').get('status'),
130
+ group=task.get('group')
127
131
  )
128
132
  )
133
+
134
+ def get_task_create_response(self, task: Dict) -> TaskCreateResponse:
135
+ return TaskCreateResponse(
136
+ task_id=task['taskId']
137
+ )
129
138
 
130
139
 
131
140
  def get_task(self, task_id: str) -> Optional[TaskResponse]:
@@ -156,18 +165,23 @@ class TaskManager:
156
165
  Returns:
157
166
  str: The final outcome of the task.
158
167
  """
168
+ not_created_counter = 0
159
169
  while True:
160
170
  task_response = self.get_task(task_id)
161
171
 
162
- if not task_response:
172
+ if not task_response and not_created_counter > 5:
163
173
  raise ValueError(f"Task with ID {task_id} not found.")
164
-
165
- task_status = task_response.info.detail_status
166
- print(f"Task {task_id} status: {task_status}")
167
- if task_status == 'ENDED':
168
- return task_response
169
-
170
- time.sleep(poll_interval)
174
+ elif not task_response:
175
+ not_created_counter += 1
176
+ else:
177
+ not_created_counter = 0
178
+
179
+ task_status = task_response.info.detail_status
180
+ print(f"Task {task_id} status: {task_status}")
181
+ if task_status == 'ENDED':
182
+ return task_response
183
+
184
+ time.sleep(poll_interval)
171
185
 
172
186
  def wait_completion_by_ids(self, task_ids: List[str], poll_interval: int = 10) -> List[TaskResponse]:
173
187
  """
@@ -187,4 +201,5 @@ class TaskManager:
187
201
  if len(completed_tasks) == len(task_ids):
188
202
  return completed_tasks
189
203
 
190
- time.sleep(poll_interval)
204
+ time.sleep(poll_interval)
205
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sws_api_client
3
- Version: 0.0.7.dev0
3
+ Version: 0.0.9.dev0
4
4
  Summary: A Python client to easily interact with the FAO SWS REST APIs
5
5
  Home-page: https://bitbucket.org/cioapps/sws-it-python-api-client/src/main/
6
6
  Author: Mansillo, Daniele (CSI)