Context:
The Chicago Park District maintains weather sensors at beaches along Chicago’s Lake Michigan lakefront. These sensors generally capture the indicated measurements hourly while the sensors are in operation during the summer. Information about the API.
MeteoApi.cs
using System;
using Xunit;
using System.Reflection;
using Api.Client;
using System.Linq;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Text;
namespace Meteo.E2E.Test
{
public partial class Meteo
{
readonly string url = @"https://data.cityofchicago.org/resource/k7hf-8y75.json?";
public bool GetOakAllHttpDemo()
{
string stationName = "Oak Street Weather Station";
string station = $"station_name='{stationName}'";
string limit = @"&$limit=1000000";
ApiSettings endPoint = new ApiSettings()
{
BaseUrl = url + station + limit,
Route = String.Empty
};
var request = (HttpWebRequest)WebRequest.Create(endPoint.BaseUrl);
request.Method = "GET";
try
{
var response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
var meteoData = JsonConvert.DeserializeObject<MeteoRecord[]>(reader.ReadToEnd());
}
//ApiClient client = new ApiClient(endPoint);
//raw = (string)client.Get("");
//var list = JsonConvert.DeserializeObject<MeteoRecord[]>(raw);
//return list.All(x => x.station_name == stationName);
return true;
}
catch { return false; }
}
public bool GetOakAll()
{
string stationName = "Oak Street Weather Station";
string station = $"station_name='{stationName}'";
string limit = @"&$limit=1000000";
string raw = "";
ApiSettings endPoint = new ApiSettings()
{
BaseUrl = url + station + limit,
Route = String.Empty
};
try
{
ApiClient client = new ApiClient(endPoint);
raw = (string)client.Get("");
var list = JsonConvert.DeserializeObject<MeteoRecord[]>(raw);
return list.All(x => x.station_name == stationName);
}
catch { return false; }
}
public bool GetPage()
{
string timeStamp =
@"&$where=measurement_timestamp between '2019-01-01T00:00:00' and '2020-01-01T00:00:00'";
string limit = @"&$limit=10";
string offset = @"&$offset=10";
string stationName = "63rd Street Weather Station";
string station = $"station_name='{stationName}'";
ApiSettings endPoint = new ApiSettings()
{
BaseUrl = url + station + limit,
Route = String.Empty
};
string raw = String.Empty;
try
{
ApiClient client = new ApiClient(endPoint);
raw = (string)client.Get("");
var list1 = JsonConvert.DeserializeObject<MeteoRecord[]>(raw);
endPoint.BaseUrl = url + station + timeStamp + limit + offset;
client = new ApiClient(endPoint);
raw = (string)client.Get("");
var list2 = JsonConvert.DeserializeObject<MeteoRecord[]>(raw);
return !list1.Any(x => list2.Contains(x));
}
catch { return false; }
}
public bool GetApiErr()
{
string range = @"&$where=battery_life < full";
string stationName = "63rd Street Weather Station";
string station = $"station_name='{stationName}'";
ApiSettings endPoint = new ApiSettings()
{
BaseUrl = url + station + range,
Route = String.Empty
};
try
{
ApiClient client = new ApiClient(endPoint);
var raw = (string)client.Get("");
var list = JsonConvert.DeserializeObject<MeteoRecord[]>(String.Empty);
if (raw.Contains("Could not parse")) return false;
}
catch
{
return false;
}
return true;
}
}
}
Meteo.cs
namespace Meteo.E2E.Test
{
public partial class Meteo
{
[Fact] public void OakDemo() => Assert.True(GetOakAllHttpDemo());
[Fact] public void OakAll() => Assert.True(GetOakAll());
[Fact] public void Page63() => Assert.True(GetPage());
[Fact] public void Err63() => Assert.False(GetApiErr());
}
}
MeteoRecord.cs
using System;
using System.Collections.Generic;
namespace Meteo.E2E.Test
{
public class MeteoRecord
{
public string station_name { get; set; }
public DateTime measurement_timestamp { get; set; }
public string air_temperature { get; set; }
public string wet_bulb_temperature { get; set; }
public string humidity { get; set; }
public string rain_intensity { get; set; }
public string interval_rain { get; set; }
public string total_rain { get; set; }
public string precipitation_type { get; set; }
public string wind_direction { get; set; }
public string wind_speed { get; set; }
public string maximum_wind_speed { get; set; }
public string barometric_pressure { get; set; }
public string solar_radiation { get; set; }
public string heading { get; set; }
public string battery_life { get; set; }
public string measurement_timestamp_label { get; set; }
public string measurement_id { get; set; }
}
}
ApiClient.cs
using System;
using Newtonsoft.Json;
using RestSharp;
namespace Api.Client
{
public class ApiClient
{
protected ApiSettings _settings;
public ApiClient(ApiSettings settings)
{
_settings = settings;
}
public object Get(string lookup, Type responseType = null)
{
return ExecuteRequest(CreateRequestor(lookup), CreateGetRequest(), responseType);
}
protected object ExecuteRequest(RestClient requestor, RestRequest request, Type responseType = null)
{
var response = requestor.Execute(request);
var content = response.Content;
if (content == null) throw new ApplicationException(string.Format("ERROR: No response received from call to web API: {0}",
requestor.BaseUrl));
return responseType != null ? JsonConvert.DeserializeObject(content, responseType) : content;
}
protected RestClient CreateRequestor(string lookup = "")
{
return new RestClient()
{
BaseUrl = new Uri(string.Format("{0}{1}{2}", _settings.BaseUrl, _settings.Route, lookup)),
Timeout = _settings.GetTimeoutValue()
};
}
protected RestRequest CreateGetRequest()
{
return CreateRequest(Method.GET);
}
protected RestRequest CreateRequest(Method requestMethod)
{
var request = new RestRequest(requestMethod);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
return request;
}
}
}
ApiSettings.cs
using System;
namespace Api.Client
{
public class ApiSettings
{
protected const int DEFAULT_TIMEOUT = 60000;
public string BaseUrl { get; set; }
public string Route { get; set; }
public string Timeout { get; set; }
internal int GetTimeoutValue()
{
if (!Int32.TryParse(Timeout, out int timeoutValue))
return DEFAULT_TIMEOUT;
return timeoutValue;
}
}
}
Full Rest-Api call Solution posted to Git

