Added post_json methods

Added DoxyGen docs skeleton
Added Content-type

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@802 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
sala
2014-03-25 10:06:31 +00:00
parent 7dccc4fc1a
commit b743ab2d04
2 changed files with 145 additions and 43 deletions

View File

@ -1,3 +1,13 @@
/**
* @file RestHelper.h
* @author Leonardo Sala <leonardo.sala@psi.ch>
* @date Tue Mar 25 09:28:19 2014
*
* @brief
*
*
*/
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
@ -13,9 +23,9 @@
#include <string>
#include <exception>
// HTTP timeout in seconds, default is 8
/// HTTP timeout in seconds, default is 8
#define HTTP_TIMEOUT 10
// Number of connection tries
/// Number of connection tries
#define N_CONNECTION_TRIES 3
using namespace Poco::Net;
@ -28,57 +38,140 @@ class RestHelper {
~RestHelper(){};
void init(string hostname, int port){
/*
/** Initialize the RestHelper. Hostname and port parameters are not supposed to change.
*
*
* @param hostname FQDN of the host to connect to , e.g. www.iamfake.org, or sodoi.org
* @param port
*
* @return
*/
full_hostname = "http://"+hostname;
session = new HTTPClientSession(hostname,port );
session->setKeepAliveTimeout( Timespan( HTTP_TIMEOUT,0) );
};
int get_json(string request, string* answer){
/*
/** Retrieves a reply from the RESTful webservice.
*
*
* @param request Request without the hostname, e.g. if the full request would have been http://fake.org/fakemethod, request=fakemethod
* @param answer
*
* @return 0 if successful, -1 if failure happens.
*/
uri = new URI(full_hostname+"/"+request);
URI * uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.setContentType("application/json\r\n");
int code = send_request(session, req, answer);
delete uri;
return code;
};
int get_json(string request, JsonBox::Value* json_value){
/*
/**
*
*
* @param request
* @param json_value
*
* @return
*/
uri = new URI(full_hostname+"/"+request);
URI *uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.setContentType("application/json\r\n");
string answer;
int code = send_request(session, req, &answer);
json_value->loadFromString(answer);
delete uri;
return code;
};
int post_json(string request, string *answer, string request_body=""){
/**
*
*
* @param request
* @param answer
* @param request_body Eventual arguments to the URL, e.g. action=login&name=mammamia
*
* @return
*/
//from: http://stackoverflow.com/questions/1499086/poco-c-net-ssl-how-to-post-https-request
URI *uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1 );
req.setContentType("application/json\r\n");
req.setContentLength( request.length() );
int code = send_request(session, req, answer, request_body);
delete uri;
return code;
}
int post_json(string request, JsonBox::Value* json_value, string request_body=""){
/**
*
*
* @param request
* @param json_value
* @param request_body Eventual arguments to the URL, e.g. action=login&name=mammamia
*
* @return
*/
URI *uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1 );
req.setContentType("application/json\r\n");
req.setContentLength( request.length() );
string answer;
int code = send_request(session, req, &answer, request_body);
json_value->loadFromString(answer);
delete uri;
return code;
}
private:
URI * uri;
//URI * uri;
HTTPClientSession *session;
string full_hostname;
int send_request(HTTPClientSession *session, HTTPRequest &req, string *answer){
int send_request(HTTPClientSession *session, HTTPRequest &req, string *answer, string request_body=""){
/**
*
*
* @param session
* @param req
* @param answer
* @param request_body
*
* @return
*/
int n=0;
int code = -1;
while(n<N_CONNECTION_TRIES){
try {
session->sendRequest( (req) );
if (request_body == "")
session->sendRequest( (req) );
else
session->sendRequest( (req) ) << request_body;
HTTPResponse res;
istream &is = session->receiveResponse(res);
StreamCopier::copyToString(is, *answer);

View File

@ -69,37 +69,46 @@ public:
}
//REST call - hardcoded
RestHelper rest ;
rest.init("localhost",8080);
std::string answer;
std::cout << "---- REST test 1: true, string "<< std::endl;
int code = rest.get_json("status", &answer);
std::cout << "Answer: " << answer << std::endl;
RestHelper rest ;
rest.init("localhost",8080);
std::string answer;
std::cout << "---- REST test 1: true, string "<< std::endl;
int code = rest.get_json("status", &answer);
std::cout << "Answer: " << answer << std::endl;
std::cout << "---- REST test 2: 404, string "<< std::endl;
code = rest.get_json("statuss", &answer);
std::cout << "---- REST test 2: 404, string "<< std::endl;
code = rest.get_json("statuss", &answer);
if (code != 0){
//throw -1;
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
}
std::cout << "---- REST test 3: true, json object "<< std::endl;
JsonBox::Value json_value;
code = rest.get_json("status", &json_value);
std::cout << "JSON " << json_value["status"] << std::endl;
answer = "";
std::cout << "---- REST test 4: POST, string "<< std::endl;
code = rest.post_json("recipes/cassoela", &answer);
std::cout << "POST answer: " << answer << std::endl;
if (code != 0){
//throw -1;
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
}
std::cout << "---- REST test 3: true, json object "<< std::endl;
JsonBox::Value json_value;
code = rest.get_json("status", &json_value);
std::cout << "JSON " << json_value["status"] << std::endl;
//throw -1;
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
}
RestHelper rest2 ;
rest2.init("reallyfake",8080);
std::cout << "---- REST test 4: host not found, json object "<< std::endl;
JsonBox::Value json_value2;
code = rest2.get_json("status", &json_value2);
if (code != 0){
//throw -1;
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
}
rest2.init("reallyfake",8080);
std::cout << "---- REST test 4: host not found, json object "<< std::endl;
JsonBox::Value json_value2;
code = rest2.get_json("status", &json_value2);
if (code != 0){
//throw -1;
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
}
}
}
char *getDetectorHostname() const {
string name = init_config.detectorHostname;