public void CepDetailsConverter_MustDeserializeAllFields()
        {
            //Given
            string cep            = "01001000";
            string address        = "Praça da Sé";
            string complement     = "lado ímpar";
            string neighbourhood  = "Sé";
            string city           = "São Paulo";
            string federativeUnit = "SP";
            string ibge           = "3550308";
            string gia            = "1004";
            string ddd            = "11";
            string siafi          = "7107";

            string json = new StringBuilder()
                          .Append("{")
                          .Append($@"""cep"":""{cep}"",")
                          .Append($@"""logradouro"":""{address}"",")
                          .Append($@"""complemento"":""{complement}"",")
                          .Append($@"""bairro"":""{neighbourhood}"",")
                          .Append($@"""localidade"":""{city}"",")
                          .Append($@"""uf"":""{federativeUnit}"",")
                          .Append($@"""ibge"":""{ibge}"",")
                          .Append($@"""gia"":""{gia}"",")
                          .Append($@"""ddd"":""{ddd}"",")
                          .Append($@"""siafi"":""{siafi}""")
                          .Append("}")
                          .ToString();

            CepDetailsConverter converter = new CepDetailsConverter();

            using JsonDocument jsonDocument = JsonDocument.Parse(json);
            IPlainJsonObject plainJsonObject = new PlainJsonObject(jsonDocument);

            //When
            CepDetails cepDetails = converter.FromJson(plainJsonObject);

            //Then
            cepDetails.Address.Should().Be(address);
            cepDetails.Cep.Should().Be(cep);
            cepDetails.City.Should().Be(city);
            cepDetails.Complement.Should().Be(complement);
            cepDetails.DDD.Should().Be(ddd);
            cepDetails.FederativeUnit.Should().Be(federativeUnit);
            cepDetails.GIA.Should().Be(gia);
            cepDetails.IBGE.Should().Be(ibge);
            cepDetails.Neighbourhood.Should().Be(neighbourhood);
            cepDetails.Siafi.Should().Be(siafi);
        }
예제 #2
0
        private static async Task CallViaCepClientFromDI(string cep)
        {
            using ServiceProvider serviceProvider = new ServiceCollection()
                                                    .AddViaCepClient(opt => opt.SetBaseAddress("http://viacep.com.br"))
                                                    .BuildServiceProvider();

            IViaCepClient viaCepClient = serviceProvider.GetRequiredService <IViaCepClient>();

            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(new Cep(cep));

            CepDetails cepDetails = response.Content;

            string json = JsonSerializer.Serialize(cepDetails, new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            });

            Console.WriteLine(json);
        }
예제 #3
0
        private static async Task CallViaCepClientDirectly(string cep)
        {
            HttpClient            httpClient   = new HttpClient();
            ViaCepClientOptions   options      = new ViaCepClientOptions();
            IViaCepRequestBuilder builder      = new ViaCepRequestBuilder(options);
            IRestClient           restClient   = new RestClient(httpClient);
            IViaCepClient         viaCepClient = new Client.ViaCepClient(restClient, builder);

            ResponseMessage <CepDetails> response = await viaCepClient.SendRequestAsync(new Cep(cep));

            CepDetails cepDetails = response.Content;

            string json = JsonSerializer.Serialize(cepDetails, new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            });

            Console.WriteLine(json);
        }