Skip to content

Commit

Permalink
Unify http client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 6, 2024
1 parent 9427381 commit f0cc211
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 108 deletions.
36 changes: 6 additions & 30 deletions CYD-Klipper/src/core/data_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 (...)
{
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 2 additions & 10 deletions CYD-Klipper/src/core/files_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions CYD-Klipper/src/core/http_client.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions CYD-Klipper/src/core/http_client.h
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 4 additions & 24 deletions CYD-Klipper/src/core/macros_query.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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){
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions CYD-Klipper/src/ui/gcode_img.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 (...){
Expand Down Expand Up @@ -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 (...){
Expand Down
13 changes: 3 additions & 10 deletions CYD-Klipper/src/ui/ip_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 3 additions & 27 deletions CYD-Klipper/src/ui/panels/print_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,19 @@
#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;

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);
Expand Down

0 comments on commit f0cc211

Please sign in to comment.