1 year ago

#374591

test-img

Brsn

Not sure why Open Weather API C# Call is not working

I am having difficulty replacing the Dark Sky API with Open weather. GPS Location works and is received through another azure function.The Dark Sky Api call still works too. It might be something little but I'm not sure what's wrong. The only significant difference from the Dark Sky Api is the data model. Yet, Postman keeps responding with null values.

Postman response:

{
    "id": 0,
    "main": null,
    "description": null,
    "icon": null
}

public interface IWeatherService
{
    Task<Weather> GetWeatherAsync(double latitude, double longitude);
}

public class WeatherService : IWeatherService
{
    private readonly HttpClient _client;
    private readonly ILogger<IWeatherService> _logger;

    public WeatherService(HttpClient client, ILogger<IWeatherService> logger)
    {
        _client = client;
        _client.BaseAddress = new Uri("https://api.openweathermap.org/data/2.5/weather?");
        _client.Timeout = TimeSpan.FromSeconds(10);

        _logger = logger;
    }

    public async Task<Weather> GetWeatherAsync(double latitude, double longitude)
    {
        Weather retval = null;

        try
        {
            if (latitude.Equals(0) || longitude.Equals(0)) 
                 return null;

            var key = "HIDDEN";

            // var API = Environment.GetEnvironmentVariable("WeatherKey");

            var exclude = "minutely,hourly,daily,alerts";
            var units = "imperial";
            var requestUrl = $"&lat={latitude}&lon={longitude}&exclude={exclude}&units={units}&appid={key}";
            var response = await _client.GetAsync(requestUrl);
            var result = response.Content.ReadAsStringAsync().Result;
                
            var weather = JsonConvert.DeserializeObject<Weather>(result);

            retval = weather;
        //}

        //handler.Dispose();
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, $"Error in {nameof(GetWeatherAsync)}");
    }

    return retval;
}

The new model:

namespace WeatherWebAPI.Models
{
        using Newtonsoft.Json;yy

        public partial class Temperatures
        {
            [JsonProperty("coord")]
            public Coord Coord { get; set; }

            [JsonProperty("weather")]
            public Weather[] Weather { get; set; }

            [JsonProperty("base")]
            public string Base { get; set; }

            [JsonProperty("main")]
            public Main Main { get; set; }

            [JsonProperty("visibility")]
            public long Visibility { get; set; }

            [JsonProperty("wind")]
            public Wind Wind { get; set; }

            [JsonProperty("clouds")]
            public Clouds Clouds { get; set; }

            [JsonProperty("dt")]
            public long Dt { get; set; }

            [JsonProperty("sys")]
            public Sys Sys { get; set; }

            [JsonProperty("timezone")]
            public long Timezone { get; set; }

            [JsonProperty("id")]
            public long Id { get; set; }

            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("cod")]
            public long Cod { get; set; }
        }

        public partial class Clouds
        {
            [JsonProperty("all")]
            public long All { get; set; }
        }

        public partial class Coord
        {
            [JsonProperty("lon")]
            public double Lon { get; set; }

            [JsonProperty("lat")]
            public double Lat { get; set; }
        }

        public partial class Main
        {
            [JsonProperty("temp")]
            public double Temp { get; set; }

            [JsonProperty("feels_like")]
            public double FeelsLike { get; set; }

            [JsonProperty("temp_min")]
            public double TempMin { get; set; }

            [JsonProperty("temp_max")]
            public double TempMax { get; set; }

            [JsonProperty("pressure")]
            public long Pressure { get; set; }

            [JsonProperty("humidity")]
            public long Humidity { get; set; }
        }

        public partial class Sys
        {
            [JsonProperty("type")]
            public long Type { get; set; }

            [JsonProperty("id")]
            public long Id { get; set; }

            [JsonProperty("message")]
            public double Message { get; set; }

            [JsonProperty("country")]
            public string Country { get; set; }

            [JsonProperty("sunrise")]
            public long Sunrise { get; set; }

            [JsonProperty("sunset")]
            public long Sunset { get; set; }
        }

        public partial class Weather
        {
            [JsonProperty("id")]
            public long Id { get; set; }

            [JsonProperty("main")]
            public string Main { get; set; }

            [JsonProperty("description")]
            public string Description { get; set; }

            [JsonProperty("icon")]
            public string Icon { get; set; }
        }

        public partial class Wind
        {
            [JsonProperty("speed")]
            public double Speed { get; set; }

            [JsonProperty("deg")]
            public long Deg { get; set; }
        }
    }

The Old Model:

namespace WeatherWebAPI.Models
{
#pragma warning disable IDE1006 // Naming Styles

    public class Response
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
        public string timezone { get; set; }
        public DataPoint currently { get; set; }
    }

    public class DataPoint
    {
        public double apparentTemperatureHigh { get; set; }
        public string apparentTemperatureHighTime { get; set; }
        public double apparentTemperatureLow { get; set; }
        public string apparentTemperatureLowTime { get; set; }
        public double apparentTemperatureMax { get; set; }
        public string apparentTemperatureMaxTime { get; set; }
        public double apparentTemperatureMin { get; set; }
        public string apparentTemperatureMinTime { get; set; }
        public long time { get; set; }
        public string summary { get; set; }
        public string icon { get; set; }
        public string sunriseTime { get; set; }
        public string sunsetTime { get; set; }
        public double nearestStormDistance { get; set; }
        public double nearestStormBearing { get; set; }
        public double precipIntensity { get; set; }
        public double precipIntensityMax { get; set; }
        public string precipIntensityMaxTime { get; set; }
        public string precipIntensityError { get; set; }
        public double preciProbability { get; set; }
        public string precipType { get; set; }
        public double temperature { get; set; }
        public double temperatureHigh { get; set; }
        public string temperatureHighTime { get; set; }
        public double temperatureLow { get; set; }
        public string temperatureLowTime { get; set; }
        public double temperatureMax { get; set; }
        public string temperatureMaxTime { get; set; }
        public double temperatureMin { get; set; }
        public string temperatureMinTime { get; set; }
        public double apparentTemperature { get; set; }
        public double dewPoint { get; set; }
        public double humidity { get; set; }
        public double pressure { get; set; }
        public double windSpeed { get; set; }
        public double windGust { get; set; }
        public string windGustTime { get; set; }
        public double windBearing { get; set; }
        public double cloudCover { get; set; }
        public double uvIndex { get; set; }
        public double uvIndexTime { get; set; }
        public double visibility { get; set; }
        public double ozone { get; set; }
        public double moonPhase { get; set; }
    }

#pragma warning restore IDE1006 // Naming Styles

}

c#

.net

openweathermap

darksky

0 Answers

Your Answer

Accepted video resources