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; }
......
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