Commit 2ccf5855 by amir

# 0.3.0

- Adding RestResponse that can return the headers and status code of the response
  check the ReadSync in the Test file
- Implementing the Add Headers to request
  check the ReadSync in the Test file
parent 456964ba
......@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.12)
project(Microservice)
# version stuff
set (Microservice_VERSION_MAJOR 0)
set (Microservice_VERSION_MINOR 2)
set (Microservice_VERSION_MINOR 3)
set (Microservice_VERSION_PATCH 0)
set(Microservice_VERSION_STRING ${Microservice_VERSION_MAJOR}.${Microservice_VERSION_MINOR}.${Microservice_VERSION_PATCH})
......@@ -36,7 +36,7 @@ file(GLOB_RECURSE SOURCES "src/*.cpp")
set (3PARTY_SOURCES ../3party/mongoose/mongoose.c)
#Generate the shared library from the sources
add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES})
add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES} src/common/Microservice_RestResponse.h)
target_link_libraries(Microservice ${PROJECT_LINK_LIBS} )
set_target_properties(Microservice PROPERTIES VERSION ${Microservice_VERSION_STRING}
SOVERSION ${Microservice_VERSION_MAJOR})
......
## C++ Microservice Framework
## VERSIONS:
# 0.3.0
- Adding RestResponse that can return the headers and status code of the response
check the ReadSync in the Test file
- Implementing the Add Headers to request
check the ReadSync in the Test file
# 0.2.0 - Add Async client operations
- Tasks Reference: https://msdn.microsoft.com/en-us/library/dd492427.aspx
#0.1.0 - Add Scheduler
\ No newline at end of file
......@@ -52,6 +52,8 @@ public:
if(!mc_ObjectNode.IsNull())
mc_ObjectNode.Clear();
}
virtual uint32_t GetTypeHash() { return 0; }
private:
bool mb_Success;
......
//
// Created by amir on 07/11/16.
//
#ifndef MICROSERVICE_MICROSERVICE_RESTRESPONSE_H
#define MICROSERVICE_MICROSERVICE_RESTRESPONSE_H
#include <Microservice_BaseRestResponse.h>
#include <map>
class Microservice_RestResponse : public cMicroservice_BaseRestResponse{
public:
Microservice_RestResponse():
cMicroservice_BaseRestResponse() {}
Microservice_RestResponse(bool b_Success, std::string &c_Error) :
cMicroservice_BaseRestResponse(b_Success, c_Error) {}
Microservice_RestResponse(bool b_Success, std::string &c_Error, rapidjson::Document &c_ObjectNode):
cMicroservice_BaseRestResponse(b_Success, c_Error, c_ObjectNode) {}
const std::map<std::string, std::string> &getHeaderMap() const {
return headerMap_;
}
void setHeaderMap(const std::map<std::string, std::string> &headerMap_) {
Microservice_RestResponse::headerMap_ = headerMap_;
}
void addHeader(std::string& header, std::string& value) {
headerMap_[header] = value;
}
/**
* please note that if the header does not exist
* returning empty string
* @param header
* @return
*/
std::string& getHeader(std::string& header) {
return headerMap_[header];
}
unsigned short getResponse_code() const {
return response_code_;
}
void setResponse_code(unsigned short response_code_) {
Microservice_RestResponse::response_code_ = response_code_;
}
void Reset(){
cMicroservice_BaseRestResponse::Reset();
response_code_ = 0;
headerMap_.clear();
}
virtual uint32_t GetTypeHash() override {
return TYPE_HASH;
}
public:
static constexpr uint32_t TYPE_HASH = 1478523102; // epoch time of creation
private:
std::map<std::string,std::string> headerMap_;
unsigned short response_code_;
};
#endif //MICROSERVICE_MICROSERVICE_RESTRESPONSE_H
......@@ -16,6 +16,7 @@
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/base_uri.h>
#include <common/Microservice_RestResponse.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
......@@ -51,9 +52,9 @@ bool MSICommandClientHttpImpl::BuildUrl(MSCommandParams* p_cmd_params, std::stri
unencoded_url.append(HTTP_SCHEME).append(entity);
unencoded_url.append(entity);
// params
if(p_cmd_params->GetParams())
if(!p_cmd_params->GetParams().empty())
{
for(auto param : *p_cmd_params->GetParams())
for(auto param : p_cmd_params->GetParams())
{
unencoded_url.append(1,'/') .append(param.c_str());
}
......@@ -92,9 +93,19 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
http_client client(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_())
......@@ -127,11 +138,16 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
if(!doc.Parse<0>(content.c_str()).HasParseError())
{
cmdDataPtr->p_command_counters->succeed++;
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH)
DelegateRestResponse(cmdDataPtr->p_response,resp);
}
else
{
cmdDataPtr->p_retstat->SetError(doc.GetParseError());
cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
}
}
}
......@@ -142,6 +158,11 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOG_ERROR(ss.str());
cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH)
DelegateRestResponse(cmdDataPtr->p_response,resp);
}
}
......@@ -207,3 +228,17 @@ void MSICommandClientHttpImpl::GetMetrics(std::map<std::string, long>& metrics_m
AddCounters(metrics_map, "update", update_counters_);
AddCounters(metrics_map, "delete", delete_counters_);
}
void MSICommandClientHttpImpl::DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse,
web::http::http_response &response) {
Microservice_RestResponse* p_RestResponse = (Microservice_RestResponse*)pResponse;
p_RestResponse->setResponse_code(response.status_code());
auto& headersMap = p_RestResponse->getHeaderMap();
for(auto header : response.headers())
{
std::string key = header.first;
p_RestResponse->addHeader(key,header.second);
}
}
......@@ -16,6 +16,7 @@
#include "../../Microservice_Iface.h"
#include <atomic>
#include <cpprest/http_msg.h>
using namespace nsMicroservice_Iface;
......@@ -88,6 +89,8 @@ private:
void AddCounters(std::map<std::string, long>& metrics_map,
const char* name,
CommandCounters& cmd_counters);
void DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse, web::http::http_response &response);
};
......
......@@ -104,7 +104,7 @@ void MSICommandClientRMQImpl::HandleCommand(HandleCommandData* p_cmd_data)
// content
message.setContent(p_cmd_data->p_cmd_params->GetContent());
// headers
for (auto header : *p_cmd_data->p_cmd_params->GetHeadersMap())
for (auto header : p_cmd_data->p_cmd_params->GetHeadersMap())
{
message.setHeader(cNameValuePair(header.first, header.second));
}
......@@ -188,9 +188,9 @@ std::string MSICommandClientRMQImpl::BuildPath(MSCommandParams* p_cmd_params) {
return path;
// params
if(p_cmd_params->GetParams())
if(!p_cmd_params->GetParams().empty())
{
for(auto param : *p_cmd_params->GetParams())
for(auto param : p_cmd_params->GetParams())
{
unencoded_path.append(1,'/') .append(param.c_str());
}
......
......@@ -22,16 +22,16 @@ class MSCommandParams
{
private:
std::string entity_;
std::vector<std::string>* p_params_;
std::vector<std::string> params_;
std::string params_string_; // params as a continues string "p1/p2/p3"
std::string request_params_;
std::string content_;
std::map<std::string,std::string>* p_headers_map_;
std::map<std::string,std::string> headers_map_;
bool async_;
public:
MSCommandParams():p_params_(nullptr),p_headers_map_(nullptr) {
MSCommandParams() {
async_ = false;
}
......@@ -41,7 +41,7 @@ public:
* @param params_string_
* @param request_params_
* @param content_
* @param p_headers_map_
* @param headers_map_
* @param async
*/
MSCommandParams(std::string entity,
......@@ -50,14 +50,13 @@ public:
std::string content,
std::map<std::string, std::string>* p_headers_map,
bool async = false) :
entity_(entity), params_string_(params_string), request_params_(request_params), content_(content), p_headers_map_(p_headers_map) {
p_params_ = nullptr;
entity_(entity), params_string_(params_string), request_params_(request_params), content_(content), headers_map_(*p_headers_map) {
async_ = async;
}
MSCommandParams(std::string entity_, std::vector<std::string>* p_params_, std::string request_params_, std::string content_, std::map<std::string, std::string>* p_headers_map_, bool async = false) :
entity_(entity_), p_params_(p_params_), request_params_(request_params_), content_(content_), p_headers_map_(p_headers_map_) {
params_string_ = nullptr;
MSCommandParams(std::string entity_, std::vector<std::string>* p_params_, std::string request_params_, std::string content_, std::map<std::string, std::string>* p_headers_map, bool async = false) :
entity_(entity_), params_(*p_params_), request_params_(request_params_), content_(content_), headers_map_(*p_headers_map) {
//params_string_ = nullptr;
async_ = async;
}
......@@ -69,12 +68,12 @@ public:
return entity_;
}
std::map<std::string, std::string>* GetHeadersMap() const {
return p_headers_map_;
std::map<std::string, std::string>& GetHeadersMap() {
return headers_map_;
}
std::vector<std::string>* GetParams() const {
return p_params_;
std::vector<std::string>& GetParams() {
return params_;
}
std::string& GetParamsString() {
......@@ -96,9 +95,9 @@ public:
MSCommandParams& WithContent(std::string& content) { this->content_.assign(content); return *this; }
MSCommandParams& WithContent(const char* p_content) { this->content_.assign(p_content); return *this; }
MSCommandParams& WithHeadersMap(std::map<std::string, std::string>* p_headers_map) { this->p_headers_map_ = p_headers_map; return *this; }
MSCommandParams& WithHeadersMap(std::map<std::string, std::string>* p_headers_map) { this->headers_map_ = *p_headers_map; return *this; }
MSCommandParams& WithParams(std::vector<std::string>* p_params) { this->p_params_ = p_params; return *this; }
MSCommandParams& WithParams(std::vector<std::string>* p_params) { this->params_ = *p_params; return *this; }
MSCommandParams& WithParamsString(std::string& params_string) { this->params_string_.assign(params_string); return *this; }
MSCommandParams& WithParamsString(const char* p_params_string) { this->params_string_.assign(p_params_string); return *this; }
......
......@@ -21,6 +21,7 @@
#include <iostream>
#include <utils/ScheduledTimer.h>
#include <pplx/pplxtasks.h>
#include <common/Microservice_RestResponse.h>
class cMicroserviceHandler: public cMicroservice_BaseHandler
{
......@@ -58,8 +59,8 @@ public:
rpj_Doc.AddMember(it->first.c_str(),dequeIt->c_str(),rpj_Alloc);
}
}
//ReadSync(pc_reqCtx);
ReadAsync2(pc_reqCtx);
ReadSync(pc_reqCtx);
//ReadAsync2(pc_reqCtx);
//this->WriteObjectToResponse(pc_reqCtx,rpj_Doc);
// add metric
long value = rand() % 1000 + 1;
......@@ -67,12 +68,16 @@ public:
}
void ReadSync(cMicroservice_RequestContext *pc_reqCtx) {
cMicroservice_BaseRestResponse rest_response;
Microservice_RestResponse rest_response;
std::map<std::string,std::string> headers;
headers["X-IPgallery"] = "OK";
MSCommandParams cmd_params;
cmd_params
.WithEntity("http://172.16.1.132:5000/v1")
.WithParamsString("search")
.WithRequestParams("q=base");
.WithRequestParams("q=base")
.WithHeadersMap(&headers);
MSRetStat retstat = p_client_->Read(&cmd_params, &rest_response);
if(retstat.IsSuccess())
WriteObjectToResponse(pc_reqCtx, rest_response);
......@@ -89,6 +94,10 @@ public:
}
/**
* reading async
* @param pc_reqCtx
*/
void ReadAsync(cMicroservice_RequestContext* pc_reqCtx)
{
ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container);
......@@ -96,14 +105,21 @@ public:
.WithParamsString("search")
.WithRequestParams("q=base");
try {
auto readTask = p_client_->AsyncRead(clientAsyncTaskParamsPtr); //&cmd_params,&rest_response);
readTask.then([clientAsyncTaskParamsPtr](MSRetStat retStat){
if(retStat.IsSuccess())
clientAsyncTaskParamsPtr->p_IContainer_->WriteObjectToResponse(clientAsyncTaskParamsPtr->p_IResponse_.get(),
readTask.then([clientAsyncTaskParamsPtr](MSRetStat retStat) {
if (retStat.IsSuccess())
clientAsyncTaskParamsPtr->p_IContainer_->WriteObjectToResponse(
clientAsyncTaskParamsPtr->p_IResponse_.get(),
*clientAsyncTaskParamsPtr->p_baseRestResoonse_);
else
clientAsyncTaskParamsPtr->p_IContainer_->SendErrorResp(clientAsyncTaskParamsPtr->p_IResponse_.get(),retStat.GetError());
clientAsyncTaskParamsPtr->p_IContainer_->SendErrorResp(clientAsyncTaskParamsPtr->p_IResponse_.get(),
retStat.GetError());
});
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
std::cout << " after\n";
}
......@@ -114,6 +130,8 @@ public:
*/
void ReadAsync2(cMicroservice_RequestContext* pc_reqCtx)
{
try {
/**
* task 1
*/
......@@ -153,6 +171,10 @@ public:
rpj_Doc.AddMember("second",clientAsyncTaskParamsPtr2->p_baseRestResoonse_->GetObjectNode()["results"],rpj_Alloc);
clientAsyncTaskParamsPtr1->p_IContainer_->WriteObjectToResponse(clientAsyncTaskParamsPtr1->p_IResponse_.get(),rpj_Doc);
});
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
std::cout << " after\n";
}
......@@ -170,6 +192,7 @@ public:
void runNewMS(){
cMicroservice_BaseClientParams clientParams("other-service", true, 10, false,"localhost",32010,"localhost:6379");
cMicroservice_App msApp("myCppService");
......@@ -185,32 +208,6 @@ void runNewMS(){
.run();
}
//void runOldMS(char** argv){
// int port = atoi(argv[3]);
// std::string host(argv[2]);
// cMicroservice_RestServerParams* pc_RsiParams = new cMicroservice_RestServerParams(port,host,1);
//
//
//
// cMicroservice_RMQServerParams* pc_MbiParams = NULL;
// const char* pba_AppName = argv[1];
// cMicroserviceHandler c_MSH(argv[5]);
//
// cMicroservice_App* pc_App = new cMicroservice_App(pc_RsiParams,pc_MbiParams,pba_AppName);
// pc_App->AddHandler(argv[4],&c_MSH);
//
// // start
// printf("Starting App...\n");
// pc_App->StartApp();
// printf("Started Waiting for CTRL-C...\n");
//
// // pause
// pause();
// printf("Stopping App...\n");
// //
// pc_App->StopApp();
//}
void testCache(){
using CacheClient = nsMicroservice_Iface::ICacheClient;
using Str = std::string;
......
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