Esempio n. 1
0
        public async Task <TModel> RestSendToUrl <TModel, TDto>(HttpMethod httpMethod, string endpointUrl, string route = null, CancellationToken cancelToken = default) where TModel : class
        {
            if (string.IsNullOrWhiteSpace(endpointUrl))
            {
                throw new ArgumentException($"'{nameof(endpointUrl)}' cannot be null or whitespace.", nameof(endpointUrl));
            }

            try
            {
                await AddAuthHeaderAsync();

                var                url            = $"{endpointUrl}{route}";
                StringContent      httpcontent    = null;
                HttpRequestMessage requestMessage = new HttpRequestMessage {
                    Method = httpMethod, Content = httpcontent, RequestUri = new Uri(url)
                };
                var apiData = await HttpClientBase.SendAsync(requestMessage, cancelToken).ConfigureAwait(false);

                if (apiData.IsSuccessStatusCode)
                {
#if DEBUG
                    var content = await apiData.Content.ReadAsStringAsync();
#endif
                    var stream = await apiData.Content.ReadAsStreamAsync();

                    if (stream.Length == 0)
                    {
                        return(null);
                    }
                    var ser = await JsonSerializer.DeserializeAsync <TDto>(stream, _options, cancelToken);

                    if (ser == null)
                    {
                        return(null);
                    }

                    var result = Mapper.Map <TModel>(ser);
                    var con    = await ConvertModelToLocalTimeBase(result);

                    return(con);
                }
                else
                {
                    throw new Exception(GetErrorText(apiData));
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, cancelToken);
                return(null);
            }
        }
Esempio n. 2
0
        public async Task <TModel> RestSend <TModel, TDto, TParam>(HttpMethod httpMethod, TParam parameter, string route, CancellationToken cancelToken = default) where TModel : class
        {
            //if (string.IsNullOrWhiteSpace(route))
            //    throw new ArgumentException($"'{nameof(route)}' cannot be null or whitespace.", nameof(route));

            if (parameter == null)
            {
                throw new ArgumentException($"'{nameof(parameter)}' cannot be null", nameof(parameter));
            }

            try
            {
                await AddAuthHeaderAsync();

                var url         = $"{ApiPart}{route}";
                var httpcontent = new StringContent(JsonSerializer.Serialize(parameter, _options), Encoding.UTF8, "application/json");
                HttpRequestMessage requestMessage = new HttpRequestMessage {
                    Method = httpMethod, Content = httpcontent, RequestUri = new Uri(HttpClientBase.BaseAddress + url)
                };
                var apiData = await HttpClientBase.SendAsync(requestMessage, cancelToken).ConfigureAwait(false);

                if (apiData.IsSuccessStatusCode)
                {
                    var stream = await apiData.Content.ReadAsStreamAsync();

                    if (stream.Length == 0)
                    {
                        return(null);
                    }
                    var ser = await JsonSerializer.DeserializeAsync <TDto>(stream, _options, cancelToken);

                    if (ser == null)
                    {
                        return(null);
                    }

                    var result = Mapper.Map <TModel>(ser);
                    var con    = await ConvertModelToLocalTimeBase(result);

                    return(con);
                }
                else
                {
                    throw new Exception(GetErrorText(apiData));
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, cancelToken);
                return(null);
            }
        }