Weather: code refactor

This commit is contained in:
李通洲
2023-09-13 18:31:22 +08:00
parent f5ad07a87b
commit 63c29b10fe
4 changed files with 55 additions and 33 deletions
+38
View File
@@ -0,0 +1,38 @@
#include "weather.h"
static FFNetworkingState state;
static int status = -1;
void ffPrepareWeather(FFWeatherOptions* options)
{
if (status != -1)
{
fputs("Error: this module can only be used once due to internal limitations\n", stderr);
exit(1);
}
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/");
if (options->location.length)
ffStrbufAppend(&path, &options->location);
ffStrbufAppendS(&path, "?format=");
ffStrbufAppend(&path, &options->outputFormat);
status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n");
}
const char* ffDetectWeather(FFWeatherOptions* options, FFstrbuf* result)
{
if(status == -1)
ffPrepareWeather(options);
if(status == 0)
return "Failed to connect to 'wttr.in'";
ffStrbufEnsureFree(result, 4095);
bool success = ffNetworkingRecvHttpResponse(&state, result, options->timeout);
if (success) ffStrbufSubstrAfterFirstS(result, "\r\n\r\n");
if(!success || result->length == 0)
return "Failed to receive the server response";
return NULL;
}
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#ifndef FF_INCLUDED_detection_weather_weather
#define FF_INCLUDED_detection_weather_weather
#include "common/networking.h"
void ffPrepareWeather(FFWeatherOptions* options);
const char* ffDetectWeather(FFWeatherOptions* options, FFstrbuf* result);
#endif
+5 -33
View File
@@ -1,50 +1,22 @@
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/networking.h"
#include "detection/weather/weather.h"
#include "modules/weather/weather.h"
#include "util/stringUtils.h"
#define FF_WEATHER_NUM_FORMAT_ARGS 1
static FFNetworkingState state;
static int status = -1;
void ffPrepareWeather(FFWeatherOptions* options)
{
if (status != -1)
{
fputs("Error: " FF_WEATHER_MODULE_NAME " can only be used once due to internal limitations\n", stderr);
exit(1);
}
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/");
if (options->location.length)
ffStrbufAppend(&path, &options->location);
ffStrbufAppendS(&path, "?format=");
ffStrbufAppend(&path, &options->outputFormat);
status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n");
}
void ffPrintWeather(FFWeatherOptions* options)
{
if(status == -1)
ffPrepareWeather(options);
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
const char* error = ffDetectWeather(options, &result);
if(status == 0)
if(error)
{
ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, "Failed to connect to 'wttr.in'");
ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, error);
return;
}
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateA(4096);
bool success = ffNetworkingRecvHttpResponse(&state, &result, options->timeout);
if (success) ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
if(!success || result.length == 0)
{
ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, "Failed to receive the server response");
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{