sequence.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from flask import request
  12. from flask_restful import Resource
  13. from taipy.core import Sequence
  14. from taipy.core.exceptions.exceptions import NonExistingScenario, NonExistingSequence
  15. from taipy.core.scenario._scenario_manager_factory import _ScenarioManagerFactory
  16. from taipy.core.sequence._sequence_manager_factory import _SequenceManagerFactory
  17. from ...commons.to_from_model import _to_model
  18. from ..exceptions.exceptions import ScenarioIdMissingException, SequenceNameMissingException
  19. from ..middlewares._middleware import _middleware
  20. from ..schemas import SequenceResponseSchema
  21. def _get_or_raise(sequence_id: str) -> Sequence:
  22. if sequence := _SequenceManagerFactory._build_manager()._get(sequence_id):
  23. return sequence
  24. raise NonExistingSequence(sequence_id)
  25. REPOSITORY = "sequence"
  26. class SequenceResource(Resource):
  27. """Single object resource
  28. ---
  29. get:
  30. tags:
  31. - api
  32. summary: Get a sequence.
  33. description: |
  34. Return a single sequence by sequence_id. If the sequence does not exist, a 404 error is returned.
  35. !!! Note
  36. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  37. requires _TAIPY_READER_ role.
  38. Code example:
  39. ```shell
  40. curl -X GET http://localhost:5000/api/v1/sequences/SEQUENCE_my_config_75750ed8-4e09-4e00-958d-e352ee426cc9
  41. ```
  42. parameters:
  43. - in: path
  44. name: sequence_id
  45. schema:
  46. type: string
  47. description: The identifier of the sequence.
  48. responses:
  49. 200:
  50. content:
  51. application/json:
  52. schema:
  53. type: object
  54. properties:
  55. sequence: SequenceSchema
  56. 404:
  57. description: No sequence has the *sequence_id* identifier.
  58. delete:
  59. tags:
  60. - api
  61. summary: Delete a sequence.
  62. description: |
  63. Delete a sequence. If the sequence does not exist, a 404 error is returned.
  64. !!! Note
  65. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  66. requires _TAIPY_EDITOR_ role.
  67. Code example:
  68. ```shell
  69. curl -X DELETE http://localhost:5000/api/v1/sequences/SEQUENCE_my_config_75750ed8-4e09-4e00-958d-e352ee426cc9
  70. ```
  71. parameters:
  72. - in: path
  73. name: sequence_id
  74. schema:
  75. type: string
  76. description: The identifier of the sequence.
  77. responses:
  78. 200:
  79. content:
  80. application/json:
  81. schema:
  82. type: object
  83. properties:
  84. message:
  85. type: string
  86. description: Status message.
  87. 404:
  88. description: No sequence has the *sequence_id* identifier.
  89. """
  90. def __init__(self, **kwargs):
  91. self.logger = kwargs.get("logger")
  92. @_middleware
  93. def get(self, sequence_id):
  94. schema = SequenceResponseSchema()
  95. sequence = _get_or_raise(sequence_id)
  96. return {"sequence": schema.dump(_to_model(REPOSITORY, sequence))}
  97. @_middleware
  98. def delete(self, sequence_id):
  99. manager = _SequenceManagerFactory._build_manager()
  100. _get_or_raise(sequence_id)
  101. manager._delete(sequence_id)
  102. return {"message": f"Sequence {sequence_id} was deleted."}
  103. class SequenceList(Resource):
  104. """Creation and get_all
  105. ---
  106. get:
  107. tags:
  108. - api
  109. summary: Get all sequences.
  110. description: |
  111. Return an array of all sequences.
  112. !!! Note
  113. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  114. requires _TAIPY_READER_ role.
  115. Code example:
  116. ```shell
  117. curl -X GET http://localhost:5000/api/v1/sequences
  118. ```
  119. responses:
  120. 200:
  121. content:
  122. application/json:
  123. schema:
  124. allOf:
  125. - type: object
  126. properties:
  127. results:
  128. type: array
  129. items:
  130. $ref: '#/components/schemas/SequenceSchema'
  131. post:
  132. tags:
  133. - api
  134. summary: Create a sequence.
  135. description: |
  136. Create a sequence from scenario_id, sequence_name and task_ids. If the scenario_id does not exist or
  137. sequence_name is not provided, a 404 error is returned.
  138. !!! Note
  139. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  140. requires _TAIPY_EDITOR_ role.
  141. Code example:
  142. ```shell
  143. curl -X POST --data '{"scenario_id": "SCENARIO_scenario_id", "sequence_name": "sequence", "tasks": []}' \\
  144. http://localhost:5000/api/v1/sequences
  145. ```
  146. parameters:
  147. - in: query
  148. name: scenario_id
  149. schema:
  150. type: string
  151. description: The Scenario the Sequence belongs to.
  152. name: sequence_name
  153. schema:
  154. type: string
  155. description: The name of the Sequence.
  156. name: tasks
  157. schema:
  158. type: list[string]
  159. description: A list of task id of the Sequence.
  160. responses:
  161. 201:
  162. content:
  163. application/json:
  164. schema:
  165. type: object
  166. properties:
  167. message:
  168. type: string
  169. description: Status message.
  170. sequence: SequenceSchema
  171. """
  172. def __init__(self, **kwargs):
  173. self.logger = kwargs.get("logger")
  174. @_middleware
  175. def get(self):
  176. schema = SequenceResponseSchema(many=True)
  177. manager = _SequenceManagerFactory._build_manager()
  178. sequences = [_to_model(REPOSITORY, sequence) for sequence in manager._get_all()]
  179. return schema.dump(sequences)
  180. @_middleware
  181. def post(self):
  182. sequence_data = request.json
  183. scenario_id = sequence_data.get("scenario_id")
  184. sequence_name = sequence_data.get("sequence_name")
  185. sequence_task_ids = sequence_data.get("task_ids", [])
  186. response_schema = SequenceResponseSchema()
  187. if not scenario_id:
  188. raise ScenarioIdMissingException
  189. if not sequence_name:
  190. raise SequenceNameMissingException
  191. scenario = _ScenarioManagerFactory._build_manager()._get(scenario_id)
  192. if not scenario:
  193. raise NonExistingScenario(scenario_id=scenario_id)
  194. scenario.add_sequence(sequence_name, sequence_task_ids)
  195. sequence = scenario.sequences[sequence_name]
  196. return {
  197. "message": "Sequence was created.",
  198. "sequence": response_schema.dump(_to_model(REPOSITORY, sequence)),
  199. }, 201
  200. class SequenceExecutor(Resource):
  201. """Execute a sequence
  202. ---
  203. post:
  204. tags:
  205. - api
  206. summary: Execute a sequence.
  207. description: |
  208. Execute a sequence from sequence_id. If the sequence does not exist, a 404 error is returned.
  209. !!! Note
  210. When the authorization feature is activated (available in the **Enterprise** edition only), This endpoint
  211. requires _TAIPY_EXECUTOR_ role.
  212. Code example:
  213. ```shell
  214. curl -X POST http://localhost:5000/api/v1/sequences/submit/SEQUENCE_my_config_7575-4e09-4e00-958d-e352ee426cc9
  215. ```
  216. parameters:
  217. - in: path
  218. name: sequence_id
  219. schema:
  220. type: string
  221. responses:
  222. 204:
  223. content:
  224. application/json:
  225. schema:
  226. type: object
  227. properties:
  228. message:
  229. type: string
  230. description: Status message.
  231. sequence: SequenceSchema
  232. 404:
  233. description: No sequence has the *sequence_id* identifier.
  234. """
  235. def __init__(self, **kwargs):
  236. self.logger = kwargs.get("logger")
  237. @_middleware
  238. def post(self, sequence_id):
  239. _get_or_raise(sequence_id)
  240. manager = _SequenceManagerFactory._build_manager()
  241. manager._submit(sequence_id)
  242. return {"message": f"Sequence {sequence_id} was submitted."}