C#
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Get(double latitude, double longitude)
{
// Create a HttpClient instance
var client = _clientFactory.CreateClient();
// Make a GET request to the external API
var response = await client.GetAsync($"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true");
// If the request is successful
if (response.IsSuccessStatusCode)
{
// Read the response content as a JSON string
var json = await response.Content.ReadAsStringAsync();
// Deserialize the JSON string into a WeatherForecastClass object
var weatherForecast = JsonSerializer.Deserialize<WeatherForecastClass>(json);
// Return the weather forecast with a 200 OK status
return Ok(weatherForecast);
}
// If the request is not successful, log an error and return the status code from the external API
_logger.LogError("Failed to retrieve weather forecast. Status Code: {ResponseStatusCode}", response.StatusCode);
return StatusCode((int)response.StatusCode);
}
}