两个字:巨烂
1、把成功字符串和拼凑的错误原因字符串放在同一个返回值里;
2、有两三百处调用这个函数,到处都要改调用代码;
3、无缓存层(只缓存了etag,没缓存返回的成功串);
4、http 304时,调用者还要判断一下字符串是不是“Content not modified”,此时要使用调用者自己缓存的上次的成功字符串;
5、http 404应该是给服务端擦屁股的(估计服务端在负载大时可能返回404,才那么写);
骨架大概长这样。貌似AI也没有特别令人满意的招数,难以避免修改那几百处调用的代码。
std::string HttpPost(const std::string &url, const std::string &body1, const bool use_etag) {
std::stringstream result;
try {
std::string err;
auto reqBody = EncryptMessage(body1, err, kDefaultCryptProtocol);
if (!err.empty()) {
CLOG4_DEBUG("[HttpPost] encrypt message failed due to " << err);
return "###Error### encrypt message failed";
}
...
long responseCode = curlpp::infos::ResponseCode::get(request.GetCurl());
switch (responseCode) {
case 200:
break;
case 304:
case 404:
return std::string("Content not modified");
default:
return std::string("###Error### Cloud Error Code: ") + std::to_string(responseCode);
}
} catch (curlpp::LogicError &e) {
CLOG4_DEBUG("[HttpPost] LogicError " << e.what());
return std::string("###Error###") + e.what();
} catch (curlpp::RuntimeError &e) {
CLOG4_DEBUG("[HttpPost] RuntimeError " << e.what());
return std::string("###Error###") + e.what();
}
...
std::string err;
auto plaintext = DecryptMessage(result.view(), err);
if (!err.empty()) {
CLOG4_DEBUG("[HttpPost] decrypt message failed due to " << err);
return "###Error### decrypt message failed";
}
return plaintext;
}
--
FROM 111.199.147.*