Commit aabc49af authored by Pavel Vainerman's avatar Pavel Vainerman

(UHttp): добавил обработку команд вида ObjectName/cmd

parent d373cbe2
...@@ -32,12 +32,14 @@ ...@@ -32,12 +32,14 @@
* *
* Пока поддерживается только метод GET * Пока поддерживается только метод GET
* Ответ в формате JSON * Ответ в формате JSON
* В текущем API не подразумеваются запросы глубже '/api/version/ObjectName/xxxx'
* *
* Версия API: v01 * Версия API: v01
* /api/version/list - Получение списка доступных объектов * /api/version/list - Получение списка доступных объектов
* /api/version/help - Получение списка доступных команд * /api/version/help - Получение списка доступных команд
* /api/version/ObjectName - получение информации об объекте ObjectName * /api/version/ObjectName - получение информации об объекте ObjectName
* /api/version/ObjectName/help - получение списка доступных команд для объекта ObjectName * /api/version/ObjectName/help - получение списка доступных команд для объекта ObjectName
* /api/version/ObjectName/xxxx - 'xxx' запрос к объекту ObjectName
* *
* \todo подумать над /api/version/tree - получение "дерева" объектов (древовидный список с учётом подчинения Manager/Objects) * \todo подумать над /api/version/tree - получение "дерева" объектов (древовидный список с учётом подчинения Manager/Objects)
*/ */
...@@ -58,7 +60,8 @@ namespace UniSetTypes ...@@ -58,7 +60,8 @@ namespace UniSetTypes
// throw SystemError // throw SystemError
virtual nlohmann::json getData( const Poco::URI::QueryParameters& p ) = 0; virtual nlohmann::json getData( const Poco::URI::QueryParameters& p ) = 0;
virtual nlohmann::json help( const Poco::URI::QueryParameters& p ) = 0; virtual nlohmann::json httpHelp( const Poco::URI::QueryParameters& p ) = 0;
virtual nlohmann::json request( const std::string& req, const Poco::URI::QueryParameters& p ) = 0;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/*! интерфейс для обработки запросов к объектам */ /*! интерфейс для обработки запросов к объектам */
...@@ -73,8 +76,8 @@ namespace UniSetTypes ...@@ -73,8 +76,8 @@ namespace UniSetTypes
// throw SystemError // throw SystemError
virtual nlohmann::json getObjectsList( const Poco::URI::QueryParameters& p ) = 0; virtual nlohmann::json getObjectsList( const Poco::URI::QueryParameters& p ) = 0;
virtual nlohmann::json helpByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0; virtual nlohmann::json helpByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
virtual nlohmann::json requestByName( const std::string& name, const std::string& req, const Poco::URI::QueryParameters& p ) = 0;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -89,6 +89,7 @@ class UniSetActivator: ...@@ -89,6 +89,7 @@ class UniSetActivator:
virtual nlohmann::json getDataByName( const std::string& name , const Poco::URI::QueryParameters& p ) override; virtual nlohmann::json getDataByName( const std::string& name , const Poco::URI::QueryParameters& p ) override;
virtual nlohmann::json getObjectsList( const Poco::URI::QueryParameters& p ) override; virtual nlohmann::json getObjectsList( const Poco::URI::QueryParameters& p ) override;
virtual nlohmann::json helpByName( const std::string& name, const Poco::URI::QueryParameters& p ) override; virtual nlohmann::json helpByName( const std::string& name, const Poco::URI::QueryParameters& p ) override;
virtual nlohmann::json requestByName( const std::string& name, const std::string& req, const Poco::URI::QueryParameters& p ) override;
protected: protected:
......
...@@ -103,7 +103,8 @@ class UniSetObject: ...@@ -103,7 +103,8 @@ class UniSetObject:
// HTTP API // HTTP API
virtual nlohmann::json getData( const Poco::URI::QueryParameters& p ) override; virtual nlohmann::json getData( const Poco::URI::QueryParameters& p ) override;
virtual nlohmann::json help( const Poco::URI::QueryParameters& p ) override; virtual nlohmann::json httpHelp( const Poco::URI::QueryParameters& p ) override;
virtual nlohmann::json request( const std::string& req, const Poco::URI::QueryParameters& p ) override;
// -------------- вспомогательные -------------- // -------------- вспомогательные --------------
/*! получить ссылку (на себя) */ /*! получить ссылку (на себя) */
......
...@@ -109,11 +109,16 @@ void UHttpRequestHandler::handleRequest( Poco::Net::HTTPServerRequest& req, Poco ...@@ -109,11 +109,16 @@ void UHttpRequestHandler::handleRequest( Poco::Net::HTTPServerRequest& req, Poco
auto json = registry->getObjectsList(qp); auto json = registry->getObjectsList(qp);
out << json.dump(); out << json.dump();
} }
else if( seg.size() >=4 && seg[3] == "help" ) // /api/version/ObjectName/help else if( seg.size() == 4 && seg[3] == "help" ) // /api/version/ObjectName/help
{ {
auto json = registry->helpByName(objectName, qp); auto json = registry->helpByName(objectName, qp);
out << json.dump(); out << json.dump();
} }
else if( seg.size() >= 4 ) // /api/version/ObjectName/xxx..
{
auto json = registry->requestByName(objectName, seg[3], qp);
out << json.dump();
}
else else
{ {
auto json = registry->getDataByName(objectName, qp); auto json = registry->getDataByName(objectName, qp);
......
...@@ -896,11 +896,25 @@ nlohmann::json UniSetActivator::getObjectsList( const Poco::URI::QueryParameters ...@@ -896,11 +896,25 @@ nlohmann::json UniSetActivator::getObjectsList( const Poco::URI::QueryParameters
nlohmann::json UniSetActivator::helpByName( const string& name, const Poco::URI::QueryParameters& p ) nlohmann::json UniSetActivator::helpByName( const string& name, const Poco::URI::QueryParameters& p )
{ {
if( name == myname ) if( name == myname )
return help(p); return httpHelp(p);
auto obj = deepFindObject(name); auto obj = deepFindObject(name);
if( obj ) if( obj )
return obj->help(p); return obj->httpHelp(p);
ostringstream err;
err << "Object '" << name << "' not found";
throw UniSetTypes::NameNotFound(err.str());
}
// ------------------------------------------------------------------------------------------
nlohmann::json UniSetActivator::requestByName( const string& name, const std::string& req, const Poco::URI::QueryParameters& p)
{
if( name == myname )
return request(req,p);
auto obj = deepFindObject(name);
if( obj )
return obj->request(req,p);
ostringstream err; ostringstream err;
err << "Object '" << name << "' not found"; err << "Object '" << name << "' not found";
......
...@@ -390,16 +390,26 @@ nlohmann::json UniSetObject::getData( const Poco::URI::QueryParameters& p ) ...@@ -390,16 +390,26 @@ nlohmann::json UniSetObject::getData( const Poco::URI::QueryParameters& p )
jdata["isActive"] = isActive(); jdata["isActive"] = isActive();
jdata["objectType"] = getType(); jdata["objectType"] = getType();
return jdata; nlohmann::json ret;
ret[myname] = jdata;
return ret;
} }
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
nlohmann::json UniSetObject::help( const Poco::URI::QueryParameters& p ) nlohmann::json UniSetObject::httpHelp( const Poco::URI::QueryParameters& p )
{ {
nlohmann::json jdata; nlohmann::json jdata;
jdata[myname]["help"] = {}; jdata[myname]["help"] = {};
return jdata; return jdata;
} }
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
nlohmann::json UniSetObject::request( const string& req, const Poco::URI::QueryParameters& p )
{
//!\todo Подумать может возвращать getData() типа стандартный ответ..
nlohmann::json jdata;
jdata[myname][req]= "";
return jdata;
}
// ------------------------------------------------------------------------------------------
ObjectPtr UniSetObject::getRef() const ObjectPtr UniSetObject::getRef() const
{ {
UniSetTypes::uniset_rwmutex_rlock lock(refmutex); UniSetTypes::uniset_rwmutex_rlock lock(refmutex);
......
...@@ -23,7 +23,7 @@ class UTestSupplier: ...@@ -23,7 +23,7 @@ class UTestSupplier:
return j; return j;
} }
virtual nlohmann::json help( const Poco::URI::QueryParameters& p ) override virtual nlohmann::json httpHelp( const Poco::URI::QueryParameters& p ) override
{ {
nlohmann::json j; nlohmann::json j;
j["test"]["help"] = { j["test"]["help"] = {
...@@ -33,6 +33,13 @@ class UTestSupplier: ...@@ -33,6 +33,13 @@ class UTestSupplier:
return j; return j;
} }
virtual nlohmann::json request( const std::string& req, const Poco::URI::QueryParameters& p ) override
{
nlohmann::json j;
j[req] = "OK";
return j;
}
}; };
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
class UTestRequestRegistry: class UTestRequestRegistry:
...@@ -70,6 +77,14 @@ class UTestRequestRegistry: ...@@ -70,6 +77,14 @@ class UTestRequestRegistry:
return j; return j;
} }
virtual nlohmann::json requestByName( const std::string& name, const std::string& req, const Poco::URI::QueryParameters& p ) override
{
nlohmann::json j;
j[name][req] = "OK";
return j;
}
private: private:
UTestSupplier sup; UTestSupplier sup;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment