Exemple #1
0
        public async Task <T> PostAsync <T>(Request.Base request, CancellationTokenSource cancel = null)
            where T : new()
        {
            HttpResponseMessage responseMessage;
            var uri = new UriBuilder($"{BaseUrl}{request.Path}")
            {
                Query = request.Query
            };

            Logger?.LogDebug(uri.ToString());
            var content = new StringContent(
                JsonSerializer.Serialize(request.RequestBody, request.RequestBody.GetType(), jsonInputOptions), Encoding.UTF8, MEDIA_TYPE);

            try
            {
                responseMessage = await Http.PostAsync(uri.Uri, content, cancel == null?CancellationToken.None : cancel.Token);
            }
            catch (Exception e)
            {
                Logger?.LogError(e.Message);
                throw;
            }
            Logger?.LogDebug("{0}\n{1}", request.ToString(), responseMessage.ReasonPhrase);
            Logger?.LogTrace(content.ReadAsStringAsync().Result);
            if (responseMessage.IsSuccessStatusCode)
            {
                Logger?.LogTrace(responseMessage.Content.ReadAsStringAsync().Result);
                if (typeof(T).Equals(typeof(Response.Empty)))
                {
                    return(new T());
                }
                else
                {
                    try
                    {
                        T result = JsonSerializer.Deserialize <T>(
                            responseMessage.Content.ReadAsStringAsync().Result,
                            new JsonSerializerOptions
                        {
                            Converters = { new DateJsonConverter() }
                        });
                        return(result);
                    }
                    catch (Exception e)
                    {
                        Logger?.LogError(e.Message);
                        throw;
                    }
                }
            }
            else
            {
                var error = responseMessage.Content.ReadAsStringAsync().Result;
                Logger?.LogTrace(error);
                throw new Exception(error);
            }
        }
Exemple #2
0
        public async Task <T> GetAsync <T>(Request.Base request, CancellationTokenSource cancel = null)
        {
            HttpResponseMessage responseMessage;

            try
            {
                var uri = new UriBuilder($"{BaseUrl}{request.Path}")
                {
                    Query = request.Query
                };
                Logger?.LogDebug(uri.ToString());
                responseMessage = await Http.GetAsync(uri.Uri, cancel == null?CancellationToken.None : cancel.Token);
            }
            catch (Exception e)
            {
                Logger?.LogError(e.Message);
                throw;
            }
            Logger?.LogDebug("{0}\n{1}", request.ToString(), responseMessage.ReasonPhrase);
            if (responseMessage.IsSuccessStatusCode)
            {
                Logger?.LogTrace(responseMessage.Content.ReadAsStringAsync().Result);
                try
                {
                    T result = JsonSerializer.Deserialize <T>(
                        responseMessage.Content.ReadAsStringAsync().Result,
                        new JsonSerializerOptions
                    {
                        Converters = { new DateJsonConverter() }
                    });
                    return(result);
                }
                catch (Exception e)
                {
                    Logger?.LogError(e.Message);
                    throw;
                }
            }
            else
            {
                var resp = await responseMessage.Content.ReadAsStringAsync();

                Logger?.LogTrace(resp);
                throw new Exception(resp);
            }
        }
Exemple #3
0
        public async Task PutAsync(Request.Base request, CancellationTokenSource cancel = null)
        {
            HttpResponseMessage responseMessage;
            var uri = new UriBuilder($"{BaseUrl}{request.Path}")
            {
                Query = request.Query
            };

            Logger?.LogDebug(uri.ToString());
            var content = new StringContent(
                JsonSerializer.Serialize(request.RequestBody, request.RequestBody.GetType(), jsonInputOptions), Encoding.UTF8, MEDIA_TYPE);

            try
            {
                responseMessage = await Http.PutAsync(uri.Uri, content, cancel == null?CancellationToken.None : cancel.Token);
            }
            catch (Exception e)
            {
                Logger?.LogError(e.Message);
                throw;
            }
            Logger?.LogDebug("{0}\n{1}", request.ToString(), responseMessage.ReasonPhrase);
            Logger?.LogTrace(content.ReadAsStringAsync().Result);
        }