mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 10:07:59 +02:00
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:
@ -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/HTTPClientSession.h>
|
||||||
#include <Poco/Net/HTTPRequest.h>
|
#include <Poco/Net/HTTPRequest.h>
|
||||||
#include <Poco/Net/HTTPResponse.h>
|
#include <Poco/Net/HTTPResponse.h>
|
||||||
@ -13,9 +23,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
// HTTP timeout in seconds, default is 8
|
/// HTTP timeout in seconds, default is 8
|
||||||
#define HTTP_TIMEOUT 10
|
#define HTTP_TIMEOUT 10
|
||||||
// Number of connection tries
|
/// Number of connection tries
|
||||||
#define N_CONNECTION_TRIES 3
|
#define N_CONNECTION_TRIES 3
|
||||||
|
|
||||||
using namespace Poco::Net;
|
using namespace Poco::Net;
|
||||||
@ -28,57 +38,140 @@ class RestHelper {
|
|||||||
~RestHelper(){};
|
~RestHelper(){};
|
||||||
|
|
||||||
void init(string hostname, int port){
|
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;
|
full_hostname = "http://"+hostname;
|
||||||
session = new HTTPClientSession(hostname,port );
|
session = new HTTPClientSession(hostname,port );
|
||||||
session->setKeepAliveTimeout( Timespan( HTTP_TIMEOUT,0) );
|
session->setKeepAliveTimeout( Timespan( HTTP_TIMEOUT,0) );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int get_json(string request, string* answer){
|
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 * uri = new URI(full_hostname+"/"+request);
|
||||||
uri = new URI(full_hostname+"/"+request);
|
|
||||||
string path(uri->getPathAndQuery());
|
string path(uri->getPathAndQuery());
|
||||||
if (path.empty()) path = "/";
|
if (path.empty()) path = "/";
|
||||||
|
|
||||||
// send request
|
// send request
|
||||||
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
||||||
|
req.setContentType("application/json\r\n");
|
||||||
int code = send_request(session, req, answer);
|
int code = send_request(session, req, answer);
|
||||||
|
delete uri;
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int get_json(string request, JsonBox::Value* json_value){
|
int get_json(string request, JsonBox::Value* json_value){
|
||||||
/*
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @param json_value
|
||||||
|
*
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
|
URI *uri = new URI(full_hostname+"/"+request);
|
||||||
uri = new URI(full_hostname+"/"+request);
|
|
||||||
string path(uri->getPathAndQuery());
|
string path(uri->getPathAndQuery());
|
||||||
if (path.empty()) path = "/";
|
if (path.empty()) path = "/";
|
||||||
// send request
|
// send request
|
||||||
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
||||||
|
req.setContentType("application/json\r\n");
|
||||||
string answer;
|
string answer;
|
||||||
int code = send_request(session, req, &answer);
|
int code = send_request(session, req, &answer);
|
||||||
json_value->loadFromString(answer);
|
json_value->loadFromString(answer);
|
||||||
|
delete uri;
|
||||||
return code;
|
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:
|
private:
|
||||||
URI * uri;
|
//URI * uri;
|
||||||
HTTPClientSession *session;
|
HTTPClientSession *session;
|
||||||
string full_hostname;
|
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 n=0;
|
||||||
int code = -1;
|
int code = -1;
|
||||||
while(n<N_CONNECTION_TRIES){
|
while(n<N_CONNECTION_TRIES){
|
||||||
try {
|
try {
|
||||||
session->sendRequest( (req) );
|
if (request_body == "")
|
||||||
|
session->sendRequest( (req) );
|
||||||
|
else
|
||||||
|
session->sendRequest( (req) ) << request_body;
|
||||||
|
|
||||||
HTTPResponse res;
|
HTTPResponse res;
|
||||||
istream &is = session->receiveResponse(res);
|
istream &is = session->receiveResponse(res);
|
||||||
StreamCopier::copyToString(is, *answer);
|
StreamCopier::copyToString(is, *answer);
|
||||||
|
@ -69,37 +69,46 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
//REST call - hardcoded
|
//REST call - hardcoded
|
||||||
RestHelper rest ;
|
RestHelper rest ;
|
||||||
rest.init("localhost",8080);
|
rest.init("localhost",8080);
|
||||||
std::string answer;
|
std::string answer;
|
||||||
std::cout << "---- REST test 1: true, string "<< std::endl;
|
std::cout << "---- REST test 1: true, string "<< std::endl;
|
||||||
int code = rest.get_json("status", &answer);
|
int code = rest.get_json("status", &answer);
|
||||||
std::cout << "Answer: " << answer << std::endl;
|
std::cout << "Answer: " << answer << std::endl;
|
||||||
|
|
||||||
std::cout << "---- REST test 2: 404, string "<< std::endl;
|
std::cout << "---- REST test 2: 404, string "<< std::endl;
|
||||||
code = rest.get_json("statuss", &answer);
|
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){
|
if (code != 0){
|
||||||
//throw -1;
|
//throw -1;
|
||||||
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
|
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;
|
|
||||||
|
|
||||||
|
|
||||||
RestHelper rest2 ;
|
RestHelper rest2 ;
|
||||||
rest2.init("reallyfake",8080);
|
rest2.init("reallyfake",8080);
|
||||||
std::cout << "---- REST test 4: host not found, json object "<< std::endl;
|
std::cout << "---- REST test 4: host not found, json object "<< std::endl;
|
||||||
JsonBox::Value json_value2;
|
JsonBox::Value json_value2;
|
||||||
code = rest2.get_json("status", &json_value2);
|
code = rest2.get_json("status", &json_value2);
|
||||||
if (code != 0){
|
if (code != 0){
|
||||||
//throw -1;
|
//throw -1;
|
||||||
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
|
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
char *getDetectorHostname() const {
|
char *getDetectorHostname() const {
|
||||||
string name = init_config.detectorHostname;
|
string name = init_config.detectorHostname;
|
||||||
|
Reference in New Issue
Block a user