rest controller update
This commit is contained in:
@@ -25,6 +25,7 @@ file (
|
||||
${workspaceRoot}/src/plugins/uhttpd/uhttpd-rest-api/core/ubus-connection.cpp
|
||||
${workspaceRoot}/src/plugins/uhttpd/uhttpd-rest-api/core/ubus-reason.cpp
|
||||
# rest
|
||||
${workspaceRoot}/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp
|
||||
${workspaceRoot}/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp
|
||||
${workspaceRoot}/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-connection.cpp
|
||||
#etag-rest
|
||||
|
||||
@@ -26,6 +26,17 @@
|
||||
|
||||
#include "core/web-controller.h"
|
||||
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn WebController
|
||||
*
|
||||
* @brief default constructor of the web controller object.
|
||||
*/
|
||||
WebController::WebController(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn WebController
|
||||
*
|
||||
|
||||
@@ -40,6 +40,7 @@ struct client;
|
||||
class WebController
|
||||
{
|
||||
public:
|
||||
WebController(void);
|
||||
WebController(const std::string &a_path);
|
||||
virtual ~WebController(void);
|
||||
|
||||
|
||||
@@ -22,22 +22,43 @@
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <json-c/json.h>
|
||||
|
||||
#include "rest/rest-connection.h"
|
||||
|
||||
#include "rest/rest-controller.h"
|
||||
|
||||
#include <uhttpd/uhttpd.h>
|
||||
|
||||
|
||||
#define kControllerModelKey "model"
|
||||
#define kControllerEndpointKey "endpoint"
|
||||
#define kControllerUbusKey "ubus"
|
||||
|
||||
#define kControllerPathKey "path"
|
||||
#define kControllerMethodKey "method"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn RestController
|
||||
*
|
||||
* @brief constructor of the rest controller object.
|
||||
*/
|
||||
RestController::RestController(const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : WebController(a_path),
|
||||
RestController::RestController(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn RestController
|
||||
*
|
||||
* @brief constructor of the rest controller object.
|
||||
*/
|
||||
RestController::RestController(const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : WebController(a_path)
|
||||
#if 0
|
||||
m_method_get(a_method_get),
|
||||
m_method_put(a_method_put),
|
||||
m_timeout(a_timeout),
|
||||
mf_raw_response(a_raw_response)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
@@ -60,6 +81,67 @@ WebConnection *RestController::new_connection(struct uhttpd_ops *an_ops, struct
|
||||
return new RestConnection(an_ops, a_client, this, a_parameters);
|
||||
}
|
||||
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn from_json
|
||||
*
|
||||
* @brief import a rest controller from a json node..
|
||||
*/
|
||||
bool RestController::from_json(struct json_object *a_node)
|
||||
{
|
||||
struct json_object *the_value_node, *the_ubus_node;
|
||||
|
||||
// model
|
||||
if (json_object_object_get_ex(a_node, kControllerModelKey, &the_value_node))
|
||||
{
|
||||
std::string the_model;
|
||||
the_model = json_object_get_string(the_value_node);
|
||||
if (the_model == "get")
|
||||
m_method = UH_HTTP_MSG_GET;
|
||||
else if (the_model == "put")
|
||||
m_method = UH_HTTP_MSG_PUT;
|
||||
else if (the_model == "post")
|
||||
m_method = UH_HTTP_MSG_POST;
|
||||
else
|
||||
m_method = UH_HTTP_MSG_DELETE;
|
||||
}
|
||||
|
||||
// endpoint
|
||||
if (json_object_object_get_ex(a_node, kControllerEndpointKey, &the_value_node))
|
||||
{
|
||||
int the_len, i;
|
||||
struct json_object *the_ep_node;
|
||||
the_len = json_object_array_length(the_value_node);
|
||||
|
||||
for (i = 0; i < the_len; i++)
|
||||
{
|
||||
std::string the_endpoint;
|
||||
the_ep_node = json_object_array_get_idx(the_value_node, i);
|
||||
the_endpoint = json_object_get_string(the_ep_node);
|
||||
m_endpoint.push_back(the_endpoint);
|
||||
}
|
||||
}
|
||||
|
||||
// ubus
|
||||
if (json_object_object_get_ex(a_node, kControllerUbusKey, &the_ubus_node))
|
||||
{
|
||||
// path
|
||||
if (json_object_object_get_ex(the_ubus_node, kControllerPathKey, &the_value_node))
|
||||
{
|
||||
m_ubus.m_path = json_object_get_string(the_value_node);
|
||||
}
|
||||
//method
|
||||
if (json_object_object_get_ex(the_ubus_node, kControllerMethodKey, &the_value_node))
|
||||
{
|
||||
m_ubus.m_method = json_object_get_string(the_value_node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn get_method_get
|
||||
*
|
||||
@@ -67,7 +149,7 @@ WebConnection *RestController::new_connection(struct uhttpd_ops *an_ops, struct
|
||||
*/
|
||||
std::string RestController::get_method_get(void)
|
||||
{
|
||||
return m_method_get;
|
||||
return ""; //m_method_get;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
@@ -77,7 +159,7 @@ std::string RestController::get_method_get(void)
|
||||
*/
|
||||
std::string RestController::get_method_put(void)
|
||||
{
|
||||
return m_method_put;
|
||||
return ""; //m_method_put;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
@@ -87,7 +169,7 @@ std::string RestController::get_method_put(void)
|
||||
*/
|
||||
uint16_t RestController::get_timeout(void)
|
||||
{
|
||||
return m_timeout;
|
||||
return 0; //m_timeout;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
@@ -97,7 +179,7 @@ uint16_t RestController::get_timeout(void)
|
||||
*/
|
||||
bool RestController::is_raw_response(void)
|
||||
{
|
||||
return mf_raw_response;
|
||||
return false; //mf_raw_response;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
@@ -107,6 +189,7 @@ bool RestController::is_raw_response(void)
|
||||
*/
|
||||
std::string RestController::get_method(uint8_t a_method)
|
||||
{
|
||||
#if 0
|
||||
switch (a_method)
|
||||
{
|
||||
case UH_HTTP_MSG_GET:
|
||||
@@ -120,6 +203,6 @@ std::string RestController::get_method(uint8_t a_method)
|
||||
// TODO ERROR
|
||||
break;
|
||||
};
|
||||
|
||||
#endif
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -27,8 +27,11 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <core/web-controller.h>
|
||||
#include <rest/ubus.h>
|
||||
|
||||
|
||||
/*--------------------------------- Define ----------------------------------*/
|
||||
|
||||
@@ -38,16 +41,21 @@
|
||||
|
||||
/*----------------------------- Dependencies --------------------------------*/
|
||||
|
||||
struct json_object;
|
||||
|
||||
/*--------------------------------- CLASS ----------------------------------*/
|
||||
|
||||
class RestController : public WebController
|
||||
{
|
||||
public:
|
||||
RestController(void);
|
||||
RestController(const std::string &a_path, const std::string &a_method_get = "", const std::string &a_method_put = "", int a_timeout = kDefaultTimeout, bool a_raw_response = false);
|
||||
virtual ~RestController(void);
|
||||
|
||||
WebConnection *new_connection(struct uhttpd_ops *an_ops, struct client *a_client, const std::string &a_parameters);
|
||||
|
||||
bool from_json(struct json_object *a_node);
|
||||
|
||||
std::string get_method_get(void);
|
||||
std::string get_method_put(void);
|
||||
uint16_t get_timeout(void);
|
||||
@@ -56,12 +64,18 @@ class RestController : public WebController
|
||||
std::string get_method(uint8_t a_method);
|
||||
|
||||
protected:
|
||||
uint8_t m_method;
|
||||
std::vector<std::string> m_endpoint;
|
||||
Ubus m_ubus;
|
||||
|
||||
/*
|
||||
std::string m_name;
|
||||
std::string m_path;
|
||||
std::string m_method_get;
|
||||
std::string m_method_put;
|
||||
uint16_t m_timeout;
|
||||
bool mf_raw_response;
|
||||
*/
|
||||
};
|
||||
|
||||
#endif /* _REST_CONTROLLER_H */
|
||||
|
||||
34
src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp
Normal file
34
src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*!
|
||||
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 24/04/2018
|
||||
*/
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include "ubus.h"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn Ubus
|
||||
*
|
||||
* @brief constructor of the ubus object.
|
||||
*/
|
||||
Ubus::Ubus(void)
|
||||
{
|
||||
}
|
||||
41
src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.h
Normal file
41
src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*!
|
||||
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 24/04/2018
|
||||
*/
|
||||
|
||||
#ifndef _UBUS_H
|
||||
#define _UBUS_H
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <string>
|
||||
|
||||
/*--------------------------------- CLASS ----------------------------------*/
|
||||
|
||||
class Ubus
|
||||
{
|
||||
public:
|
||||
Ubus(void);
|
||||
|
||||
std::string m_path;
|
||||
std::string m_method;
|
||||
};
|
||||
|
||||
#endif /* _UBUS_H */
|
||||
@@ -425,7 +425,7 @@ int UhttpServer::load_config_dir(const char *a_config_dir_path)
|
||||
struct json_object *the_root_node;
|
||||
the_rep = opendir(a_config_dir_path);
|
||||
if (the_rep == NULL) {
|
||||
fprintf (stderr, "Impossible to open the config directory. check your parameters.\n");
|
||||
fprintf (stderr, "Impossible to open the config directory (%s).\n", a_config_dir_path);
|
||||
return -1;
|
||||
}
|
||||
while ((the_dir_ent = readdir(the_rep)) != NULL)
|
||||
@@ -469,22 +469,18 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node)
|
||||
|
||||
if (json_object_object_get_ex(a_root_node, kControllerKey, &the_ctr_array_node))
|
||||
{
|
||||
RestController *the_rest_controller;
|
||||
the_len = json_object_array_length(the_ctr_array_node);
|
||||
|
||||
for (i = 0; i < the_len; i++)
|
||||
{
|
||||
the_ctrl_node = json_object_array_get_idx(the_ctr_array_node, i);
|
||||
the_rest_controller = new RestController;
|
||||
|
||||
if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response))
|
||||
if (the_rest_controller->from_json(the_ctrl_node))
|
||||
{
|
||||
#if 0
|
||||
printf("Path: <%s>\n", the_path.c_str());
|
||||
printf("\t- model name: <%s>\n", the_model_name.c_str());
|
||||
printf("\t- get: <%s>\n", the_get_method.c_str());
|
||||
printf("\t- set: <%s>\n", the_set_method.c_str());
|
||||
printf("\t- raw response: <%s>\n", the_raw_response?"true":"false");
|
||||
#endif
|
||||
add_controller(the_path, new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response));
|
||||
//new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response)
|
||||
add_controller(the_path, the_rest_controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -535,6 +531,7 @@ bool UhttpServer::get_controller_fields(struct json_object *a_node, std::string
|
||||
std::string &a_get_method, std::string &a_set_method, bool &a_raw_response)
|
||||
{
|
||||
bool the_result = false;
|
||||
|
||||
struct json_object *the_path_node, *the_ubus_model_node, *the_ubus_event_node, *the_get_method_node, *the_etag_key_node;
|
||||
struct json_object *the_set_method_node, *the_raw_response_node;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user