1/*************************************************************************
2 * libjson-rpc-cpp
3 *************************************************************************
4 * @file responsehandler.h
5 * @date 13.03.2013
6 * @author Peter Spiess-Knafl <dev@spiessknafl.at>
7 * @license See attached LICENSE.txt
8 ************************************************************************/
9
10#ifndef RESPONSEHANDLER_H
11#define RESPONSEHANDLER_H
12
13#include "client.h"
14#include <jsonrpccpp/common/exception.h>
15#include <jsonrpccpp/common/jsonparser.h>
16#include <string>
17
18namespace jsonrpc {
19
20 /**
21 * @brief The RpcProtocolClient class handles the json-rpc 2.0 protocol for the client side.
22 */
23 class RpcProtocolClient {
24 public:
25 RpcProtocolClient(clientVersion_t version = JSONRPC_CLIENT_V2, bool omitEndingLineFeed = false);
26
27 /**
28 * @brief This method builds a valid json-rpc 2.0 request object based on passed parameters.
29 * The id starts at 1 and is incremented for each request. To reset this value to one, call
30 * the jsonrpc::RpcProRpcProtocolClient::resetId() method.
31 * @param method - name of method or notification to be called
32 * @param parameter - parameters represented as json objects
33 * @return the string representation of the request to be built.
34 */
35 std::string BuildRequest(const std::string &method, const Json::Value &parameter, bool isNotification);
36
37 /**
38 * @brief BuildRequest does the same as std::string jsonrpc::RpcProRpcProtocolClient::BuildRequest(const std::string& method, const Json::Value& parameter);
39 * The only difference here is that the result is returend by value, using the result parameter.
40 * @param method - name of method or notification to be called
41 * @param parameter - parameters represented as json objects
42 * @param result - the string representation will be hold within this reference.
43 */
44 void BuildRequest(const std::string &method, const Json::Value &parameter, std::string &result, bool isNotification);
45
46 /**
47 * @brief Does the same as Json::Value RpcProtocolClient::HandleResponse(const std::string& response) throw(Exception)
48 * but returns result as reference for performance speed up.
49 */
50 void HandleResponse(const std::string &response, Json::Value &result);
51
52 /**
53 * @brief HandleResponse
54 * @param response
55 * @param result
56 * @return response id
57 */
58 Json::Value HandleResponse(const Json::Value &response, Json::Value &result);
59
60 static const std::string KEY_PROTOCOL_VERSION;
61 static const std::string KEY_PROCEDURE_NAME;
62 static const std::string KEY_ID;
63 static const std::string KEY_PARAMETER;
64 static const std::string KEY_AUTH;
65 static const std::string KEY_RESULT;
66 static const std::string KEY_ERROR;
67 static const std::string KEY_ERROR_CODE;
68 static const std::string KEY_ERROR_MESSAGE;
69 static const std::string KEY_ERROR_DATA;
70
71 private:
72 clientVersion_t version;
73 bool omitEndingLineFeed;
74
75 void BuildRequest(int id, const std::string &method, const Json::Value &parameter, Json::Value &result, bool isNotification);
76 bool ValidateResponse(const Json::Value &response);
77 bool HasError(const Json::Value &response);
78 void throwErrorException(const Json::Value &response);
79 };
80} // namespace jsonrpc
81#endif // RESPONSEHANDLER_H
82