Пример #1
0
        public async Task <IHueResponse> GetResponseAsync(IHueRequest hueRequest)
        {
            if (hueRequest is IContainsCommand setsCommand)
            {
                setsCommand.Command.CompleteAddress = $"/api/{User}/{setsCommand.Command.Address}";
            }

            var uri = GetRequestUri(hueRequest.Address);

            LogTrace(@"Preparing {method}-request to ""{uri}"".", hueRequest.Method.Method, uri);
            var requestMessage = new HttpRequestMessage(hueRequest.Method, uri);


            if (hueRequest is IUploadable uploadableRequest)
            {
                var requestContent = uploadableRequest.GetRequestBody();
                LogTrace($"Preparing json to send: {Environment.NewLine}{requestContent}");
                requestMessage.Content = new StringContent(requestContent, Encoding.UTF8);
            }
            else
            {
                requestMessage.Content = new StringContent(string.Empty);
            }

            using (var client = GetClient())
                using (var response = await client.SendAsync(requestMessage))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var responseString = await response.Content.ReadAsStringAsync();

                        LogTrace(responseString);
                        return(hueRequest.GetResponse(responseString));
                    }

                    LogError($"Error getting response from hub, HttpStatus {response.StatusCode}");
                    LogDebug(await response.Content.ReadAsStringAsync());
                    throw new HttpRequestException();
                }
        }