public async Task <City> Add(CityDTO data) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); string baseURL = Configuration.GetSection("WeatherForecastApi:BaseURL").Value; string key = Configuration.GetSection("WeatherForecastApi:ApiKey").Value; HttpResponseMessage response = await client.GetAsync( baseURL + "/weather?" + $"APPID={key}&" + $"q={data.Name}"); response.EnsureSuccessStatusCode(); string bodyResponse = await response.Content.ReadAsStringAsync(); WeatherForecastCityDTO result = JsonConvert.DeserializeObject <WeatherForecastCityDTO>(bodyResponse); Boolean cityExists = await CityRepository.FindByApiCityCode(result.Id) != null; if (cityExists) { throw new Exception("Cidade já cadastrada!"); } var newCity = new City() { Name = data.Name, ApiCityCode = result.Id }; await CityRepository.Add(newCity); return(newCity); } }