diff --git a/include/villas/api.h b/include/villas/api.h index abca157f5..cd9f7d084 100644 --- a/include/villas/api.h +++ b/include/villas/api.h @@ -13,13 +13,14 @@ #include "list.h" #include "common.h" +#include "api/session.h" + /* Forward declarations */ struct lws; struct super_node; struct api; struct api_ressource; -struct api_session; /** Callback type of command function * @@ -30,16 +31,6 @@ struct api_session; */ typedef int (*api_cb_t)(struct api_ressource *c, json_t *args, json_t **resp, struct api_session *s); -enum api_version { - API_VERSION_UNKOWN = 0, - API_VERSION_1 = 1 -}; - -enum api_mode { - API_MODE_WS, /**< This API session was established over a WebSocket connection. */ - API_MODE_HTTP /**< This API session was established via a HTTP REST request. */ -}; - struct api { struct list sessions; /**< List of currently active connections */ @@ -48,27 +39,6 @@ struct api { struct super_node *super_node; }; -/** A connection via HTTP REST or WebSockets to issue API actions. */ -struct api_session { - enum api_mode mode; - enum api_version version; - - int runs; - - struct { - struct api_buffer body; /**< HTTP body / WS payload */ - } request; - - struct { - struct api_buffer body; /**< HTTP body / WS payload */ - struct api_buffer headers; /**< HTTP headers */ - } response; - - bool completed; /**< Did we receive the complete body yet? */ - - struct api *api; -}; - /** Command descriptor * * Every command is described by a descriptor. diff --git a/include/villas/api/session.h b/include/villas/api/session.h new file mode 100644 index 000000000..558074e0d --- /dev/null +++ b/include/villas/api/session.h @@ -0,0 +1,53 @@ +/** API session. + * + * @file + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#pragma once + +#include +#include + +#include "common.h" +#include "web/buffer.h" + +enum api_version { + API_VERSION_UNKOWN = 0, + API_VERSION_1 = 1 +}; + +enum api_mode { + API_MODE_WS, /**< This API session was established over a WebSocket connection. */ + API_MODE_HTTP /**< This API session was established via a HTTP REST request. */ +}; + +/** A connection via HTTP REST or WebSockets to issue API actions. */ +struct api_session { + enum api_mode mode; + enum api_version version; + + int runs; + + struct { + struct web_buffer body; /**< HTTP body / WS payload */ + } request; + + struct { + struct web_buffer body; /**< HTTP body / WS payload */ + struct web_buffer headers; /**< HTTP headers */ + } response; + + bool completed; /**< Did we receive the complete body yet? */ + + enum state state; + + struct api *api; +}; + +int api_session_init(struct api_session *s, struct api *a, enum api_mode m); + +int api_session_destroy(struct api_session *s); + +int api_session_run_command(struct api_session *s, json_t *req, json_t **resp); diff --git a/lib/api.c b/lib/api.c index 780a14d5c..b6e2f80a6 100644 --- a/lib/api.c +++ b/lib/api.c @@ -15,58 +15,10 @@ -int api_session_run_command(struct api_session *s, json_t *json_in, json_t **json_out) -{ - int ret; - const char *rstr; - char *id; - struct plugin *p; - - json_t *json_args = NULL, *json_resp; - - ret = json_unpack(json_in, "{ s: s, s: s, s?: o }", - "request", &rstr, - "id", &id, - "args", &json_args); - if (ret) { - *json_out = json_pack("{ s: s, s: s, s: i, s: s }", - "command", rstr, - "error", "invalid request", - "code", -1, - "id", id); - goto out; - } - - p = plugin_lookup(PLUGIN_TYPE_API, rstr); - if (!p) { - *json_out = json_pack("{ s: s, s: s, s: d, s: s, s: s }", - "command", rstr, - "error", "command not found", - "code", -2, - "command", rstr, - "id", id); - goto out; - } - debug(LOG_API, "Running API request: %s", p->name); - ret = p->api.cb(&p->api, json_args, &json_resp, s); - if (ret) - *json_out = json_pack("{ s: s, s: s, s: s }", - "command", rstr, - "error", "command failed", - "code", ret, - "id", id); - else - *json_out = json_pack("{ s: s, s: o, s: s }", - "command", rstr, - "response", json_resp, - "id", id); -out: debug(LOG_API, "API request completed with code: %d", ret); - return 0; -} int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { diff --git a/lib/api/Makefile.inc b/lib/api/Makefile.inc new file mode 100644 index 000000000..ffdab31bc --- /dev/null +++ b/lib/api/Makefile.inc @@ -0,0 +1,3 @@ +LIB_SRCS += $(wildcard lib/api/*.c) + +-include lib/api/actions/Makefile.inc \ No newline at end of file diff --git a/lib/api/session.c b/lib/api/session.c new file mode 100644 index 000000000..d6261e383 --- /dev/null +++ b/lib/api/session.c @@ -0,0 +1,94 @@ +/** API session. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#include "api/session.h" + +#include "web.h" +#include "plugin.h" + +int api_session_init(struct api_session *s, struct api *a, enum api_mode m) +{ + s->mode = m; + s->api = a; + + s->completed = false; + + web_buffer_init(&s->request.body, s->mode == API_MODE_HTTP ? LWS_WRITE_HTTP : LWS_WRITE_TEXT); + web_buffer_init(&s->response.body, s->mode == API_MODE_HTTP ? LWS_WRITE_HTTP : LWS_WRITE_TEXT); + + if (s->mode == API_MODE_HTTP) + web_buffer_init(&s->response.headers, LWS_WRITE_HTTP_HEADERS); + + return 0; +} + +int api_session_destroy(struct api_session *s) +{ + if (s->state == STATE_DESTROYED) + return 0; + + web_buffer_destroy(&s->request.body); + web_buffer_destroy(&s->response.body); + + if (s->mode == API_MODE_HTTP) + web_buffer_destroy(&s->response.headers); + + s->state = STATE_DESTROYED; + + return 0; +} + +int api_session_run_command(struct api_session *s, json_t *json_in, json_t **json_out) +{ + int ret; + const char *action; + char *id; + struct plugin *p; + + json_t *json_args = NULL, *json_resp; + + ret = json_unpack(json_in, "{ s: s, s: s, s?: o }", + "action", &action, + "id", &id, + "request", &json_args); + if (ret) { + ret = -100; + *json_out = json_pack("{ s: s, s: i }", + "error", "invalid request", + "code", ret); + goto out; + } + + p = plugin_lookup(PLUGIN_TYPE_API, action); + if (!p) { + ret = -101; + *json_out = json_pack("{ s: s, s: s, s: i, s: s }", + "action", action, + "id", id, + "code", ret, + "error", "command not found"); + goto out; + } + + debug(LOG_API, "Running API request: %s", p->name); + + ret = p->api.cb(&p->api, json_args, &json_resp, s); + if (ret) + *json_out = json_pack("{ s: s, s: s, s: i, s: s }", + "action", action, + "id", id, + "code", ret, + "error", "command failed"); + else + *json_out = json_pack("{ s: s, s: s, s: o }", + "action", action, + "id", id, + "response", json_resp); + +out: debug(LOG_API, "API request completed with code: %d", ret); + + return 0; +} \ No newline at end of file