0

I have a project to do that requires the calculation of distances. For that, i'm using the Nominatim Library in order to get coordinates with postal codes. However, I made an exception that would be thrown if the postal code was invalid and it keeps happening, even though they're real portuguese postal codes.

here is the code used to verify if the postal code is valid:

private static bool ValidPostalCode(string postalCode)
        {
            RestResponse response = EnviaRequesicao(codigoPostal);

            if (response.IsSuccessful)
            {                
                JArray resultes = JArray.Parse(response.Content);

                
                return resultes.Count > 0;
            }

            else
                return false;
        }

and in the main i'm typing this:

Morada morada = new Morada("4770-713",Distrito.Braga );
morada.Rua = "Rua das fontainhas";
morada.NumeroPorta = 5;
morada.Cidade = "Joane";
Console.WriteLine($"{morada.Rua}, {morada.CodigoPostal} {morada.Cidade} ( {morada.Coordenadas} )");

the exception is thrown in the class's constructor:

public Morada(string codigoPostal, Distrito distrito)
        {
            bool valido = CodigoPostalValido(codigoPostal);

            if (valido)
            {
                this.CodigoPostal = codigoPostal;
                this.Coordenadas = ObterCoordenadas(codigoPostal);
            }
            else
                throw new CodigoPostalInvalidoException("RNCCI.Modelos.Morada.Morada");

(...)
}

I tried changing the postal code to only the first 4 numbers, but it also doesn't work.

/EDIT/ I did a debug and the problem is in the EnviaRequesicao method, that sends the request.

private static RestResponse EnviaRequesicao(string postalCode)
        {
            //cria o link que usa o codigo postal
            string requesitoUrl = $"https://nominatim.openstreetmap.org/search?format=json&limit=1&postalcode={postalCode}";

            //cria o cliente com o link do requesito
            RestClient cliente = new RestClient(requesitoUrl);

            //cria um objeto de requesito
            RestRequest requesito = new RestRequest("GET");

            RestResponse resposta = cliente.Execute(requesito);
            return resposta;   
        }

resposta has the statusCode of NotFound, even though the URL exists...

xaloftal
  • 1
  • 1

0 Answers0