1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

refactoring: moved api_session to api/session

This commit is contained in:
Steffen Vogel 2017-04-09 16:22:03 +02:00
parent 6b8b9e5c1f
commit 5c6188ccc2
5 changed files with 152 additions and 80 deletions

View file

@ -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.

View file

@ -0,0 +1,53 @@
/** API session.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
*********************************************************************************/
#pragma once
#include <stdbool.h>
#include <jansson.h>
#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);

View file

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

3
lib/api/Makefile.inc Normal file
View file

@ -0,0 +1,3 @@
LIB_SRCS += $(wildcard lib/api/*.c)
-include lib/api/actions/Makefile.inc

94
lib/api/session.c Normal file
View file

@ -0,0 +1,94 @@
/** API session.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @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;
}