httpHelper.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * @Author: error: git config user.name && git config user.email & please set dead value or install git
  3. * @Date: 2022-07-10 16:34:00
  4. * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
  5. * @LastEditTime: 2022-07-10 22:43:12
  6. * @FilePath: /gsd/plugins/httpHelper.cpp
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. /**
  10. *
  11. * httpHelper.cc
  12. *
  13. */
  14. #include "httpHelper.hpp"
  15. #include "HttpNull.hpp"
  16. #include "HttpToken.hpp"
  17. #include "HttpResultMsg.hpp"
  18. using namespace std;
  19. using namespace drogon;
  20. void httpHelper::initAndStart(const Json::Value &config)
  21. {
  22. /// Initialize and start the plugin
  23. }
  24. void httpHelper::shutdown()
  25. {
  26. /// Shutdown the plugin
  27. }
  28. /**
  29. * @description: 创建错误恢复
  30. * @return {*}
  31. */
  32. HttpResponsePtr httpHelper::makeWrongReply(std::string data){
  33. HttpResultMsg<HttpNull, HttpNull> msg;
  34. HttpResponsePtr resp;
  35. resp = HttpResponse::newHttpResponse();
  36. msg.code = "502";
  37. msg.status = "Failure";
  38. msg.msg = data;
  39. msg.attr1 = "服务不可用";
  40. std::string json;
  41. msg.objectToJson(json);
  42. resp->setStatusCode(k502BadGateway);
  43. resp->setBody(json);
  44. return resp;
  45. }
  46. /**
  47. * @description: 回复请求
  48. * @param {pair<ReqResult, HttpResponsePtr>} result
  49. * @param {function<void (HttpResponsePtr &)>} &
  50. * @return {*}
  51. */
  52. HttpResponsePtr httpHelper::replyRequest(std::pair<ReqResult, HttpResponsePtr> result, const HttpMethod method){
  53. HttpResultMsg<HttpNull, HttpNull> msg;
  54. HttpResponsePtr resp;
  55. resp = HttpResponse::newHttpResponse();
  56. std::string json;
  57. if(result.first != ReqResult::Ok){
  58. ErrorL << "error while sending request to server! result " << result.first << std::endl;
  59. msg.code = "502";
  60. msg.status = "Failure";
  61. msg.attr1 = "服务不可用";
  62. msg.objectToJson(json);
  63. resp->setBody(json);
  64. return resp;
  65. }
  66. resp->setStatusCode(result.second->getStatusCode());
  67. msg.code = std::to_string(result.second->getStatusCode());
  68. msg.status = "Failure";
  69. msg.attr1 = result.second->getBody().data();
  70. switch (method)
  71. {
  72. case Get:
  73. if(result.second->getStatusCode() == k200OK) msg.status = "Success";
  74. break;
  75. case Post:
  76. if(result.second->getStatusCode() == k201Created) msg.status = "Success";
  77. break;
  78. case Delete:
  79. if(result.second->getStatusCode() == k200OK) msg.status = "Success";
  80. break;
  81. case Put:
  82. if(result.second->getStatusCode() == k200OK) msg.status = "Success";
  83. break;
  84. default:
  85. break;
  86. }
  87. msg.attr1 = result.second->getBody().data();
  88. msg.objectToJson(json);
  89. resp->setBody(json);
  90. return resp;
  91. }