| 1 | /************************************************************************* |
|---|---|
| 2 | * libjson-rpc-cpp |
| 3 | ************************************************************************* |
| 4 | * @file batchresponse.cpp |
| 5 | * @date 10/9/2014 |
| 6 | * @author Peter Spiess-Knafl <dev@spiessknafl.at> |
| 7 | * @license See attached LICENSE.txt |
| 8 | ************************************************************************/ |
| 9 | |
| 10 | #include "batchresponse.h" |
| 11 | #include <algorithm> |
| 12 | |
| 13 | using namespace jsonrpc; |
| 14 | using namespace std; |
| 15 | |
| 16 | BatchResponse::BatchResponse() {} |
| 17 | |
| 18 | void BatchResponse::addResponse(Json::Value &id, Json::Value response, bool isError) { |
| 19 | if (isError) { |
| 20 | errorResponses.push_back(id); |
| 21 | } |
| 22 | responses[id] = response; |
| 23 | } |
| 24 | |
| 25 | Json::Value BatchResponse::getResult(int id) { |
| 26 | Json::Value result; |
| 27 | Json::Value i = id; |
| 28 | getResult(i, result); |
| 29 | return result; |
| 30 | } |
| 31 | |
| 32 | void BatchResponse::getResult(Json::Value &id, Json::Value &result) { |
| 33 | if (getErrorCode(id) == 0) |
| 34 | result = responses[id]; |
| 35 | else |
| 36 | result = Json::nullValue; |
| 37 | } |
| 38 | |
| 39 | int BatchResponse::getErrorCode(Json::Value &id) { |
| 40 | if (std::find(errorResponses.begin(), errorResponses.end(), id) != errorResponses.end()) { |
| 41 | return responses[id]["code"].asInt(); |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | string BatchResponse::getErrorMessage(Json::Value &id) { |
| 47 | if (std::find(errorResponses.begin(), errorResponses.end(), id) != errorResponses.end()) { |
| 48 | return responses[id]["message"].asString(); |
| 49 | } |
| 50 | return ""; |
| 51 | } |
| 52 | |
| 53 | string BatchResponse::getErrorMessage(int id) { |
| 54 | Json::Value i = id; |
| 55 | return getErrorMessage(i); |
| 56 | } |
| 57 | |
| 58 | bool BatchResponse::hasErrors() { return !errorResponses.empty(); } |
| 59 |