Skip to content

Commit

Permalink
feat(restful): add group subscribe update api
Browse files Browse the repository at this point in the history
  • Loading branch information
eeff committed Aug 2, 2023
1 parent f4c4586 commit 5f0e698
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
53 changes: 38 additions & 15 deletions plugins/restful/group_config_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,50 @@ void handle_get_driver_group_resp(nng_aio * aio,
utarray_free(groups->groups);
}

void handle_grp_subscribe(nng_aio *aio)
static inline int send_subscribe(nng_aio *aio, neu_reqresp_type_e type,
neu_json_subscribe_req_t *req)
{
neu_plugin_t *plugin = neu_rest_get_plugin();

neu_reqresp_head_t header = {
.ctx = aio,
.type = type,
};

neu_req_subscribe_t cmd = { 0 };
strcpy(cmd.app, req->app);
strcpy(cmd.driver, req->driver);
strcpy(cmd.group, req->group);
cmd.params = req->params; // ownership moved
req->params = NULL;

if (0 != neu_plugin_op(plugin, header, &cmd)) {
return NEU_ERR_IS_BUSY;
}

return 0;
}

void handle_grp_subscribe(nng_aio *aio)
{
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_subscribe_req_t, neu_json_decode_subscribe_req, {
int ret = 0;
neu_req_subscribe_t cmd = { 0 };
neu_reqresp_head_t header = { 0 };
header.ctx = aio;
header.type = NEU_REQ_SUBSCRIBE_GROUP;
strcpy(cmd.app, req->app);
strcpy(cmd.driver, req->driver);
strcpy(cmd.group, req->group);
cmd.params = req->params; // ownership moved
req->params = NULL;
ret = neu_plugin_op(plugin, header, &cmd);
int ret = send_subscribe(aio, NEU_REQ_SUBSCRIBE_GROUP, req);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
NEU_JSON_RESPONSE_ERROR(
ret, { neu_http_response(aio, ret, result_error); });
}
})
}

void handle_grp_update_subscribe(nng_aio *aio)
{
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_subscribe_req_t, neu_json_decode_subscribe_req, {
int ret = send_subscribe(aio, NEU_REQ_UPDATE_SUBSCRIBE_GROUP, req);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(
ret, { neu_http_response(aio, ret, result_error); });
}
})
}
Expand Down
1 change: 1 addition & 0 deletions plugins/restful/group_config_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void handle_get_driver_group_resp(nng_aio * aio,
neu_resp_get_driver_group_t *groups);

void handle_grp_subscribe(nng_aio *aio);
void handle_grp_update_subscribe(nng_aio *aio);
void handle_grp_unsubscribe(nng_aio *aio);
void handle_grp_get_subscribe(nng_aio *aio);
void handle_grp_get_subscribe_resp(nng_aio * aio,
Expand Down
6 changes: 6 additions & 0 deletions plugins/restful/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/subscribe",
.value.handler = handle_grp_subscribe,
},
{
.method = NEU_HTTP_METHOD_PUT,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/subscribe",
.value.handler = handle_grp_update_subscribe,
},
{
.method = NEU_HTTP_METHOD_DELETE,
.type = NEU_HTTP_HANDLER_FUNCTION,
Expand Down

0 comments on commit 5f0e698

Please sign in to comment.