Merge pull request #17159 from barbudor/berry_wc_put_delete_patch

Adding PUT, PATCH and DELETE to Berry webclient
This commit is contained in:
s-hadinger 2022-11-21 20:28:48 +01:00 committed by GitHub
commit 1cdc03f835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 21 deletions

View File

@ -569,6 +569,20 @@ int HTTPClientLight::PUT(String payload) {
return PUT((uint8_t *) payload.c_str(), payload.length()); return PUT((uint8_t *) payload.c_str(), payload.length());
} }
/**
* sends a delete request to the server
* @param payload uint8_t *
* @param size size_t
* @return http code
*/
int HTTPClientLight::DELETE(uint8_t * payload, size_t size) {
return sendRequest("DELETE", payload, size);
}
int HTTPClientLight::DELETE(String payload) {
return DELETE((uint8_t *) payload.c_str(), payload.length());
}
/** /**
* sendRequest * sendRequest
* @param type const char * "GET", "POST", .... * @param type const char * "GET", "POST", ....

View File

@ -194,6 +194,8 @@ public:
int POST(String payload); int POST(String payload);
int PUT(uint8_t * payload, size_t size); int PUT(uint8_t * payload, size_t size);
int PUT(String payload); int PUT(String payload);
int DELETE(uint8_t * payload, size_t size);
int DELETE(String payload);
int sendRequest(const char * type, String payload); int sendRequest(const char * type, String payload);
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0); int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
int sendRequest(const char * type, Stream * stream, size_t size = 0); int sendRequest(const char * type, Stream * stream, size_t size = 0);

View File

@ -20,6 +20,9 @@ extern int wc_close(bvm *vm);
extern int wc_addheader(bvm *vm); extern int wc_addheader(bvm *vm);
extern int wc_GET(bvm *vm); extern int wc_GET(bvm *vm);
extern int wc_POST(bvm *vm); extern int wc_POST(bvm *vm);
extern int wc_PUT(bvm *vm);
extern int wc_PATCH(bvm *vm);
extern int wc_DELETE(bvm *vm);
extern int wc_getstring(bvm *vm); extern int wc_getstring(bvm *vm);
extern int wc_writefile(bvm *vm); extern int wc_writefile(bvm *vm);
extern int wc_writeflash(bvm *vm); extern int wc_writeflash(bvm *vm);
@ -49,6 +52,9 @@ class be_class_webclient (scope: global, name: webclient) {
add_header, func(wc_addheader) add_header, func(wc_addheader)
GET, func(wc_GET) GET, func(wc_GET)
POST, func(wc_POST) POST, func(wc_POST)
PUT, func(wc_PUT)
PATCH, func(wc_PATCH)
DELETE, func(wc_DELETE)
get_string, func(wc_getstring) get_string, func(wc_getstring)
write_file, func(wc_writefile) write_file, func(wc_writefile)
write_flash, func(wc_writeflash) write_flash, func(wc_writeflash)

View File

@ -428,6 +428,75 @@ extern "C" {
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }
// 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);
}
// 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);
}
// 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);
}
int32_t wc_getstring(struct bvm *vm); int32_t wc_getstring(struct bvm *vm);
int32_t wc_getstring(struct bvm *vm) { int32_t wc_getstring(struct bvm *vm) {
HTTPClientLight * cl = wc_getclient(vm); HTTPClientLight * cl = wc_getclient(vm);