def reverse_strings(arg: Iterator, ctx: ActionContext) -> Iterator[FunctionResponse]: for element in arg: if element.foo == "boom": ctx.fail("Intentionally failed.") yield FunctionResponse(bar=element.foo[::-1])
def reverse_string2(arg: FunctionRequest2, ctx: ActionContext) -> FunctionResponse2: if arg.foo == "boom": ctx.fail("Intentionally failed.") else: return FunctionResponse2(bar=arg.foo[::-1] + "!")
def sum_stream( arg: Iterator, # todo, really need generics on this api but the # reflection api doesn't allow it.. ctx: ActionContext, ) -> SumTotal: total = 0 for element in arg: if element.quantity < 0: ctx.fail("Intentionally failed.") total += element.quantity return SumTotal(total=total)
def handleStreamedIn(self, request_iterator, context): peek = request_iterator.next() # evidently, the first message has no payload # and is probably intended to prime the stream handler. logging.debug(f"peeked: {peek}") if peek.service_name in self.action_protocol_entities: handler = ActionHandler(self.action_protocol_entities[peek.service_name]) logging.debug(f"set stream in handler to {peek.service_name}") else: context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details("Method not implemented!") raise NotImplementedError("Method not implemented!") reconstructed = (get_payload(x) for x in request_iterator) ctx = ActionContext(peek.name) try: result = handler.handle_stream_in( reconstructed, ctx ) # the proto the user defined function returned. client_action = ctx.create_client_action(result, False) action_reply = ActionResponse() if not ctx.has_errors(): action_reply.side_effects.extend(ctx.effects) if client_action.HasField("reply"): action_reply.reply.CopyFrom(client_action.reply) elif client_action.HasField("forward"): action_reply.forward.CopyFrom(client_action.forward) else: action_reply.failure.CopyFrom(client_action.failure) return action_reply except Exception as ex: ctx.fail(str(ex)) logging.exception("Failed to execute command:" + str(ex))
def handleUnary(self, request: ActionCommand, context): logging.info(f"handling unary {request} {context}.") if request.service_name in self.action_protocol_entities: service = self.action_protocol_entities[request.service_name] handler = ActionHandler(service) ctx = ActionContext(request.name) result = None try: result = handler.handle_unary( get_payload(request), ctx ) # the proto the user defined function returned. except Exception as ex: ctx.fail(str(ex)) logging.exception("Failed to execute command:" + str(ex)) client_action: ClientAction = ctx.create_client_action(result, False) action_reply = ActionResponse() if not ctx.has_errors(): action_reply.side_effects.extend(ctx.effects) if client_action.HasField("reply"): action_reply.reply.CopyFrom(client_action.reply) elif client_action.HasField("forward"): action_reply.forward.CopyFrom(client_action.forward) else: action_reply.failure.CopyFrom(client_action.failure) return action_reply
def handleStreamedOut(self, request, context): if request.service_name in self.action_protocol_entities: handler = ActionHandler(self.action_protocol_entities[request.service_name]) else: context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details("Method not implemented!") raise NotImplementedError("Method not implemented!") reconstructed = get_payload(request) ctx = ActionContext(request.name) try: for result in handler.handle_stream_out(reconstructed, ctx): client_action = ctx.create_client_action(result, False) action_reply = ActionResponse() if not ctx.has_errors(): action_reply.side_effects.extend(ctx.effects) if client_action.HasField("reply"): action_reply.reply.CopyFrom(client_action.reply) elif client_action.HasField("forward"): action_reply.forward.CopyFrom(client_action.forward) else: action_reply.failure.CopyFrom(client_action.failure) yield action_reply except Exception as ex: ctx.fail(str(ex)) logging.exception("Failed to execute command:" + str(ex))
def silly_letter_stream(arg: FunctionRequest, ctx: ActionContext) -> SumTotal: if arg.foo == "nope": ctx.fail("Intentionally failed.") letters = list(arg.foo) for letter in letters: yield FunctionResponse(bar=letter + "!!")