public async Task <Respuesta <TBody> > DeleteAsync <TBody>(Peticion <T> peticion)
        {
            Respuesta <TBody> respuesta = new Respuesta <TBody>();

            setHeaders(peticion);
            try
            {
                var json     = JsonSerializer.Serialize <T>(peticion.Body);
                var response = await _client.DeleteAsync(peticion.ResolverRequestURL());

                respuesta.HttpStatus = (int)response.StatusCode;
                response.EnsureSuccessStatusCode();
                var msg = await response.Content.ReadAsStringAsync();

                foreach (var header in response.Headers)
                {
                    _logger.LogDebug($"{header.Key} : {string.Join(" ", header.Value)}");
                    respuesta.Headers.Add(header.Key, string.Join(" ", header.Value));
                }

                respuesta.Body = await JsonSerializer.DeserializeAsync <TBody>(await response.Content?.ReadAsStreamAsync());
            }
            catch (HttpRequestException ex)
            {
                _logger.LogError($"Message :{ex.Message}");
                respuesta.Mensaje = ex.Message;
            }
            catch (FormatException ex)
            {
                _logger.LogError($"Message :{ex.Message}");
                respuesta.Mensaje = "Petición inválida";
            }
            catch (JsonException ex)
            {
                _logger.LogError($"Message :{ex.Message}");
                respuesta.Mensaje = "No se pudo obtener el cuerpo de la respuesta";
            }

            return(respuesta);
        }
Beispiel #2
0
        public void ShouldBuildRequestPathWithQueryParamsFromPeticion()
        {
            string           basePath = "https://localhost:5001";
            string           endpoint = "tareas/{0}/{1}/{2}";
            Peticion <Tarea> peticion = new Peticion <Tarea>($"{basePath}/{endpoint}");

            peticion.PathVariables.Add("var1");
            peticion.PathVariables.Add("var2");
            peticion.PathVariables.Add("var3");

            peticion.Params.Add("param1", "string1");
            peticion.Params.Add("param2", "string2");

            Assert.Equal("https://localhost:5001/tareas/var1/var2/var3?param1=string1&param2=string2", peticion.ResolverRequestURL());
        }