/* * @Author: error: git config user.name && git config user.email & please set dead value or install git * @Date: 2022-07-10 16:34:00 * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git * @LastEditTime: 2022-07-10 22:43:12 * @FilePath: /gsd/plugins/httpHelper.cpp * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ /** * * httpHelper.cc * */ #include "httpHelper.hpp" #include "HttpNull.hpp" #include "HttpToken.hpp" #include "HttpResultMsg.hpp" using namespace std; using namespace drogon; void httpHelper::initAndStart(const Json::Value &config) { /// Initialize and start the plugin } void httpHelper::shutdown() { /// Shutdown the plugin } /** * @description: 创建错误恢复 * @return {*} */ HttpResponsePtr httpHelper::makeWrongReply(std::string data){ HttpResultMsg msg; HttpResponsePtr resp; resp = HttpResponse::newHttpResponse(); msg.code = "502"; msg.status = "Failure"; msg.msg = data; msg.attr1 = "服务不可用"; std::string json; msg.objectToJson(json); resp->setStatusCode(k502BadGateway); resp->setBody(json); return resp; } /** * @description: 回复请求 * @param {pair} result * @param {function} & * @return {*} */ HttpResponsePtr httpHelper::replyRequest(std::pair result, const HttpMethod method){ HttpResultMsg msg; HttpResponsePtr resp; resp = HttpResponse::newHttpResponse(); std::string json; if(result.first != ReqResult::Ok){ ErrorL << "error while sending request to server! result " << result.first << std::endl; msg.code = "502"; msg.status = "Failure"; msg.attr1 = "服务不可用"; msg.objectToJson(json); resp->setBody(json); return resp; } resp->setStatusCode(result.second->getStatusCode()); msg.code = std::to_string(result.second->getStatusCode()); msg.status = "Failure"; msg.attr1 = result.second->getBody().data(); switch (method) { case Get: if(result.second->getStatusCode() == k200OK) msg.status = "Success"; break; case Post: if(result.second->getStatusCode() == k201Created) msg.status = "Success"; break; case Delete: if(result.second->getStatusCode() == k200OK) msg.status = "Success"; break; case Put: if(result.second->getStatusCode() == k200OK) msg.status = "Success"; break; default: break; } msg.attr1 = result.second->getBody().data(); msg.objectToJson(json); resp->setBody(json); return resp; }