This commit is contained in:
Mars 2024-06-13 17:11:47 -04:00
parent ee61c994ac
commit 60d4160033
Signed by: pupbrained
GPG key ID: EF82E8CA83FF158C
2 changed files with 17 additions and 17 deletions

View file

@ -29,12 +29,16 @@
else pkgs # TODO: Remove when fixed on darwin else pkgs # TODO: Remove when fixed on darwin
); );
[ [
curl
fmt fmt
glib glib
tomlplusplus tomlplusplus
yyjson yyjson
] ]
++ (
if !stdenv.isDarwin && system == "x86_64-linux"
then [pkgsStatic.curl]
else [pkgs.curl]
)
++ linuxPkgs ++ linuxPkgs
++ darwinPkgs; ++ darwinPkgs;

View file

@ -1,4 +1,5 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <fmt/core.h>
#include <rfl/json.hpp> #include <rfl/json.hpp>
#include <rfl/json/load.hpp> #include <rfl/json/load.hpp>
#include <util/result.h> #include <util/result.h>
@ -48,21 +49,17 @@ fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
return Ok(); return Ok();
} }
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* buffer) fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str) -> size_t {
-> size_t { size_t totalSize = size * nmemb;
size_t realsize = size * nmemb; str->append(static_cast<char*>(contents), totalSize);
return totalSize;
buffer->append(static_cast<char*>(contents), realsize);
return realsize;
} }
// Function to make API request // Function to make API request
fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> { fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
fmt::println("Making API request..."); fmt::println("Making API request to URL: {}", url);
CURL* curl = curl_easy_init(); CURL* curl = curl_easy_init();
std::string responseBuffer; std::string responseBuffer;
if (curl) { if (curl) {
@ -72,17 +69,16 @@ fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (res != CURLE_OK) if (res != CURLE_OK) {
return Error(fmt::format( return Error(fmt::format("Failed to perform cURL request: {}", curl_easy_strerror(res)));
"Failed to perform cURL request: {}", curl_easy_strerror(res) }
));
fmt::println("Received response from API. Response size: {}", responseBuffer.size());
fmt::println("Received response from API.");
// Parse the JSON response
WeatherOutput output = WeatherOutput output =
rfl::json::read<WeatherOutput>(responseBuffer).value(); rfl::json::read<WeatherOutput>(responseBuffer).value();
return Ok(output); return Ok(output); // Return an empty result for now
} }
return Error("Failed to initialize cURL."); return Error("Failed to initialize cURL.");