-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherAPI.cs
More file actions
79 lines (62 loc) · 2.67 KB
/
WeatherAPI.cs
File metadata and controls
79 lines (62 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace sketchyWeatherApi;
public struct Location
{
public float lon { get; set; }
public float lat { get; set; }
public string name { get; set; }
public string country { get; set; }
public string state { get; set; }
public LocalNames local_names { get; set; }
}
public struct LocalNames
{
public string de { get; set; }
public string ja { get; set; }
}
public class WeatherApi(HttpClient client, float lat = 0, float lon = 0)
{
private readonly string _apiKey =
Environment.GetEnvironmentVariable("API_KEY_WEATHER") ?? throw new Exception("API_KEY not set");
private readonly string _baseUrl = "https://api.openweathermap.org/data/2.5/weather";
private float _lat = lat;
private float _lon = lon;
private readonly HttpClient _client = client;
public Location getLocation(string location)
{
_client.BaseAddress = new Uri("http://api.openweathermap.org/geo/1.0/direct");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("Accept", "application/json");
string query = $"?q={location}&appid={_apiKey}&limit=1";
HttpResponseMessage response = _client.GetAsync(query).Result;
response.EnsureSuccessStatusCode();
var result = System.Text.Json.JsonSerializer.Deserialize<List<Location>>(response.Content.ReadAsStringAsync().Result);
return result[0];
}
public string getWeather(float clat, float clon)
{
if (clat != 0) _lat = clat;
if (clon != 0) _lon = clon;
_client.BaseAddress = new Uri(_baseUrl);
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("Accept", "application/json");
string query = $"?lat={_lat}&lon={_lon}&appid={_apiKey}&units=metric";
HttpResponseMessage response = _client.GetAsync(query).Result;
response.EnsureSuccessStatusCode();
using var doc = System.Text.Json.JsonDocument.Parse(response.Content.ReadAsStringAsync().Result);
string name = "";
if (doc.RootElement.TryGetProperty("name", out var nameElement) &&
nameElement.ValueKind == System.Text.Json.JsonValueKind.String)
{
name = nameElement.GetString() ?? "";
}
double temp = 0;
if (doc.RootElement.TryGetProperty("main", out var mainElement) &&
mainElement.TryGetProperty("temp", out var tempElement) &&
tempElement.ValueKind == System.Text.Json.JsonValueKind.Number)
{
temp = tempElement.GetDouble();
}
var result = new { name, temp };
return System.Text.Json.JsonSerializer.Serialize(result);
}
}