Esempio n. 1
0
 public static StringContent ToHttpContent(this object obj)
 {
     return(new StringContent(
                JsonForCastle.SerializeObject(obj),
                Encoding.UTF8,
                "application/json"));
 }
Esempio n. 2
0
        public void Should_not_deserialize_with_json_property_if_not_appropriate_type(DeviceItem obj)
        {
            var json   = JsonForCastle.SerializeObject(obj);
            var result = JsonForCastle.DeserializeObject <DeviceItem>(json);

            result.Should().NotBeNull();
        }
Esempio n. 3
0
        public void Should_deserialize_with_json_property_if_appropriate_type(IHasJson obj)
        {
            var json   = JsonForCastle.SerializeObject(obj);
            var result = JsonForCastle.DeserializeObject <Device>(json);

            result.Internal.Should().NotBeNull();
        }
Esempio n. 4
0
        public void Should_serialize_client_id_to_false_if_empty(string value, string expected)
        {
            var obj = new RequestContext()
            {
                ClientId = value
            };

            var result = JsonForCastle.SerializeObject(obj);

            result.Should().Contain(expected);
        }
Esempio n. 5
0
        public void Should_serialize_header_to_truebool_if_truestring(
            string name,
            string value,
            string expected)
        {
            var obj = new RequestContext()
            {
                Headers = new Dictionary <string, string>()
                {
                    [name] = value
                }
            };

            var result = JsonForCastle.SerializeObject(obj);

            result.Should().Contain(expected);
        }
        private async Task <TResponse> SendRequest <TResponse>(HttpMethod method, string endpoint, object payload = null)
            where TResponse : class, new()
        {
            var jsonContent = payload != null?payload.ToHttpContent() : null;

            var requestMessage = new HttpRequestMessage(method, endpoint)
            {
                Content = jsonContent
            };

            _logger.Info(() => string.Concat(
                             "Request",
                             Environment.NewLine,
                             requestMessage,
                             Environment.NewLine,
                             payload != null ? JsonForCastle.SerializeObject(payload) : ""
                             ));

            try
            {
                var response = await _httpClient.SendAsync(requestMessage);

                var content = response.Content != null ? await response.Content.ReadAsStringAsync() : "";

                _logger.Info(() => string.Concat(
                                 "Response",
                                 Environment.NewLine,
                                 response,
                                 Environment.NewLine,
                                 content
                                 ));

                if (response.IsSuccessStatusCode)
                {
                    return(JsonForCastle.DeserializeObject <TResponse>(content));
                }

                throw await response.ToCastleException(requestMessage.RequestUri.AbsoluteUri);
            }
            catch (OperationCanceledException)
            {
                throw new CastleTimeoutException(
                          requestMessage.RequestUri.AbsoluteUri,
                          (int)_httpClient.Timeout.TotalMilliseconds);
            }
        }