2 Commits eb29d4475b ... 4897b23014

Autor SHA1 Mensaje Fecha
  世祺 4897b23014 添加错误类型检测 hace 1 año
  世祺 051a985afc 提高代码健壮性与错误兼容性 hace 1 año
Se han modificado 2 ficheros con 106 adiciones y 19 borrados
  1. 39 18
      src/main.py
  2. 67 1
      测试.ipynb

+ 39 - 18
src/main.py

@@ -1,9 +1,11 @@
+import json
 from concurrent import futures
 
 import grpc
+import pydash
 
-from ll_sdk.config_api import ConfigApiClient
 import llnw_pb2_grpc
+from ll_sdk.config_api import ConfigApiClient
 from llnw_pb2 import *
 
 
@@ -13,23 +15,23 @@ def get_cl(base):
 
 def delivery_to_grpc(delivery):
     return DeliveryEntity(
-        uuid=delivery['uuid'],
-        isEnabled=delivery['isEnabled'],
-        shortname=delivery['shortname'],
-        status=delivery['status']['state'],
-        sourceHostname=delivery['body']['sourceHostname'],
-        publishedHostname=delivery['body']['publishedHostname'],
-        sourceUrlPath=delivery['body']['sourceUrlPath'],
-        publishedUrlPath=delivery['body']['publishedUrlPath'],
+        uuid=delivery.get('uuid', None),
+        isEnabled=delivery.get('isEnabled', None),
+        shortname=delivery.get('shortname', None),
+        status=pydash.objects.get(delivery, "status.state"),
+        sourceHostname=pydash.objects.get(delivery, "body.sourceHostname"),
+        publishedHostname=pydash.objects.get(delivery, "body.publishedHostname"),
+        sourceUrlPath=pydash.objects.get(delivery, "body.sourceUrlPath"),
+        publishedUrlPath=pydash.objects.get(delivery, "body.publishedUrlPath"),
         protocolSets=[ProtocolSet(
-            sourceProtocol=protocolSets['sourceProtocol'],
-            publishedProtocol=protocolSets['publishedProtocol'],
+            sourceProtocol=protocolSets.get('sourceProtocol', None),
+            publishedProtocol=protocolSets.get('publishedProtocol', None),
             options=[ProtocolSetOptions(
-                name=options['name'],
-                parameters=options['parameters'],
-                source=options['source'],
-            ) for options in protocolSets['options']]
-        ) for protocolSets in delivery['body']['protocolSets']]
+                name=options.get('name', None),
+                parameters=[str(param) for param in options.get('parameters', None)],
+                source=options.get('source', None),
+            ) for options in protocolSets.get('options', [])]
+        ) for protocolSets in pydash.objects.get(delivery, 'body.protocolSets', [])]
     )
 
 
@@ -48,7 +50,9 @@ def delivery_from_grpc(delivery_entity: DeliveryEntity):
                     "options": [
                         {
                             "name": po.name,
-                            "parameters": po.parameters,
+                            "parameters": [
+                                para if po.name != "refresh_absmin" and po.name != "refresh_absmax" else int(para)
+                                for para in po.parameters],
                             "source": po.source
                         } for po in protocol.options
                     ],
@@ -72,33 +76,50 @@ def delivery_from_grpc(delivery_entity: DeliveryEntity):
     }
 
 
+def check_error(res, context):
+    if not res.get('success', True):
+        match res.get('errorType'):
+            case 'validation':
+                context.abort(grpc.StatusCode.INVALID_ARGUMENT, json.dumps(res.get('errors')))
+            case _:
+                context.abort(grpc.StatusCode.UNKNOWN, json.dumps(res))
+
+
 class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
 
     def ListDelivery(self, request, context):
         res = get_cl(request.base).list_delivery_service_instances(request.shortName).json()
+        check_error(res, context)
         return DeliveryList(deliveries=[delivery_to_grpc(results) for results in res['results']])
 
     def ValidateDelivery(self, request, context):
         delivery_config = delivery_from_grpc(request.delivery)
+        del delivery_config['uuid']
         res = get_cl(request.base).validate_delivery_service_instance(delivery_config).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def CreateDelivery(self, request, context):
         delivery_config = delivery_from_grpc(request.delivery)
+        del delivery_config['uuid']
         res = get_cl(request.base).create_delivery_service_instance(delivery_config).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def GetDelivery(self, request, context):
         res = get_cl(request.base).get_delivery_service_instance(request.id).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def UpdateDelivery(self, request, context):
         delivery_config = delivery_from_grpc(request.delivery)
-        res = get_cl(request.base).update_delivery_service_instance(delivery_config.uuid. delivery_config).json()
+        res = get_cl(request.base).update_delivery_service_instance(delivery_config['uuid'], delivery_config).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def DeleteDelivery(self, request, context):
         res = get_cl(request.base).delete_delivery_service_instance(request.id).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
 

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 67 - 1
测试.ipynb