Skip to content

Commit

Permalink
add support for user data
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamTambellini committed Dec 7, 2015
1 parent a5dae07 commit 9cfe5e3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
14 changes: 10 additions & 4 deletions rest/RESTCallBack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ void RESTCallBack::getDescription(Dumais::JSON::JSON& json)
}
}

RESTEngine::ResponseCode RESTCallBack::call(Dumais::JSON::JSON& json, const std::string& paramString, const std::string& dataString, std::smatch& matches)
RESTEngine::ResponseCode RESTCallBack::call(Dumais::JSON::JSON& json, const std::string& paramString,
const std::string& dataString, std::smatch& matches,
std::shared_ptr<void> userData)
{
RESTParameters params(paramString, mParamList);
// Let's check mandatory/required params
// Let's check required params
for (ParamMap::const_iterator it = mParamList.begin(); it != mParamList.end(); ++it) {
if (it->second.mRequired) {
// todo later : handle/check in "path" parameters: http://server:port/param1/param2
if (it->second.mRequired && (it->second.mLocation == "query") ) {
if (params.getParam(it->first).empty()) {
std::cerr << "Missing mandatory parameter " << it->first << std::endl;
// todo : replace empty parameter value by default parameter value
json.addValue("Missing required parameter " + it->first, "error");
return RESTEngine::ResponseCode::BadRequest;
}
}
}
Expand All @@ -44,7 +49,8 @@ RESTEngine::ResponseCode RESTCallBack::call(Dumais::JSON::JSON& json, const std:
&params,
dataString,
matches,
responseCode
responseCode,
userData
};
assert(mCallback);
if (mCallback)
Expand Down
5 changes: 3 additions & 2 deletions rest/RESTCallBack.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct RESTContext
const std::string& data;
std::smatch& matches;
RESTEngine::ResponseCode &responseCode;
std::shared_ptr<void> userData;
};

class RESTCallBack
Expand Down Expand Up @@ -62,6 +63,6 @@ class RESTCallBack
j.addValue(it->second.mDefaultValue, "default");
}
}
RESTEngine::ResponseCode call(Dumais::JSON::JSON& json, const std::string& paramString, const std::string& data, std::smatch&);
RESTEngine::ResponseCode call(Dumais::JSON::JSON& json, const std::string& paramString,
const std::string& data, std::smatch&, std::shared_ptr<void> userData = NULL);
};

5 changes: 3 additions & 2 deletions rest/RESTEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void RESTEngine::removeCallBack(RESTCallBack* p)
}

RESTEngine::ResponseCode RESTEngine::invoke(Dumais::JSON::JSON& j, std::string url,
const std::string& method, const std::string& data) const
const std::string& method, const std::string& data,
std::shared_ptr<void> userData) const
{
std::string tmp = method;
std::transform(tmp.begin(),tmp.end(),tmp.begin(),::toupper);
Expand Down Expand Up @@ -93,7 +94,7 @@ RESTEngine::ResponseCode RESTEngine::invoke(Dumais::JSON::JSON& j, std::string u

if (p)
{
return p->call(j,urlPart2, data, matches);
return p->call(j,urlPart2, data, matches, userData);
}
return NotFound;
}
Expand Down
3 changes: 2 additions & 1 deletion rest/RESTEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class RESTEngine{

void addCallBack(std::string url, std::string method, RESTCallBack* p);
void removeCallBack(RESTCallBack* p);
ResponseCode invoke(Dumais::JSON::JSON& j,std::string url, const std::string& method, const std::string& data) const;
ResponseCode invoke(Dumais::JSON::JSON& j, std::string url, const std::string& method, const std::string& data,
std::shared_ptr<void> userData = NULL) const;

void documentInterface(Dumais::JSON::JSON& j);

Expand Down
22 changes: 19 additions & 3 deletions tests/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,19 @@ class class1
}
void c4(RESTContext* context)
{
context->responseCode = (RESTEngine::ResponseCode) 3;
context->responseCode = (RESTEngine::ResponseCode) 99;
}
void c5(RESTContext* context)
{
if (!context->userData.get()) {
std::cerr << "Failed to retrieve user data for this call" << std::endl;
context->responseCode = RESTEngine::ResponseCode::ServiceUnavailable;
}
else {
// the user data has been successfully transferred from the invoke() to this callback implementation.
}
}

};


Expand All @@ -45,6 +56,7 @@ int main(int argc, char**argv)
RESTCallBack *pc3 = new RESTCallBack(p,&class1::c2,"description 2");
RESTCallBack *pc4 = new RESTCallBack(p,&class1::c3,"description 4");
RESTCallBack *pc5 = new RESTCallBack(p,&class1::c4,"description 5");
RESTCallBack *pc6 = new RESTCallBack(p,&class1::c5,"description 6");

pc1->addParam("p1","p1 description");
pc1->addParam("p2","p1 description");
Expand All @@ -59,10 +71,11 @@ int main(int argc, char**argv)
engine.addCallBack("/test2/t.*","DELETE",pc3);
engine.addCallBack("/test3/([a-z0-9]*)","POST",pc4);
engine.addCallBack("/test4", "GET", pc5);
engine.addCallBack("/test5", "GET", pc6);

Dumais::JSON::JSON j;
RESTEngine::ResponseCode rc;
rc = engine.invoke(j,"/test1/blah?p1=test1 ","GET","");
rc = engine.invoke(j,"/test1/blah?p1=test1&p2=test2 ","GET","");
CHECK(rc == RESTEngine::OK);
rc = engine.invoke(j,"/test2/blah2?p1=test1&p2=test2&p3=test3&p4=test4","GET","");
CHECK(rc == RESTEngine::OK);
Expand All @@ -73,7 +86,10 @@ int main(int argc, char**argv)
rc = engine.invoke(j,"/test3/product144","POST","");
CHECK(rc == RESTEngine::OK);
rc = engine.invoke(j,"/test4","GET","");
CHECK(rc == 3);
CHECK(rc == 99);
std::shared_ptr<int> myUserData = std::make_shared<int>(999);
rc = engine.invoke(j,"/test5","GET","", myUserData);
CHECK(rc == RESTEngine::OK);

Dumais::JSON::JSON json;
engine.documentInterface(json);
Expand Down

0 comments on commit 9cfe5e3

Please sign in to comment.