Exemple #1
0
        private CloudMessage InvokeRawRequest(CloudMessage requestMessage)
        {
            try
            {
                var jsonRequestPayload = Encoding.UTF8.GetString(requestMessage.Payload);
                var jsonRequest        = JObject.Parse(jsonRequestPayload);
                var pythonDictionary   = PythonConvert.ToPythonDictionary(jsonRequest);

                CloudMessageHandler cloudMessageHandler;
                lock (_rawMessageHandlers)
                {
                    if (!_rawMessageHandlers.TryGetValue(pythonDictionary.GetValueOr("type", string.Empty), out cloudMessageHandler))
                    {
                        throw new NotSupportedException("RAW message type handler not supported.");
                    }
                }

                var responseContent = cloudMessageHandler.Invoke(pythonDictionary);

                var jsonResponse = PythonConvert.FromPythonToJson(responseContent);

                return(new CloudMessage()
                {
                    CorrelationId = requestMessage.CorrelationId,
                    Payload = Encoding.UTF8.GetBytes(jsonResponse.ToString())
                });
            }
            catch (Exception exception)
            {
                return(_cloudMessageFactory.Create(exception));
            }
        }
Exemple #2
0
        CloudMessage InvokeRawRequest(CloudMessage requestMessage)
        {
            try
            {
                var jsonRequestPayload = Encoding.UTF8.GetString(requestMessage.Payload);
                var invokeParameter    = JsonConvert.DeserializeObject <IDictionary <object, object> >(jsonRequestPayload);

                if (!invokeParameter.TryGetValue("type", out var handlerType))
                {
                    throw new NotSupportedException("Mandatory key 'type' not found in request parameter.");
                }

                RawCloudMessageHandler cloudMessageHandler;
                lock (_rawMessageHandlers)
                {
                    if (!_rawMessageHandlers.TryGetValue(Convert.ToString(handlerType), out cloudMessageHandler))
                    {
                        throw new NotSupportedException($"RAW message handler '{handlerType}' not supported.");
                    }
                }

                var invokeResult = cloudMessageHandler.Invoke(invokeParameter);

                var jsonResponse = JsonConvert.SerializeObject(invokeResult);

                return(new CloudMessage()
                {
                    CorrelationId = requestMessage.CorrelationId,
                    Payload = Encoding.UTF8.GetBytes(jsonResponse)
                });
            }
            catch (Exception exception)
            {
                return(_cloudMessageFactory.Create(exception));
            }
        }