示例#1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var tiempo = Stopwatch.StartNew();

            logger.LogInformation("Inicia Request");

            var response = await base.SendAsync(request, cancellationToken);

            logger.LogInformation($"Este proceso se hizo en {tiempo.ElapsedMilliseconds}ms");

            if (response.IsSuccessStatusCode)
            {
                //se obtiene contenido en formato string
                var contenido = await response.Content.ReadAsStringAsync();

                //serializarla en clase objeto
                var options = new JsonSerializerOptions {
                    PropertyNameCaseInsensitive = true
                };
                var resultado     = JsonSerializer.Deserialize <LibroModeloRemote>(contenido, options);
                var responseAutor = await autorRemote.GetAutor(resultado.AutorLibro ?? Guid.Empty);

                if (responseAutor.resultado)
                {
                    var objetoAutor = responseAutor.autor;
                    resultado.AutorData = objetoAutor;
                    var resultadoStr = JsonSerializer.Serialize(resultado);
                    response.Content = new StringContent(resultadoStr, System.Text.Encoding.UTF8, "application/json");
                }
            }



            return(response);
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancel)
        {
            var tiempo = Stopwatch.StartNew();

            _logger.LogInformation("Inicia el request");
            var respone = await base.SendAsync(request, cancel);

            if (respone.IsSuccessStatusCode)
            {
                var contenido = await respone.Content.ReadAsStringAsync();

                var options = new JsonSerializerOptions {
                    PropertyNameCaseInsensitive = true
                };
                var resultado     = JsonSerializer.Deserialize <LibroModeloRemote>(contenido, options);
                var responseAutor = await _autorRemote.GetAutor(resultado.AutorLibro ?? Guid.Empty); //Si se declara un Guid? para que acepte nulos, pero nos pide un Guid que no acepte nulos entonces se agrega eso apra forzar

                if (responseAutor.resultado)
                {
                    var oAutor = responseAutor.AutorRemote;
                    resultado.AutorData = oAutor;
                    var resultadoStr = JsonSerializer.Serialize(resultado);
                    respone.Content = new StringContent(resultadoStr, System.Text.Encoding.UTF8, "application/json");
                }
            }

            _logger.LogInformation($"Este Proceso se hizo en {tiempo.ElapsedMilliseconds}ms");
            return(respone);
        }