From b743ab2d0444ffa444a5ce1bebc7379f28108322 Mon Sep 17 00:00:00 2001 From: sala Date: Tue, 25 Mar 2014 10:06:31 +0000 Subject: [PATCH] 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 --- slsDetectorSoftware/slsReceiver/RestHelper.h | 127 +++++++++++++++--- .../slsReceiver/eigerReceiver.cpp | 61 +++++---- 2 files changed, 145 insertions(+), 43 deletions(-) diff --git a/slsDetectorSoftware/slsReceiver/RestHelper.h b/slsDetectorSoftware/slsReceiver/RestHelper.h index 9f9804cfb..6f423f5e1 100644 --- a/slsDetectorSoftware/slsReceiver/RestHelper.h +++ b/slsDetectorSoftware/slsReceiver/RestHelper.h @@ -1,3 +1,13 @@ +/** + * @file RestHelper.h + * @author Leonardo Sala + * @date Tue Mar 25 09:28:19 2014 + * + * @brief + * + * + */ + #include #include #include @@ -13,9 +23,9 @@ #include #include -// 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){ - /* - */ - - uri = new URI(full_hostname+"/"+request); + 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); 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){ - /* - */ - - uri = new URI(full_hostname+"/"+request); + int get_json(string request, JsonBox::Value* json_value){ + /** + * + * + * @param request + * @param json_value + * + * @return + */ + 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(nsendRequest( (req) ); + if (request_body == "") + session->sendRequest( (req) ); + else + session->sendRequest( (req) ) << request_body; + HTTPResponse res; istream &is = session->receiveResponse(res); StreamCopier::copyToString(is, *answer); diff --git a/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp b/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp index b05980a23..c30915292 100644 --- a/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp +++ b/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp @@ -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;