|
9 | 9 | from dataclasses import dataclass, asdict
|
10 | 10 |
|
11 | 11 | import falcon
|
| 12 | +import json |
12 | 13 | from dataclasses_json import dataclass_json
|
13 | 14 | from keri import kering
|
14 | 15 | from keri.app.oobiing import Result
|
@@ -138,6 +139,26 @@ def get(self, name):
|
138 | 139 |
|
139 | 140 | return operation
|
140 | 141 |
|
| 142 | + def getOperations(self, type=None): |
| 143 | + """ Return list of long running opterations, optionally filtered by type """ |
| 144 | + ops = self.opr.ops.getItemIter() |
| 145 | + if type != None: |
| 146 | + ops = filter(lambda i: i[1].type == type, ops) |
| 147 | + |
| 148 | + def get_status(op): |
| 149 | + try: |
| 150 | + return self.status(op) |
| 151 | + except Exception as err: |
| 152 | + # self.status may throw an exception. |
| 153 | + # Handling error by returning an operation with error status |
| 154 | + return Operation( |
| 155 | + name=f"{op.type}.{op.oid}", |
| 156 | + metadata=op.metadata, |
| 157 | + done=True, |
| 158 | + error=Status(code=500, message=f"{err}")) |
| 159 | + |
| 160 | + return [get_status(op) for (_, op) in ops] |
| 161 | + |
141 | 162 | def rem(self, name):
|
142 | 163 | """ Remove tracking of the long running operation represented by name """
|
143 | 164 | return self.opr.ops.rem(keys=(name,))
|
@@ -365,6 +386,40 @@ def status(self, op):
|
365 | 386 | return operation
|
366 | 387 |
|
367 | 388 |
|
| 389 | +class OperationCollectionEnd: |
| 390 | + @staticmethod |
| 391 | + def on_get(req, rep): |
| 392 | + """ Get list of long running operations |
| 393 | + |
| 394 | + Parameters: |
| 395 | + req (Request): Falcon HTTP Request object |
| 396 | + rep (Response): Falcon HTTP Response object |
| 397 | +
|
| 398 | + --- |
| 399 | + summary: Get list of long running operations |
| 400 | + parameters: |
| 401 | + - in: query |
| 402 | + name: type |
| 403 | + schema: |
| 404 | + type: string |
| 405 | + required: false |
| 406 | + description: filter list of long running operations by type |
| 407 | + responses: |
| 408 | + 200: |
| 409 | + content: |
| 410 | + application/json: |
| 411 | + schema: |
| 412 | + type: array |
| 413 | +
|
| 414 | + """ |
| 415 | + agent = req.context.agent |
| 416 | + type = req.params.get("type") |
| 417 | + ops = agent.monitor.getOperations(type=type) |
| 418 | + rep.data = json.dumps(ops, default=lambda o: o.to_dict()).encode("utf-8") |
| 419 | + rep.content_type = "application/json" |
| 420 | + rep.status = falcon.HTTP_200 |
| 421 | + |
| 422 | + |
368 | 423 | class OperationResourceEnd:
|
369 | 424 | """ Single Resource REST endpoint for long running operations
|
370 | 425 |
|
|
0 commit comments