Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
add - Added the async version of functions
Browse files Browse the repository at this point in the history
We've added asynchronous functions.

---

We've added asynchronous version of the functions so that web developers can use this library.

---

Type: add
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 13, 2023
1 parent 2ba464c commit 47a9ec1
Showing 1 changed file with 92 additions and 23 deletions.
115 changes: 92 additions & 23 deletions ManagedWeatherMap/Forecast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.IO.Compression;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace ManagedWeatherMap.Core
Expand Down Expand Up @@ -68,36 +69,85 @@ public static ForecastInfo GetWeatherInfo(string CityName, string APIKey, UnitMe
/// <returns>A class containing properties of weather information</returns>
internal static ForecastInfo GetWeatherInfo(string WeatherURL, UnitMeasurement Unit = UnitMeasurement.Metric)
{
ForecastInfo WeatherInfo = new();
string WeatherData;
JToken WeatherToken;
Debug.WriteLine("Weather URL: {0} | Unit: {1}", WeatherURL, Unit);

// Deal with measurements
if (Unit == UnitMeasurement.Imperial)
{
WeatherURL += "&units=imperial";
}
else
{
WeatherURL += "&units=metric";
}

// Download and parse JSON data
WeatherData = WeatherDownloader.GetStringAsync(WeatherURL).Result;
WeatherToken = JToken.Parse(WeatherData);
return FinalizeInstallation(WeatherToken, Unit);
}

// Put needed data to the class
WeatherInfo.Weather = (WeatherCondition)WeatherToken.SelectToken("weather").First.SelectToken("id").ToObject(typeof(WeatherCondition));
WeatherInfo.Temperature = (double)WeatherToken.SelectToken("main").SelectToken("temp").ToObject(typeof(double));
WeatherInfo.FeelsLike = (double)WeatherToken.SelectToken("main").SelectToken("feels_like").ToObject(typeof(double));
WeatherInfo.Pressure = (double)WeatherToken.SelectToken("main").SelectToken("pressure").ToObject(typeof(double));
WeatherInfo.Humidity = (double)WeatherToken.SelectToken("main").SelectToken("humidity").ToObject(typeof(double));
WeatherInfo.WindSpeed = (double)WeatherToken.SelectToken("wind").SelectToken("speed").ToObject(typeof(double));
WeatherInfo.WindDirection = (double)WeatherToken.SelectToken("wind").SelectToken("deg").ToObject(typeof(double));
WeatherInfo.CityID = (long)WeatherToken.SelectToken("id").ToObject(typeof(long));
WeatherInfo.CityName = (string)WeatherToken.SelectToken("name").ToObject(typeof(string));
WeatherInfo.TemperatureMeasurement = Unit;
/// <summary>
/// Gets current weather info from OpenWeatherMap
/// </summary>
/// <param name="CityID">City ID</param>
/// <param name="APIKey">API key</param>
/// <returns>A class containing properties of weather information</returns>
public static async Task<ForecastInfo> GetWeatherInfoAsync(long CityID, string APIKey, UnitMeasurement Unit = UnitMeasurement.Metric)
{
string WeatherURL = $"http://api.openweathermap.org/data/2.5/weather?id={CityID}&appid={APIKey}";
return await GetWeatherInfoAsync(WeatherURL, Unit);
}

/// <summary>
/// Gets current weather info from OpenWeatherMap
/// </summary>
/// <param name="CityName">City name</param>
/// <param name="APIKey">API Key</param>
/// <returns>A class containing properties of weather information</returns>
public static async Task<ForecastInfo> GetWeatherInfoAsync(string CityName, string APIKey, UnitMeasurement Unit = UnitMeasurement.Metric)
{
string WeatherURL = $"http://api.openweathermap.org/data/2.5/weather?q={CityName}&appid={APIKey}";
return await GetWeatherInfoAsync(WeatherURL, Unit);
}

/// <summary>
/// Gets current weather info from OpenWeatherMap
/// </summary>
/// <param name="WeatherURL">An URL to the weather API request</param>
/// <returns>A class containing properties of weather information</returns>
internal static async Task<ForecastInfo> GetWeatherInfoAsync(string WeatherURL, UnitMeasurement Unit = UnitMeasurement.Metric)
{
string WeatherData;
JToken WeatherToken;
Debug.WriteLine("Weather URL: {0} | Unit: {1}", WeatherURL, Unit);

// Deal with measurements
if (Unit == UnitMeasurement.Imperial)
WeatherURL += "&units=imperial";
else
WeatherURL += "&units=metric";

// Download and parse JSON data
WeatherData = await WeatherDownloader.GetStringAsync(WeatherURL);
WeatherToken = JToken.Parse(WeatherData);
return FinalizeInstallation(WeatherToken, Unit);
}

internal static ForecastInfo FinalizeInstallation(JToken WeatherToken, UnitMeasurement Unit = UnitMeasurement.Metric)
{
ForecastInfo WeatherInfo = new()
{
// Put needed data to the class
Weather = (WeatherCondition)WeatherToken.SelectToken("weather").First.SelectToken("id").ToObject(typeof(WeatherCondition)),
Temperature = (double)WeatherToken.SelectToken("main").SelectToken("temp").ToObject(typeof(double)),
FeelsLike = (double)WeatherToken.SelectToken("main").SelectToken("feels_like").ToObject(typeof(double)),
Pressure = (double)WeatherToken.SelectToken("main").SelectToken("pressure").ToObject(typeof(double)),
Humidity = (double)WeatherToken.SelectToken("main").SelectToken("humidity").ToObject(typeof(double)),
WindSpeed = (double)WeatherToken.SelectToken("wind").SelectToken("speed").ToObject(typeof(double)),
WindDirection = (double)WeatherToken.SelectToken("wind").SelectToken("deg").ToObject(typeof(double)),
CityID = (long)WeatherToken.SelectToken("id").ToObject(typeof(long)),
CityName = (string)WeatherToken.SelectToken("name").ToObject(typeof(string)),
TemperatureMeasurement = Unit
};
return WeatherInfo;
}

Expand All @@ -107,18 +157,37 @@ internal static ForecastInfo GetWeatherInfo(string WeatherURL, UnitMeasurement U
public static Dictionary<long, string> ListAllCities()
{
string WeatherCityListURL = $"http://bulk.openweathermap.org/sample/city.list.json.gz";
GZipStream WeatherCityListData;
Stream WeatherCityListDataStream;
var WeatherCityListUncompressed = new List<byte>();
int WeatherCityListReadByte = 0;
JToken WeatherCityListToken;
var WeatherCityList = new Dictionary<long, string>();
Debug.WriteLine("Weather City List URL: {0}", WeatherCityListURL);

// Open the stream to the city list URL
WeatherCityListDataStream = WeatherDownloader.GetStreamAsync(WeatherCityListURL).Result;
return FinalizeCityList(WeatherCityListDataStream);
}

/// <summary>
/// Lists all the available cities
/// </summary>
public static async Task<Dictionary<long, string>> ListAllCitiesAsync()
{
string WeatherCityListURL = $"http://bulk.openweathermap.org/sample/city.list.json.gz";
Stream WeatherCityListDataStream;
Debug.WriteLine("Weather City List URL: {0}", WeatherCityListURL);

// Open the stream to the city list URL
WeatherCityListDataStream = await WeatherDownloader.GetStreamAsync(WeatherCityListURL);
return FinalizeCityList(WeatherCityListDataStream);
}

internal static Dictionary<long, string> FinalizeCityList(Stream WeatherCityListDataStream)
{
GZipStream WeatherCityListData;
var WeatherCityListUncompressed = new List<byte>();
int WeatherCityListReadByte = 0;
JToken WeatherCityListToken;
var WeatherCityList = new Dictionary<long, string>();

// Download and parse the JSON. Since the output is gzipped, we'll have to uncompress it using stream, since the city list
// Parse the weather list JSON. Since the output is gzipped, we'll have to uncompress it using stream, since the city list
// is large anyways. This saves you from downloading full 45+ MB of text.
WeatherCityListData = new GZipStream(WeatherCityListDataStream, CompressionMode.Decompress, false);
while (WeatherCityListReadByte != -1)
Expand All @@ -143,4 +212,4 @@ internal static ForecastInfo GetWeatherInfo(string WeatherURL, UnitMeasurement U
return WeatherCityList;
}
}
}
}

0 comments on commit 47a9ec1

Please sign in to comment.