Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
content_type=""
if request.method == 'my_method':
custom_response = response_messages.CustomResponse(isSuccess=True, code=200, message="Hello World - Success!")
data = Any()
data.Pack(custom_response)
else:
data = Any(value='METHOD_NOT_SUPPORTED'.encode('utf-8'))
content_type="text/plain"
print(data, flush=True)
print(content_type, flush=True)
return common_v1.InvokeResponse(data=data, content_type=content_type)
# Create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers = 10))
appcallback_service_v1.add_AppCallbackServicer_to_server(AppCallback(), server)
# Start the gRPC server
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# Since server.start() doesn't block, we need to do a sleep loop
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
def OnInvoke(self, request, context):
data=None
content_type=""
if request.method == 'my-method':
data = Any(value='INVOKE_RECEIVED'.encode('utf-8'))
content_type = "text/plain; charset=UTF-8"
else:
data = Any(value='unsupported methods'.encode('utf-8'))
content_type = "text/plain; charset=UTF-8"
# Return response to caller
return common_v1.InvokeResponse(data=data, content_type=content_type)
# Create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers = 10))
appcallback_service_v1.add_AppCallbackServicer_to_server(AppCallback(), server)
# Start the gRPC server
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# Since server.start() doesn't block, we need to do a sleep loop
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
class AppCallback(appcallback_service_v1.AppCallbackServicer):
def ListInputBindings(self, request, context):
return appcallback_v1.ListInputBindingsResponse(bindings=['kafkaBinding'])
def OnBindingEvent(self, request, context):
print(request.name, flush=True)
print(request.data, flush=True)
# Return response to caller
return appcallback_v1.BindingEventResponse(store_name='Non Persistant', to='nothing', data=request.data)
# Create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
appcallback_service_v1.add_AppCallbackServicer_to_server(AppCallback(), server)
# Start the gRPC server
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# Since server.start() doesn't block, we need to do a sleep loop
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
def __init__(self):
"""Inits App object and creates gRPC server.
Args:
app_port (int, optional): application port gRPC server listens to.
"""
self._servicer = _CallbackServicer()
self._server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
appcallback_service_v1.add_AppCallbackServicer_to_server(self._servicer, self._server)
# Dapr will call this method to get the list of topics the app
# wants to subscribe to. In this example, we are telling Dapr
# To subscribe to a topic named TOPIC_A
return appcallback_v1.ListTopicSubscriptionsResponse(subscriptions=[appcallback_v1.TopicSubscription(topic='TOPIC_A'),])
def OnTopicEvent(self, request, context):
logging.info("Topic: " + request.topic)
logging.info("Data content type: " + str(request.data_content_type))
logging.info("Data: " + str(request.data))
logging.info("Event received!!")
return empty_pb2.Empty()
# Create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
appcallback_service_v1.add_AppCallbackServicer_to_server(AppCallback(), server)
# Start the gRPC server
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# Since server.start() doesn't block, we need to do a sleep loop
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
def __init__(self):
self._servicer = AppCallbackServicer()
self._server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
appcallback_service_v1.add_AppCallbackServicer_to_server(self._servicer, self._server)
self._server.add_insecure_port('[::]:50051')
import os
from concurrent import futures
import time
import grpc
from dapr.proto import api_v1, api_service_v1, appcallback_service_v1, common_v1
import proto.response_pb2 as response_messages
from google.protobuf.any_pb2 import Any
# Our server methods
class AppCallback(appcallback_service_v1.AppCallbackServicer):
def OnInvoke(self, request, context):
data=None
content_type=""
if request.method == 'my_method':
custom_response = response_messages.CustomResponse(isSuccess=True, code=200, message="Hello World - Success!")
data = Any()
data.Pack(custom_response)
else:
data = Any(value='METHOD_NOT_SUPPORTED'.encode('utf-8'))
content_type="text/plain"
print(data, flush=True)
print(content_type, flush=True)
return common_v1.InvokeResponse(data=data, content_type=content_type)
# Create a gRPC server
import os
from concurrent import futures
import time
import grpc
from dapr.proto import appcallback_v1, appcallback_service_v1, common_v1
from google.protobuf import empty_pb2
import logging
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger(__name__)
# Our server methods
class AppCallback(appcallback_service_v1.AppCallbackServicer):
def ListTopicSubscriptions(self, request, context):
# Dapr will call this method to get the list of topics the app
# wants to subscribe to. In this example, we are telling Dapr
# To subscribe to a topic named TOPIC_A
return appcallback_v1.ListTopicSubscriptionsResponse(subscriptions=[appcallback_v1.TopicSubscription(topic='TOPIC_A'),])
def OnTopicEvent(self, request, context):
logging.info("Topic: " + request.topic)
logging.info("Data content type: " + str(request.data_content_type))
logging.info("Data: " + str(request.data))
logging.info("Event received!!")
return empty_pb2.Empty()
# Create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
import os
import time
import grpc
from concurrent import futures
from dapr.proto import appcallback_service_v1, appcallback_v1
from google.protobuf.any_pb2 import Any
# Our server methods
class AppCallback(appcallback_service_v1.AppCallbackServicer):
def ListInputBindings(self, request, context):
return appcallback_v1.ListInputBindingsResponse(bindings=['kafkaBinding'])
def OnBindingEvent(self, request, context):
print(request.name, flush=True)
print(request.data, flush=True)
# Return response to caller
return appcallback_v1.BindingEventResponse(store_name='Non Persistant', to='nothing', data=request.data)
# Create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
appcallback_service_v1.add_AppCallbackServicer_to_server(AppCallback(), server)
# Start the gRPC server
from google.protobuf import empty_pb2
from google.protobuf.message import Message as GrpcMessage
from dapr.proto import appcallback_service_v1, common_v1, appcallback_v1
from dapr.clients.base import DEFAULT_JSON_CONTENT_TYPE
from dapr.clients.grpc._request import InvokeServiceRequest, BindingRequest
from dapr.clients.grpc._response import InvokeServiceResponse
InvokeMethodCallable = Callable[[InvokeServiceRequest], Union[str, bytes, InvokeServiceResponse]]
TopicSubscribeCallable = Callable[[v1.Event], None]
BindingCallable = Callable[[BindingRequest], None]
class _CallbackServicer(appcallback_service_v1.AppCallbackServicer):
"""The implementation of AppCallback Server.
This internal class implements application server and provides helpers to register
method, topic, and input bindings. It implements the routing handling logic to route
mulitple methods, topics, and bindings.
:class:`App` provides useful decorators to register method, topic, input bindings.
"""
def __init__(self):
self._invoke_method_map: Dict[str, InvokeMethodCallable] = {}
self._topic_map: Dict[str, TopicSubscribeCallable] = {}
self._binding_map: Dict[str, BindingCallable] = {}
self._registered_topics: List[str] = []
self._registered_bindings: List[str] = []