59 lines
2.2 KiB
C#
59 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace PipefyProspUpdate.Services;
|
|
|
|
public class IBGEService
|
|
{
|
|
|
|
public static async Task<List<Models.Suggestion>> GetBrazilDataAsync()
|
|
{
|
|
string? apiUrl = App.config["IBGE_URL"];
|
|
List<Models.Suggestion> sugs = new();
|
|
|
|
using (HttpClient client = new())
|
|
{
|
|
HttpResponseMessage response = await client.GetAsync(apiUrl);
|
|
response.EnsureSuccessStatusCode();
|
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|
JArray data = JArray.Parse(responseBody);
|
|
|
|
foreach (var item in data)
|
|
{
|
|
try
|
|
{
|
|
string UF;
|
|
string Meso;
|
|
if (item["microrregiao"]!.Type == JTokenType.Null)
|
|
{
|
|
UF = (string)item["regiao-imediata"]!["regiao-intermediaria"]!["UF"]!["nome"]! + " (" + (string)item["regiao-imediata"]!["regiao-intermediaria"]!["UF"]!["sigla"]! + ")";
|
|
Meso = (string)item["regiao-imediata"]!["regiao-intermediaria"]!["nome"]!;
|
|
if ((string)item["nome"]! == "Boa Esperança do Norte") { Meso = "Norte Mato-grossense"; }
|
|
}
|
|
else
|
|
{
|
|
UF = (string)item["microrregiao"]!["mesorregiao"]!["UF"]!["nome"]! + " (" + (string)item["microrregiao"]!["mesorregiao"]!["UF"]!["sigla"]! + ")";
|
|
Meso = (string)item["microrregiao"]!["mesorregiao"]!["nome"]!;
|
|
}
|
|
|
|
sugs.Add(new()
|
|
{
|
|
ID = (string)item["id"]!,
|
|
UF = UF,
|
|
Meso = Meso,
|
|
City = (string)item["nome"]!
|
|
});
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show($"Erro ao carregar {item} do IBGE. Verifique a conexão com a internet ou o arquivo de configuração.");
|
|
}
|
|
}
|
|
}
|
|
|
|
return sugs;
|
|
}
|
|
} |