Skip to content

Commit

Permalink
refactor: respect naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
ligerlac committed Nov 29, 2023
1 parent 4120ffe commit e668b2a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 68 deletions.
120 changes: 58 additions & 62 deletions include/nopayloadclient/realwrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,100 +32,96 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use

class CurlSession{
public:
CurlSession(const string& _url, const json& data = json{}) {
url = _url;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ans.readBuffer);
CurlSession(const string& url, const json& data = json{}) {
url_ = url;
json_str_ = data.dump();
curl_ = curl_easy_init();
curl_easy_setopt(curl_, CURLOPT_URL, url_.c_str());
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &ans_.readBuffer);
};
void executeVoid();
Answer ans;
Answer ans_;

protected:
CURL *curl;
string url;
struct curl_slist *slist1 = NULL;
string json_str;
int n_retries_;
CURL *curl_;
string url_;
string json_str_;
};

class GetSession: public CurlSession {
public:
GetSession(const string& _url, const json& data = json{}) : CurlSession(_url, data) {
GetSession(const string& url, const json& data = json{}) : CurlSession(url, data) {
};
};

class DeleteSession: public CurlSession {
public:
DeleteSession(const string& _url, const json& data = json{}) : CurlSession(_url, data) {
std::cout << "DeleteSession()" << std::endl;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
DeleteSession(const string& url, const json& data = json{}) : CurlSession(url, data) {
curl_easy_setopt(curl_, CURLOPT_CUSTOMREQUEST, "DELETE");
};
};

class PostSession: public CurlSession {
public:
PostSession(const string& _url, const json& data = json{}) : CurlSession(_url, data) {
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
json_str = data.dump();
if (json_str != "") {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
struct curl_slist *slist = NULL;
slist = curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, json_str_.c_str());
};
};

class PutSession: public PostSession {
public:
PutSession(const string& _url, const json& data = json{}) : PostSession(_url, data) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
PutSession(const string& url, const json& data = json{}) : PostSession(url, data) {
curl_easy_setopt(curl_, CURLOPT_CUSTOMREQUEST, "PUT");
};
};


class RealWrapper : public CurlWrapper {
public:
RealWrapper() {};
RealWrapper(const json& config);

// Reading
json get(const string& url);
// Writing
json del(const string& url);
json put(const string& url);
json put(const string& url, const json& data);
json post(const string& url, const json& data);

template <typename T>
json executeTemp(const string& url, const json& data = json{}) {
for(int i = 0; i < n_retries_; i++){
T cm = T(base_url_ + url, data);
cm.executeVoid();
logging::debug("cm.ans.httpCode = " + std::to_string(cm.ans.httpCode));
if (cm.ans.httpCode == 504){
int n_sleep = int(std::exp(i));
logging::debug("connection timed-out. counter: " + std::to_string(i+1));
logging::debug("sleeping for " + std::to_string(n_sleep) + " before retrying...");
std::this_thread::sleep_for(std::chrono::seconds(n_sleep));
continue;
}
json response = json::parse(cm.ans.readBuffer);
if (cm.ans.httpCode!=200){
std::string msg;
if (response.contains("name")) msg = response["name"][0];
else if (response.contains("detail")) msg = response["detail"];
else msg = response.dump();
throw DataBaseException(msg);
public:
RealWrapper() {};
RealWrapper(const json& config);

// Reading
json get(const string& url);
// Writing
json del(const string& url);
json put(const string& url);
json put(const string& url, const json& data);
json post(const string& url, const json& data);

template <typename T>
json executeTemp(const string& url, const json& data = json{}) {
for(int i = 0; i < n_retries_; i++){
T cm = T(base_url_ + url, data);
cm.executeVoid();
logging::debug("cm.ans_.httpCode = " + std::to_string(cm.ans_.httpCode));
if (cm.ans_.httpCode == 504){
int n_sleep = int(std::exp(i));
logging::debug("connection timed-out. counter: " + std::to_string(i+1));
logging::debug("sleeping for " + std::to_string(n_sleep) + " before retrying...");
std::this_thread::sleep_for(std::chrono::seconds(n_sleep));
continue;
}
json response = json::parse(cm.ans_.readBuffer);
if (cm.ans_.httpCode!=200){
std::string msg;
if (response.contains("name")) msg = response["name"][0];
else if (response.contains("detail")) msg = response["detail"];
else msg = response.dump();
throw DataBaseException(msg);
}
return response;
}
return response;
return json{};
}
return json{};
}

private:
string base_url_;
int n_retries_;
private:
string base_url_;
int n_retries_;
};


Expand Down
12 changes: 6 additions & 6 deletions src/realwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ json RealWrapper::put(const string& url, const json& data){
void CurlSession::executeVoid(){
using namespace std::chrono;
logging::debug("begin curl: " + std::to_string(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count()));
ans.res = curl_easy_perform(curl);
ans_.res = curl_easy_perform(curl_);
logging::debug("end curl: " + std::to_string(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count()));
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &ans.httpCode);
curl_easy_cleanup(curl);
logging::debug("res = " + std::to_string(ans.res));
logging::debug("readBuffer = " + ans.readBuffer);
logging::debug("httpCode = " + std::to_string(ans.httpCode));
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &ans_.httpCode);
curl_easy_cleanup(curl_);
logging::debug("res = " + std::to_string(ans_.res));
logging::debug("readBuffer = " + ans_.readBuffer);
logging::debug("httpCode = " + std::to_string(ans_.httpCode));
}

}

0 comments on commit e668b2a

Please sign in to comment.