summaryrefslogtreecommitdiff
path: root/src/weatherFetcher.zig
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2023-12-01 20:31:30 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2023-12-01 20:31:30 +0100
commit486628f42e5f9b4ab75f35995bce7b46e49995de (patch)
treeebd8881c544ba0b18f84a6a13a2c5b6d8c338297 /src/weatherFetcher.zig
parent9e6837a5fdb672dbf38b1315100a5dac7961b367 (diff)
parameterize locationHEADmaster
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);