private async Task SendResponse(CloudConnectorApiContext apiContext)
        {
            try
            {
                using (var httpClient = new HttpClient())
                    using (var content = CreateContent(apiContext))
                    {
                        httpClient.DefaultRequestHeaders.Authorization = CreateAuthorizationHeader();

                        var result = await httpClient.PostAsync(_sendResponseUri, content);

                        if (result.IsSuccessStatusCode)
                        {
                            _log.Verbose("Sent response message to Cloud.");
                        }
                        else
                        {
                            _log.Warning($"Failed to send response message to Cloud (Error code: {result.StatusCode}).");
                        }
                    }
            }
            catch (Exception exception)
            {
                _log.Warning(exception, "Error while sending response message to cloud.");
            }
        }
        private CloudConnectorApiContext ProcessCloudMessage(CloudRequestMessage cloudMessage)
        {
            var apiContext = new CloudConnectorApiContext(cloudMessage);
            var eventArgs  = new ApiRequestReceivedEventArgs(apiContext);

            RequestReceived?.Invoke(this, eventArgs);

            if (!eventArgs.IsHandled)
            {
                apiContext.ResultCode = ApiResultCode.ActionNotSupported;
            }

            return(apiContext);
        }
        private static StringContent CreateContent(CloudConnectorApiContext apiContext)
        {
            var cloudMessage = new CloudResponseMessage();

            cloudMessage.Header.CorrelationId = apiContext.RequestMessage.Header.CorrelationId;
            cloudMessage.Response.ResultCode  = apiContext.ResultCode;
            cloudMessage.Response.Result      = apiContext.Result;

            var stringContent = new StringContent(JsonConvert.SerializeObject(cloudMessage));

            stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            stringContent.Headers.ContentEncoding.Add("utf-8");

            return(stringContent);
        }