Commit 553b93d0 by amir

- setting false on seterror in cMicroservice_BaseRestResponse

- fix error resp format
- change rmq-handler to support async operations
parent 2ccf5855
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
project(Microservice) project(Microservice)
# version stuff # version stuff
set (Microservice_VERSION_MAJOR 0) set (Microservice_VERSION_MAJOR 1)
set (Microservice_VERSION_MINOR 3) set (Microservice_VERSION_MINOR 3)
set (Microservice_VERSION_PATCH 0) set (Microservice_VERSION_PATCH 0)
set(Microservice_VERSION_STRING ${Microservice_VERSION_MAJOR}.${Microservice_VERSION_MINOR}.${Microservice_VERSION_PATCH}) set(Microservice_VERSION_STRING ${Microservice_VERSION_MAJOR}.${Microservice_VERSION_MINOR}.${Microservice_VERSION_PATCH})
......
## C++ Microservice Framework ## C++ Microservice Framework
## VERSIONS: ## VERSIONS:
# 0.3.1
- setting false on seterror in cMicroservice_BaseRestResponse
- fix error resp format
- change rmq-handler to support async operations
# 0.3.0 # 0.3.0
- Adding RestResponse that can return the headers and status code of the response - Adding RestResponse that can return the headers and status code of the response
check the ReadSync in the Test file check the ReadSync in the Test file
......
...@@ -27,6 +27,7 @@ public: ...@@ -27,6 +27,7 @@ public:
virtual ~cMicroservice_BaseRestResponse() {} virtual ~cMicroservice_BaseRestResponse() {}
void SetError(std::string& error) { void SetError(std::string& error) {
mb_Success = false;
this->mc_Error = error; this->mc_Error = error;
} }
......
...@@ -35,7 +35,9 @@ namespace nsMicroservice_Constants ...@@ -35,7 +35,9 @@ namespace nsMicroservice_Constants
static const char* FAILED_TO_GET_PARAMS = "failed to get params"; static const char* FAILED_TO_GET_PARAMS = "failed to get params";
static const char* JSON_NULL_VALUE = "null"; static const char* JSON_NULL_VALUE = "null";
static const char* SUCCESS_REST_RESPONSE_TEMPLATE = "{ \"success\": true, \"error\": null, \"objectNode\": "; static const char* SUCCESS_REST_RESPONSE_TEMPLATE = "{ \"success\": true, \"error\": null, \"objectNode\": ";
static const char* ERROR_REST_RESPONSE_TEMPLATE = "{ \"success\": false, \"error\": \"%s\", \"objectNode\": null }"; static const char* SUCCESS_NULL_REST_RESPONSE_TEMPLATE = "{ \"success\": true, \"error\": null, \"objectNode\": null ";
static const char* ERROR_REST_RESPONSE_TEMPLATE_PREFIX = "{ \"success\": false, \"error\": \"";
static const char* ERROR_REST_RESPONSE_TEMPLATE_SUFFIX = "\", \"objectNode\": null ";
static const char* COMMAND_ERROR = "Command Error: "; static const char* COMMAND_ERROR = "Command Error: ";
static const char* MON_PREFIX = "/_mon"; static const char* MON_PREFIX = "/_mon";
static const char* LOG_FILE_PATH = "/var/log/mcx/msApp.log"; static const char* LOG_FILE_PATH = "/var/log/mcx/msApp.log";
......
...@@ -155,24 +155,45 @@ void cMicroservice_RMQHandler::SendErrorResp(nsMicroservice_Iface::IResponse* pt ...@@ -155,24 +155,45 @@ void cMicroservice_RMQHandler::SendErrorResp(nsMicroservice_Iface::IResponse* pt
/* /*
* create error rest response * create error rest response
*/ */
snprintf(mba_ErrorBuff, // snprintf(mba_ErrorBuff,
nsMicroservice_Constants::MAX_ERROR_BUFF_URI_LENGTH, // nsMicroservice_Constants::MAX_ERROR_BUFF_URI_LENGTH,
nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE, // nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE,
error.c_str()); // error.c_str());
std::ostringstream c_OutputStream;
c_OutputStream << nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE_PREFIX
<< error.c_str()
<< nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE_SUFFIX
<< '}';
/* /*
* send it * send it
*/ */
pti_Response->Send(mba_ErrorBuff); pti_Response->Send(c_OutputStream.str().c_str());
// pti_Response->Send(mba_ErrorBuff);
} }
void cMicroservice_RMQHandler::WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,rapidjson::Document& t_ObjectDoc) void cMicroservice_RMQHandler::WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,rapidjson::Document& t_ObjectDoc)
{ {
std::ostringstream c_OutputStream; std::ostringstream c_OutputStream;
t_ObjectDoc.Accept(*this->mpc_Writer);
c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << mpc_Buffer->GetString() << '}'; if(!t_ObjectDoc.IsNull()) {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
t_ObjectDoc.Accept(writer);
c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << buffer.GetString() << '}';
} else {
c_OutputStream << nsMicroservice_Constants::SUCCESS_NULL_REST_RESPONSE_TEMPLATE << '}';
}
pti_Response->Send(c_OutputStream.str().c_str()); pti_Response->Send(c_OutputStream.str().c_str());
// clear
mpc_Buffer->Clear(); // std::ostringstream c_OutputStream;
// t_ObjectDoc.Accept(*this->mpc_Writer);
// c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << mpc_Buffer->GetString() << '}';
// pti_Response->Send(c_OutputStream.str().c_str());
// // clear
// mpc_Buffer->Clear();
} }
void cMicroservice_RMQHandler::WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,cMicroservice_BaseRestResponse& t_BaseRestResponse) void cMicroservice_RMQHandler::WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,cMicroservice_BaseRestResponse& t_BaseRestResponse)
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include "impl/MSIMetricsFactoryDropwisardImpl.h" #include "impl/MSIMetricsFactoryDropwisardImpl.h"
cMicroservice_RestHandler::cMicroservice_RestHandler(std::string apiContextPath,cMicroservice_BaseHandler* pc_Handler): cMicroservice_RestHandler::cMicroservice_RestHandler(std::string apiContextPath,cMicroservice_BaseHandler* pc_Handler):
mpc_Handler(pc_Handler),mpc_Logger(nullptr),mpc_PubSub(nullptr),p_metrics_factory_(nullptr) mpc_Handler(pc_Handler),mpc_Logger(nullptr),mpc_PubSub(nullptr),p_metrics_factory_(nullptr)
{ {
mpc_Buffer = new rapidjson::StringBuffer(0,nsMicroservice_Constants::MAX_JSON_BUFFER); mpc_Buffer = new rapidjson::StringBuffer(0,nsMicroservice_Constants::MAX_JSON_BUFFER);
mpc_Writer = new JsonStringWriter(*mpc_Buffer); mpc_Writer = new JsonStringWriter(*mpc_Buffer);
...@@ -171,12 +171,12 @@ void cMicroservice_RestHandler::SendErrorResp(nsMicroservice_Iface::IResponse* p ...@@ -171,12 +171,12 @@ void cMicroservice_RestHandler::SendErrorResp(nsMicroservice_Iface::IResponse* p
/* /*
* create error rest response * create error rest response
*/ */
// snprintf(mba_ErrorBuff,
// nsMicroservice_Constants::MAX_ERROR_BUFF_URI_LENGTH,
// nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE,
// error.c_str());
std::ostringstream c_OutputStream; std::ostringstream c_OutputStream;
c_OutputStream << nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE << error.c_str() << '}'; c_OutputStream << nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE_PREFIX
<< error.c_str()
<< nsMicroservice_Constants::ERROR_REST_RESPONSE_TEMPLATE_SUFFIX
<< '}';
/* /*
* send it * send it
*/ */
...@@ -186,14 +186,16 @@ void cMicroservice_RestHandler::SendErrorResp(nsMicroservice_Iface::IResponse* p ...@@ -186,14 +186,16 @@ void cMicroservice_RestHandler::SendErrorResp(nsMicroservice_Iface::IResponse* p
void cMicroservice_RestHandler::WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,rapidjson::Document& t_ObjectDoc) void cMicroservice_RestHandler::WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,rapidjson::Document& t_ObjectDoc)
{ {
std::ostringstream c_OutputStream;
if(!t_ObjectDoc.IsNull()) {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
std::ostringstream c_OutputStream;
// t_ObjectDoc.Accept(*this->mpc_Writer);
// c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << mpc_Buffer->GetString() << '}';
t_ObjectDoc.Accept(writer); t_ObjectDoc.Accept(writer);
c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << buffer.GetString() << '}'; c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << buffer.GetString() << '}';
} else {
c_OutputStream << nsMicroservice_Constants::SUCCESS_NULL_REST_RESPONSE_TEMPLATE << '}';
}
pti_Response->Send(c_OutputStream.str().c_str()); pti_Response->Send(c_OutputStream.str().c_str());
// clear // clear
//mpc_Buffer->Clear(); //mpc_Buffer->Clear();
......
...@@ -90,24 +90,9 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){ ...@@ -90,24 +90,9 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
if(BuildUrl(cmdDataPtr->p_cmd_params,url)) if(BuildUrl(cmdDataPtr->p_cmd_params,url))
{ {
cmdDataPtr->p_response->Reset(); cmdDataPtr->p_response->Reset();
http_client client(url); pplx::task<http_response> request_task = createRequestTask(cmdDataPtr, url);
//config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2));
http_request request(*cmdDataPtr->p_mtd);
// headers
request.headers().add(header_names::accept,"*/*");
auto& headers = cmdDataPtr->p_cmd_params->GetHeadersMap();
if (!headers.empty()){
for (auto header : headers){
request.headers().add(header.first,header.second);
}
}
// content
if(!cmdDataPtr->p_cmd_params->GetContent().empty())
request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json");
//auto request_task = client.request(*cmdDataPtr->p_mtd);
auto request_task = client.request(request);
if(cmdDataPtr->p_cmd_params->IsAsync_()) if(cmdDataPtr->p_cmd_params->IsAsync_())
{ {
request_task.then([this,cmdDataPtr](http_response resp){ request_task.then([this,cmdDataPtr](http_response resp){
...@@ -180,6 +165,39 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){ ...@@ -180,6 +165,39 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
} }
} }
/**
* creating the request task
* adding all the cmd data params
* @param cmdDataPtr
* @param url
* @return request task to perform
*/
pplx::task<http_response>
MSICommandClientHttpImpl::createRequestTask(const MSICommandClientHttpImpl::HttpCommandDataPtr &cmdDataPtr,
const std::string &url) const {
http_client client(url);
//config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2));
http_request request(*cmdDataPtr->p_mtd);
// http_client_config config = client.client_config();
// config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2));
// headers
request.headers().add(header_names::accept, "*/*");
auto& headers = cmdDataPtr->p_cmd_params->GetHeadersMap();
if (!headers.empty()){
for (auto header : headers){
request.headers().add(header.first,header.second);
}
}
// content
if(!cmdDataPtr->p_cmd_params->GetContent().empty())
request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json");
//auto request_task = client.request(*cmdDataPtr->p_mtd);
//auto request_task =
return client.request(request);//request_task;
}
MSRetStat MSICommandClientHttpImpl::Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) { MSRetStat MSICommandClientHttpImpl::Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat; MSRetStat retstat;
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_)); auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_));
......
...@@ -91,6 +91,8 @@ private: ...@@ -91,6 +91,8 @@ private:
CommandCounters& cmd_counters); CommandCounters& cmd_counters);
void DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse, web::http::http_response &response); void DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse, web::http::http_response &response);
pplx::task<web::http::http_response> createRequestTask(const HttpCommandDataPtr &cmdDataPtr, const std::string &url) const;
}; };
......
...@@ -232,10 +232,30 @@ void to_func() ...@@ -232,10 +232,30 @@ void to_func()
class TO class TO
{ {
public: public:
TO() {
std::cout << __PRETTY_FUNCTION__ << '\n';
}
void to_member_func() { void to_member_func() {
std::cout << " member boring function" << std::endl; std::cout << " member boring function" << std::endl;
} }
}; };
TO testDocs()
{
cMicroservice_BaseRestResponse resp;
if (resp.GetObjectNode().IsNull()) { std::cout << "empty\n"; }
rapidjson::Document& rpj_Doc = resp.GetObjectNode();
rapidjson::Document::AllocatorType& rpj_Alloc = rpj_Doc.GetAllocator();
rpj_Doc.SetObject();
if (resp.GetObjectNode().IsNull()) { std::cout << "empty\n"; }
TO to;
return to;
}
void test_timers() void test_timers()
{ {
TO toA; TO toA;
...@@ -264,6 +284,7 @@ int main(int argc, char *argv[]) ...@@ -264,6 +284,7 @@ int main(int argc, char *argv[])
{ {
// testCache(); // testCache();
TO to2 = testDocs();
// test_timers(); // test_timers();
runNewMS(); runNewMS();
......
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 sign in to comment