Exemple #1
0
        public async Task <ServiceResponseViewModel> ExecuteGetWithJson <T>(string vMethod, object _object) where T : class
        {
            var vFullMethod = $"{_vBaseUrl}/{vMethod}";
            var para        = JsonConvert.SerializeObject(_object, Formatting.Indented);
            var content     = new StringContent(para, Encoding.UTF8, "application/json");
            var request     = new HttpRequestMessage {
                RequestUri = new Uri(vFullMethod, UriKind.Absolute),
                Method     = HttpMethod.Get,
                Content    = content
            };

            ////request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(para));
            //request.Content = new StringContent(para, Encoding.UTF8, "application/json");
            //request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var vResponse     = _vHttpClient.SendAsync(request).GetAwaiter().GetResult();
            var vResponseJson = await vResponse.Content.ReadAsStringAsync().ConfigureAwait(false);



            var vResult = new ServiceResponseViewModel {
                Content           = JsonConvert.DeserializeObject <T>(vResponseJson),
                Status            = vResponse.StatusCode,
                StatusDescription = vResponse.ReasonPhrase
            };

            return(vResult);
        }
Exemple #2
0
        public async Task <ServiceResponseViewModel> ExecutePost(string vMethod, string vNameParameter)
        {
            //var para = JsonConvert.SerializeObject(_parameters, Formatting.Indented);
            var para     = _vParameters[vNameParameter].ToString();
            var content  = new StringContent(para, Encoding.UTF8, "application/json");
            var response = await _vHttpClient.PostAsync(new Uri($"{_vBaseUrl}/{vMethod}", UriKind.Absolute), content).ConfigureAwait(false);

            var result = new ServiceResponseViewModel {
                Status            = response.StatusCode,
                StatusDescription = response.ReasonPhrase,
                Content           = response.IsSuccessStatusCode
            };

            return(result);
        }
Exemple #3
0
        public async Task <ServiceResponseViewModel> ExecutePost(string method, object _object)
        {
            //var para = JsonConvert.SerializeObject(_object);
            var para         = JsonConvert.SerializeObject(_object, Formatting.Indented);
            var content      = new StringContent(para, Encoding.UTF8, "application/json");
            var response     = _vHttpClient.PostAsync(new Uri($"{_vBaseUrl}/{method}", UriKind.Absolute), content).GetAwaiter().GetResult();
            var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var result = new ServiceResponseViewModel {
                Content           = JsonConvert.DeserializeObject(responseJson),
                Status            = response.StatusCode,
                StatusDescription = response.ReasonPhrase
            };

            return(result);
        }
Exemple #4
0
        public async Task <ServiceResponseViewModel> ExecuteGet <T>(string method, string id) where T : class
        {
            var fullMethod = $"{_vBaseUrl}/{method}/{id}";
            //if (_parameters.Any()) {
            //    fullMethod += "?";
            //    fullMethod = _parameters.Aggregate(fullMethod,
            //        (current, parameter) => current + $"{parameter.Key}={parameter.Value}&");
            //    fullMethod = fullMethod.Substring(0, fullMethod.Length - 1);
            //}
            var response = await _vHttpClient.GetAsync(fullMethod).ConfigureAwait(false);

            var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var result = new ServiceResponseViewModel {
                Content           = JsonConvert.DeserializeObject <T>(responseJson),
                Status            = response.StatusCode,
                StatusDescription = response.ReasonPhrase
            };

            return(result);
        }
        public static void ConfigureExceptionHandler(this IApplicationBuilder app, ILogger logger)
        {
            app.UseExceptionHandler(appError =>
            {
                appError.Run(async context =>
                {
                    context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    context.Response.ContentType = "application/json";

                    var contextFeature = context.Features.Get <IExceptionHandlerFeature>();
                    if (contextFeature != null)
                    {
                        logger.LogError($"Something went wrong: {contextFeature.Error}");
                        var errorVM = new ServiceResponseViewModel()
                        {
                            StatusCode = context.Response.StatusCode,
                            Message    = "Internal Server Error."
                        };
                        await context.Response.WriteAsync(JsonConvert.SerializeObject(errorVM)).ConfigureAwait(false);
                    }
                });
            });
        }
Exemple #6
0
        public async Task <ServiceResponseViewModel> ExecuteGet <T>(string vMethod) where T : class
        {
            var vFullMethod = $"{_vBaseUrl}/{vMethod}";

            if (_vParameters.Any())
            {
                vFullMethod += "?";
                vFullMethod  = _vParameters.Aggregate(vFullMethod,
                                                      (current, parameter) => current + $"{parameter.Key}={parameter.Value}&");
                vFullMethod = vFullMethod.Substring(0, vFullMethod.Length - 1);
            }
            var vResponse = await _vHttpClient.GetAsync(vFullMethod).ConfigureAwait(false);

            var vResponseJson = await vResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            var vResult = new ServiceResponseViewModel {
                Content           = JsonConvert.DeserializeObject <T>(vResponseJson),
                Status            = vResponse.StatusCode,
                StatusDescription = vResponse.ReasonPhrase
            };

            return(vResult);
        }
Exemple #7
0
        public async Task <ServiceResponseViewModel> ExecuteGetToString(string vMethod)
        {
            var vFullMethod = $"{_vBaseUrl}/{vMethod}";

            if (_vParameters.Any())
            {
                vFullMethod += "?";
                vFullMethod  = _vParameters.Aggregate(vFullMethod,
                                                      (current, parameter) => current + $"{parameter.Key}={parameter.Value}&");
                vFullMethod = vFullMethod.Substring(0, vFullMethod.Length - 1);
            }
            var vResponse = await _vHttpClient.GetAsync(vFullMethod).ConfigureAwait(false);

            var vResponseJson = await vResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            var result = new ServiceResponseViewModel();

            //         var result = new ClientResponseViewModel {
            //	Content = vResponseJson,
            //	Status = vResponse.StatusCode,
            //	StatusDescription = vResponse.ReasonPhrase
            //};
            return(result);
        }