/// <summary> /// Busca todos os dados relacionado com o Cep. /// </summary> /// <param name="numCep">Cep do endereço.</param> /// <returns></returns> public Cep GetCep(string numCep) { int i = new Random().Next(1); HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", arrayTokens[i]); HttpResponseMessage response = httpClient.GetAsync(string.Format(urlCepAbertoApi, numCep)).Result; try { if (response.IsSuccessStatusCode) { Cep dadosCep = JsonConvert.DeserializeObject <Cep>(response.Content.ReadAsStringAsync().Result); dadosCep.cidade.nome = _nameService.GetMunicipioByIbgeId(int.Parse(dadosCep.cidade.ibge)).nome ?? dadosCep.cidade.nome; return(dadosCep); } else { throw new Exception("Erro de autorizacao ao buscar pelo cep"); } } catch (Exception e) { throw new Exception("Verifique o CEP e tente novamente!"); } }
public async Task <IActionResult> Get(int id) { if (_memoryCache.TryGetValue(id, out object cidadeData)) { return(Ok(cidadeData)); } else { var cidadeDados = _searchCity.GetMunicipioByIbgeId(id); if (cidadeDados != null) { var memoryCacheEntryOptions = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(3600), SlidingExpiration = TimeSpan.FromSeconds(1200) }; _memoryCache.Set(cidadeDados.id, cidadeDados, memoryCacheEntryOptions); return(Ok(cidadeDados)); } else { return(NotFound(new { message = "Cidade não encontrada" })); } } }