From 32bfd2a27b69a5b722bcd812806436c1f9a8e2a1 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sat, 10 Dec 2022 18:58:42 +0100 Subject: [PATCH] Berry refactor webclient POST/PUT/PATCH/DELETE --- .../xdrv_52_3_berry_webclient.ino | 93 +++++++------------ 1 file changed, 35 insertions(+), 58 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino index 09a0d0b91..0971c098b 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino @@ -405,9 +405,15 @@ extern "C" { be_return(vm); /* return code */ } - // wc.POST(string | bytes) -> httpCode:int - int32_t wc_POST(struct bvm *vm); - int32_t wc_POST(struct bvm *vm) { + // Combined function for POST/PUT/PATCH/DELETE + enum { + wc_POST_op, + wc_PUT_op, + wc_PATCH_op, + wc_DELETE_op + }; + int32_t wc_PostPutPatchDelete(struct bvm *vm, int32_t op); + int32_t wc_PostPutPatchDelete(struct bvm *vm, int32_t op) { int32_t argc = be_top(vm); if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) { HTTPClientLight * cl = wc_getclient(vm); @@ -420,7 +426,22 @@ extern "C" { buf = (const char*) be_tobytes(vm, 2, &buf_len); } uint32_t http_connect_time = millis(); - int32_t httpCode = cl->POST((uint8_t*)buf, buf_len); + int32_t httpCode; + switch (op) { + case wc_PUT_op: + httpCode = cl->PUT((uint8_t*)buf, buf_len); + break; + case wc_PATCH_op: + httpCode = cl->PATCH((uint8_t*)buf, buf_len); + break; + case wc_DELETE_op: + httpCode = cl->DELETE((uint8_t*)buf, buf_len); + break; + case wc_POST_op: + default: + httpCode = cl->POST((uint8_t*)buf, buf_len); + break; + } wc_errorCodeMessage(httpCode, http_connect_time); be_pushint(vm, httpCode); be_return(vm); /* return code */ @@ -428,73 +449,29 @@ extern "C" { be_raise(vm, kTypeError, nullptr); } + + // wc.POST(string | bytes) -> httpCode:int + int32_t wc_POST(struct bvm *vm); + int32_t wc_POST(struct bvm *vm) { + return wc_PostPutPatchDelete(vm, wc_POST_op); + } + // wc.PUT(string | bytes) -> httpCode:int int32_t wc_PUT(struct bvm *vm); int32_t wc_PUT(struct bvm *vm) { - int32_t argc = be_top(vm); - if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) { - HTTPClientLight * cl = wc_getclient(vm); - const char * buf = nullptr; - size_t buf_len = 0; - if (be_isstring(vm, 2)) { // string - buf = be_tostring(vm, 2); - buf_len = strlen(buf); - } else { // bytes - buf = (const char*) be_tobytes(vm, 2, &buf_len); - } - uint32_t http_connect_time = millis(); - int32_t httpCode = cl->PUT((uint8_t*)buf, buf_len); - wc_errorCodeMessage(httpCode, http_connect_time); - be_pushint(vm, httpCode); - be_return(vm); /* return code */ - } - be_raise(vm, kTypeError, nullptr); + return wc_PostPutPatchDelete(vm, wc_PUT_op); } // wc.PATCH(string | bytes) -> httpCode:int int32_t wc_PATCH(struct bvm *vm); int32_t wc_PATCH(struct bvm *vm) { - int32_t argc = be_top(vm); - if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) { - HTTPClientLight * cl = wc_getclient(vm); - const char * buf = nullptr; - size_t buf_len = 0; - if (be_isstring(vm, 2)) { // string - buf = be_tostring(vm, 2); - buf_len = strlen(buf); - } else { // bytes - buf = (const char*) be_tobytes(vm, 2, &buf_len); - } - uint32_t http_connect_time = millis(); - int32_t httpCode = cl->PATCH((uint8_t*)buf, buf_len); - wc_errorCodeMessage(httpCode, http_connect_time); - be_pushint(vm, httpCode); - be_return(vm); /* return code */ - } - be_raise(vm, kTypeError, nullptr); + return wc_PostPutPatchDelete(vm, wc_PATCH_op); } // wc.DELETE(string | bytes) -> httpCode:int int32_t wc_DELETE(struct bvm *vm); int32_t wc_DELETE(struct bvm *vm) { - int32_t argc = be_top(vm); - if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) { - HTTPClientLight * cl = wc_getclient(vm); - const char * buf = nullptr; - size_t buf_len = 0; - if (be_isstring(vm, 2)) { // string - buf = be_tostring(vm, 2); - buf_len = strlen(buf); - } else { // bytes - buf = (const char*) be_tobytes(vm, 2, &buf_len); - } - uint32_t http_connect_time = millis(); - int32_t httpCode = cl->DELETE((uint8_t*)buf, buf_len); - wc_errorCodeMessage(httpCode, http_connect_time); - be_pushint(vm, httpCode); - be_return(vm); /* return code */ - } - be_raise(vm, kTypeError, nullptr); + return wc_PostPutPatchDelete(vm, wc_DELETE_op); } int32_t wc_getstring(struct bvm *vm);