summaryrefslogtreecommitdiff
path: root/src/weatherFetcher.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/weatherFetcher.zig')
-rw-r--r--src/weatherFetcher.zig25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/weatherFetcher.zig b/src/weatherFetcher.zig
index be99d5c..42847f9 100644
--- a/src/weatherFetcher.zig
+++ b/src/weatherFetcher.zig
@@ -6,6 +6,10 @@ const expect = std.testing.expect;
const http = @import("./http.zig");
+pub const Location = struct {
+ lat: f64,
+ lon: f64,
+};
pub const DailyDataSchema = struct {
latitude: f64,
@@ -48,19 +52,22 @@ pub const DailyData = struct{
/// Gets Daily Data. Allocates and returns an DailyData struct that must be
/// .deinit() later
-pub fn getDailyData(allocator: std.mem.Allocator) !DailyData {
+pub fn getDailyData(allocator: std.mem.Allocator, location: Location) !DailyData {
const stream = try net.tcpConnectToHost(allocator, "api.open-meteo.com", 80);
defer stream.close();
const writer = stream.writer();
- try writer.writeAll( "GET /v1/forecast" ++
- "?latitude=52.52" ++
- "&longitude=13.41" ++
- "&hourly=temperature_2m" ++ " HTTP/1.1\r\n" ++
- "Host: api.open-meteo.com\r\n" ++
- "Connection: keep-alive\r\n" ++
- "Accept-Encoding: identity\r\n" ++
- "\r\n");
+ const request = try std.fmt.allocPrint(allocator,
+ "GET /v1/forecast" ++
+ "?latitude={d}" ++
+ "&longitude={d}" ++
+ "&hourly=temperature_2m" ++ " HTTP/1.1\r\n" ++
+ "Host: api.open-meteo.com\r\n" ++
+ "Connection: keep-alive\r\n" ++
+ "Accept-Encoding: identity\r\n" ++ "\r\n", .{location.lat, location.lon});
+ defer allocator.free(request);
+
+ try writer.writeAll(request);
const reader = stream.reader();
var response = try http.HttpResponseParser.init(allocator);