#include "include/dao.h" using namespace std; Dao::Dao() { this->api_key = getenv("FRONTEND_API_KEY"); if (this->api_key.empty()) { cout << "FRONTEND_API_KEY environment variable is not set" << endl; exit(1); } this->base_url = getenv("BASE_API_URL"); if (this->base_url.empty()) { cout << "BASE_API_URL environment variable is not set" << endl; exit(1); } } static size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) { size_t total = size * nmemb; std::string* s = static_cast(userp); s->append(static_cast(contents), total); return total; } ticket_t Dao::fetch_ticket_create(std::string title, std::string description) { string response; string url = this->base_url + "/tickets"; CURL *handle = curl_easy_init(); struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, ("Authorization: Bearer " + this->api_key).c_str()); string body = nlohmann::json{ {"title", title}, {"description", description} }.dump(); curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_POST, 1L); curl_easy_setopt(handle, CURLOPT_POSTFIELDS, body.c_str()); curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, static_cast(body.size())); curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response); CURLcode rc = curl_easy_perform(handle); curl_slist_free_all(headers); curl_easy_cleanup(handle); if (rc != CURLE_OK) { throw std::runtime_error(std::string("curl_easy_perform failed: ") + curl_easy_strerror(rc)); } nlohmann::json j = nlohmann::json::parse(response); ticket_t t = j.get(); return t; } vector Dao::fetch_ticket_get_all() { string response; string url = this->base_url + "/tickets"; CURL *handle = curl_easy_init(); struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, ("Authorization: Bearer " + this->api_key).c_str()); curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response); CURLcode rc = curl_easy_perform(handle); curl_slist_free_all(headers); curl_easy_cleanup(handle); if (rc != CURLE_OK) { throw std::runtime_error(std::string("curl_easy_perform failed: ") + curl_easy_strerror(rc)); } nlohmann::json j = nlohmann::json::parse(response); vector t = j.get>(); return t; } vector Dao::fetch_ticket_get_by_status(string status) { } ticket_t Dao::fetch_ticket_get_by_id(unsigned long id) { }