Unify http client creation

This commit is contained in:
suchmememanyskill
2024-03-06 22:32:49 +01:00
parent 9427381e05
commit f0cc211e30
8 changed files with 60 additions and 108 deletions

View File

@@ -2,11 +2,11 @@
#include "data_setup.h"
#include "lvgl.h"
#include "../conf/global_config.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <esp_task_wdt.h>
#include "macros_query.h"
#include <UrlEncode.h>
#include "http_client.h"
const char *printer_state_messages[] = {
"Error",
@@ -45,22 +45,12 @@ void unfreeze_render_thread(){
void send_gcode(bool wait, const char *gcode)
{
Serial.printf("Sending gcode: %s\n", gcode);
char buff[256] = {};
sprintf(buff, "http://%s:%d/printer/gcode/script?script=%s", global_config.klipperHost, global_config.klipperPort, urlEncode(gcode).c_str());
HTTPClient client;
client.begin(buff);
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
if (!wait)
{
client.setTimeout(1000);
}
SETUP_HTTP_CLIENT_FULL("/printer/gcode/script?script=" + urlEncode(gcode), false, wait ? 5000 : 750);
try
{
client.GET();
int result = client.GET();
Serial.printf("Send gcode result: %d\n", result);
}
catch (...)
{
@@ -75,14 +65,7 @@ int get_slicer_time_estimate_s()
delay(10);
char buff[256] = {};
sprintf(buff, "http://%s:%d/server/files/metadata?filename=%s", global_config.klipperHost, global_config.klipperPort, urlEncode(printer.print_filename).c_str());
HTTPClient client;
client.useHTTP10(true);
client.begin(buff);
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT("/server/files/metadata?filename=" + urlEncode(printer.print_filename));
int httpCode = client.GET();
@@ -128,14 +111,7 @@ int last_slicer_time_query = -15000;
void fetch_printer_data()
{
freeze_request_thread();
char buff[256] = {};
sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan", global_config.klipperHost, global_config.klipperPort);
HTTPClient client;
client.useHTTP10(true);
client.begin(buff);
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT("/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan")
int httpCode = client.GET();
delay(10);

View File

@@ -2,9 +2,9 @@
#include "files_query.h"
#include "../conf/global_config.h"
#include "data_setup.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <HardwareSerial.h>
#include "http_client.h"
// Always has +1 entry with a null'd name
FILESYSTEM_FILE* last_query = NULL;
@@ -27,15 +27,7 @@ FILESYSTEM_FILE* get_files(int limit){
std::list<FILESYSTEM_FILE> files;
auto timer_request = millis();
char buff[256] = {};
sprintf(buff, "http://%s:%d/server/files/list", global_config.klipperHost, global_config.klipperPort);
HTTPClient client;
client.useHTTP10(true);
client.setTimeout(5000);
client.begin(buff);
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT_FULL("/server/files/list", true, 5000);
int httpCode = client.GET();
auto timer_parse = millis();

View File

@@ -0,0 +1,26 @@
#include "http_client.h"
#include "../conf/global_config.h"
String get_full_url(String url_part)
{
return "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + url_part;
}
void configure_http_client(HTTPClient &client, String url, bool stream, int timeout)
{
Serial.println(url);
if (stream){
client.useHTTP10(true);
}
if (timeout > 0){
client.setTimeout(timeout);
client.setConnectTimeout(timeout);
}
client.begin(url);
if (global_config.auth_configured) {
client.addHeader("X-Api-Key", global_config.klipper_auth);
}
}

View File

@@ -0,0 +1,11 @@
#pragma once
#include <HTTPClient.h>
String get_full_url(String url_part);
void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000);
#define SETUP_HTTP_CLIENT(url_part) HTTPClient client; configure_http_client(client, get_full_url(url_part));
#define SETUP_HTTP_CLIENT_FULL(url_part, stream, timeout) HTTPClient client; configure_http_client(client, get_full_url(url_part), stream, timeout);

View File

@@ -1,10 +1,10 @@
#include "lvgl.h"
#include "macros_query.h"
#include "./data_setup.h"
#include <HTTPClient.h>
#include "../conf/global_config.h"
#include <ArduinoJson.h>
#include <UrlEncode.h>
#include "http_client.h"
static char* macros[64] = {0};
static int macros_count = 0;
@@ -14,13 +14,7 @@ static bool power_device_states[16] = {0};
static int power_devices_count = 0;
static void _macros_query_internal(){
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/gcode/help";
HTTPClient client;
client.useHTTP10(true);
client.begin(url.c_str());
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT("/printer/gcode/help")
int httpCode = client.GET();
if (httpCode == 200){
@@ -55,15 +49,7 @@ void power_devices_clear(){
}
void _power_devices_query_internal(){
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/machine/device_power/devices";
HTTPClient client;
client.useHTTP10(true);
client.setTimeout(500);
client.setConnectTimeout(1000);
client.begin(url.c_str());
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT("/machine/device_power/devices")
int httpCode = client.GET();
if (httpCode == 200){
@@ -94,13 +80,7 @@ static void on_state_change(void * s, lv_msg_t * m) {
}
bool set_power_state(const char* device_name, bool state) {
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off");
HTTPClient client;
client.useHTTP10(true);
client.begin(url.c_str());
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT("/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off"));
if (client.POST("") != 200)
return false;

View File

@@ -4,7 +4,7 @@
#include <Esp.h>
#include <ArduinoJson.h>
#include "../conf/global_config.h"
#include <HTTPClient.h>
#include "../core/http_client.h"
static unsigned char * data_png = NULL;
static char img_filename_path[256] = {0};
@@ -17,11 +17,10 @@ bool has_128_128_gcode(const char* filename)
return false;
}
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/thumbnails?filename=" + String(filename);
HTTPClient client;
SETUP_HTTP_CLIENT("/server/files/thumbnails?filename=" + String(filename));
int httpCode = 0;
try {
client.begin(url.c_str());
httpCode = client.GET();
}
catch (...){
@@ -72,11 +71,10 @@ lv_obj_t* draw_gcode_img()
return NULL;
}
HTTPClient client;
SETUP_HTTP_CLIENT_FULL("/server/files/gcodes/" + String(img_filename_path), false, 2000);
int httpCode = 0;
try {
String img_url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/gcodes/" + String(img_filename_path);
client.begin(img_url);
httpCode = client.GET();
}
catch (...){

View File

@@ -6,6 +6,7 @@
#include "ui_utils.h"
#include "../core/macros_query.h"
#include "panels/panel.h"
#include "../core/http_client.h"
bool connect_ok = false;
lv_obj_t * hostEntry;
@@ -36,19 +37,11 @@ enum connection_status_t {
};
connection_status_t verify_ip(){
HTTPClient client;
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/info";
SETUP_HTTP_CLIENT_FULL("/printer/info", true, 1000);
int httpCode;
try {
client.setTimeout(500);
client.setConnectTimeout(1000);
client.begin(url.c_str());
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
httpCode = client.GET();
Serial.printf("%d %s\n", httpCode, url.c_str());
if (httpCode == 401)
return CONNECT_AUTH_REQUIRED;

View File

@@ -4,10 +4,11 @@
#include "../../core/files_query.h"
#include "../../conf/global_config.h"
#include <HardwareSerial.h>
#include <HTTPClient.h>
#include "../ui_utils.h"
#include "../../core/lv_setup.h"
#include "../gcode_img.h"
#include "../../core/http_client.h"
#include <UrlEncode.h>
FILESYSTEM_FILE* selected_file = NULL;
@@ -15,32 +16,7 @@ static void btn_print_file(lv_event_t * e){
lv_obj_t * panel = (lv_obj_t*)lv_event_get_user_data(e);
lv_obj_del(panel);
char* buff = (char*)malloc(128 + (strlen(selected_file->name) * 3));
sprintf(buff, "http://%s:%d/printer/print/start?filename=", global_config.klipperHost, global_config.klipperPort);
char* ptr = buff + strlen(buff);
int filename_length = strlen(selected_file->name);
for (int i = 0; i < filename_length; i++){
char c = selected_file->name[i];
if (c == ' '){
*ptr = '%';
ptr++;
*ptr = '2';
ptr++;
*ptr = '0';
} else {
*ptr = c;
}
ptr++;
}
*ptr = 0;
HTTPClient client;
client.begin(buff);
if (global_config.auth_configured)
client.addHeader("X-Api-Key", global_config.klipper_auth);
SETUP_HTTP_CLIENT("/printer/print/start?filename=" + urlEncode(selected_file->name));
int httpCode = client.POST("");
Serial.printf("Print start: HTTP %d\n", httpCode);