Example #1
0
        private static async Task <T> DeserializedResponse <T>(HttpResponseMessage httpResponse)
        {
            var httpContent = await httpResponse.Content.ReadAsStringAsync();

            try
            {
                httpResponse.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                var message = $"Ensure success status code failed.{Environment.NewLine}{Environment.NewLine}"
                              + $"Response:{Environment.NewLine}{httpContent}";
                throw new Exception(message, ex);
            }

            var deserializedResponse = ApiResponseSerializationHelper.DeserializeResponse <T>(httpContent);

            return(deserializedResponse);
        }
Example #2
0
        public async Task <HttpResponseMessage> HttpPost(string requestUri, object json = null)
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }

            var content = json != null?ApiResponseSerializationHelper.SerializeRequest(json) : null;

            var contentType = json != null ? "application/json" : null;

            using (var client = CreateHttpClient())
            {
                var postContent = new StringContent(content ?? string.Empty, Encoding.UTF8, contentType);

                Debug.WriteLine($"HttpClient POST '{requestUri}'.");

                return(await client.PostAsync(requestUri, postContent));
            }
        }