Berry refactor webclient POST/PUT/PATCH/DELETE

This commit is contained in:
Stephan Hadinger 2022-12-10 18:58:42 +01:00
parent e018b61a9d
commit 32bfd2a27b
1 changed files with 35 additions and 58 deletions

View File

@ -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);