clang-tidy : cleanup sdk

This commit is contained in:
timoreo 2023-12-11 21:17:47 +01:00
parent fb37deb948
commit edd1a90dec
Signed by: timoreo
GPG key ID: 121A72C3512BA288
2 changed files with 85 additions and 90 deletions

View file

@ -51,26 +51,27 @@ private:
uint32_t requestId = 0; //TODO make atomic uint32_t requestId = 0; //TODO make atomic
bool stopSyncing = false; bool stopSyncing = false;
bool isSyncing = false; bool isSyncing = false;
Thread syncThread; Thread syncThread = nullptr;
olm::Account acc; olm::Account acc;
struct { struct {
eventCallback event = NULL; eventCallback event = nullptr;
eventCallback leaveRoom = NULL; eventCallback leaveRoom = nullptr;
eventCallback inviteRoom = NULL; eventCallback inviteRoom = nullptr;
roomInfoCallback roomInfo = NULL; roomInfoCallback roomInfo = nullptr;
roomLimitedCallback roomLimited = NULL; roomLimitedCallback roomLimited = nullptr;
} callbacks; } callbacks;
void processSync(json_t* sync); void processSync(json_t* sync);
void registerFilter(); void registerFilter();
json_t* doSync(std::string token, std::string filter, u32 timeout, CURLcode* res); json_t* doSync(const std::string& syncToken, const std::string& filter, u32 timeout, CURLcode* res);
json_t* doRequest(const char* method, std::string path, json_t* body = nullptr, u32 timeout = 5, CURLcode* retRes = nullptr, bool doRequest = true); json_t* doRequest(const char* method,
json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes); const std::string& path, json_t* body = nullptr, u32 timeout = 5, CURLcode* retRes = nullptr, bool needsRequest = true);
json_t* doRequestCurl(const char* method, const std::string& url, json_t* body, u32 timeout, CURLcode* retRes);
static void print_json(json_t *json); static void print_json(json_t *json);
public: public:
Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL); Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL);
std::string getToken() const; std::string getToken() const;
bool login(std::string username, std::string password, std::string device_id=""); bool login(const std::string& username, const std::string& password, const std::string& device_id="");
void logout(); void logout();
std::string getUserId(); std::string getUserId();
std::string getDeviceId(); std::string getDeviceId();
@ -78,18 +79,18 @@ public:
std::vector<std::string> getJoinedRooms(); std::vector<std::string> getJoinedRooms();
RoomInfo getRoomInfo(std::string roomId); RoomInfo getRoomInfo(std::string roomId);
ExtraRoomInfo getExtraRoomInfo(std::string roomId); ExtraRoomInfo getExtraRoomInfo(std::string roomId);
MemberInfo getMemberInfo(std::string userId, std::string roomId = ""); MemberInfo getMemberInfo(const std::string& userId, const std::string& roomId = "");
std::string getRoomName(std::string roomId); std::string getRoomName(const std::string& roomId);
std::string getRoomTopic(std::string roomId); std::string getRoomTopic(const std::string& roomId);
std::string getRoomAvatar(std::string roomId); std::string getRoomAvatar(const std::string& roomId);
std::string getCanonicalAlias(std::string roomId); std::string getCanonicalAlias(const std::string& roomId);
void sendReadReceipt(std::string roomId, std::string eventId); void sendReadReceipt(const std::string& roomId, const std::string& eventId);
void setTyping(std::string roomId, bool typing, u32 timeout = 30000); void setTyping(std::string roomId, bool typing, u32 timeout = 30000);
std::string sendEmote(std::string roomId, std::string text); std::string sendEmote(const std::string& roomId, const std::string& text);
std::string sendNotice(std::string roomId, std::string text); std::string sendNotice(const std::string& roomId, const std::string& text);
std::string sendText(std::string roomId, std::string text); std::string sendText(const std::string& roomId, const std::string& text);
std::string sendMessage(std::string roomId, json_t* content); std::string sendMessage(const std::string& roomId, json_t* content);
std::string sendEvent(std::string roomId, std::string eventType, json_t* content); std::string sendEvent(const std::string& roomId, const std::string& eventType, json_t* content);
// Format Json in shape // Format Json in shape
// { // {
@ -108,9 +109,9 @@ public:
// } // }
// } // }
void sendEventToDevice(const std::string& eventType, json_t* content); void sendEventToDevice(const std::string& eventType, json_t* content);
json_t* getStateEvent(std::string roomId, std::string type, std::string stateKey); json_t* getStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey);
std::string sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content); std::string sendStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey, json_t* content);
std::string redactEvent(std::string roomId, std::string eventId, std::string reason = ""); std::string redactEvent(const std::string& roomId, const std::string& eventId, const std::string& reason = "");
void startSyncLoop(); void startSyncLoop();
void stopSyncLoop(); void stopSyncLoop();
void setEventCallback(eventCallback cb); void setEventCallback(eventCallback cb);

View file

@ -4,7 +4,7 @@
#include <malloc.h> #include <malloc.h>
#include <matrixclient.h> #include <matrixclient.h>
#include <olm/olm.h> #include <olm/olm.h>
#include <stdio.h> #include <cstdio>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
@ -12,6 +12,7 @@
#include <map> #include <map>
#include <olm/account.hh> #include <olm/account.hh>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "memorystore.h" #include "memorystore.h"
#include "olm/base64.hh" #include "olm/base64.hh"
@ -30,7 +31,7 @@
#endif #endif
#if DEBUG #if DEBUG
PrintConsole* topScreenDebugConsole = NULL; PrintConsole* topScreenDebugConsole = nullptr;
#endif #endif
#if DEBUG #if DEBUG
@ -43,13 +44,13 @@ namespace Matrix {
#define POST_BUFFERSIZE 0x100000 #define POST_BUFFERSIZE 0x100000
static u32 *SOC_buffer = NULL; static u32 *SOC_buffer = nullptr;
bool HTTPC_inited = false; bool HTTPC_inited = false;
bool haveHttpcSupport = false; bool haveHttpcSupport = false;
Client::Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore) { Client::Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore) {
hsUrl = homeserverUrl; hsUrl = std::move(homeserverUrl);
token = matrixToken; token = std::move(matrixToken);
if (!clientStore) { if (!clientStore) {
clientStore = new MemoryStore; clientStore = new MemoryStore;
} }
@ -66,7 +67,7 @@ std::string Client::getToken() const {
return token; return token;
} }
bool Client::login(std::string username, std::string password, std::string device_id) { bool Client::login(const std::string& username, const std::string& password, const std::string& device_id) {
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "type", json_string("m.login.password")); json_object_set_new(request, "type", json_string("m.login.password"));
json_t* identifier = json_object(); json_t* identifier = json_object();
@ -164,7 +165,7 @@ std::vector<std::string> Client::getJoinedRooms() {
json_array_foreach(roomsArr, index, value) { json_array_foreach(roomsArr, index, value) {
const char* val = json_string_value(value); const char* val = json_string_value(value);
if (val) { if (val) {
rooms.push_back(val); rooms.emplace_back(val);
} }
} }
json_decref(ret); json_decref(ret);
@ -217,11 +218,11 @@ ExtraRoomInfo Client::getExtraRoomInfo(std::string roomId) {
return info; return info;
} }
MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) { MemberInfo Client::getMemberInfo(const std::string& userId, const std::string& roomId) {
std::string displayname = ""; std::string displayname;
std::string avatarUrl = ""; std::string avatarUrl;
if (roomId != "") { if (!roomId.empty()) {
// first try fetching fro the room // first try fetching from the room
json_t* ret = getStateEvent(roomId, "m.room.member", userId); json_t* ret = getStateEvent(roomId, "m.room.member", userId);
if (ret) { if (ret) {
char* valCStr; char* valCStr;
@ -236,7 +237,7 @@ MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) {
json_decref(ret); json_decref(ret);
} }
} }
if (displayname == "") { if (displayname.empty()) {
// then attempt the account // then attempt the account
std::string path = "/_matrix/client/v3/profile/" + urlencode(userId); std::string path = "/_matrix/client/v3/profile/" + urlencode(userId);
json_t* ret = doRequest("GET", path); json_t* ret = doRequest("GET", path);
@ -260,7 +261,7 @@ MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) {
return info; return info;
} }
std::string Client::getRoomName(std::string roomId) { std::string Client::getRoomName(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.name", ""); json_t* ret = getStateEvent(roomId, "m.room.name", "");
const char* nameCStr = json_object_get_string_value(ret, "name"); const char* nameCStr = json_object_get_string_value(ret, "name");
if (!nameCStr) { if (!nameCStr) {
@ -272,7 +273,7 @@ std::string Client::getRoomName(std::string roomId) {
return nameStr; return nameStr;
} }
std::string Client::getRoomTopic(std::string roomId) { std::string Client::getRoomTopic(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.topic", ""); json_t* ret = getStateEvent(roomId, "m.room.topic", "");
const char* topicCStr = json_object_get_string_value(ret, "topic"); const char* topicCStr = json_object_get_string_value(ret, "topic");
if (!topicCStr) { if (!topicCStr) {
@ -284,7 +285,7 @@ std::string Client::getRoomTopic(std::string roomId) {
return topicStr; return topicStr;
} }
std::string Client::getRoomAvatar(std::string roomId) { std::string Client::getRoomAvatar(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.avatar", ""); json_t* ret = getStateEvent(roomId, "m.room.avatar", "");
const char* urlCStr = json_object_get_string_value(ret, "url"); const char* urlCStr = json_object_get_string_value(ret, "url");
if (!urlCStr) { if (!urlCStr) {
@ -296,7 +297,7 @@ std::string Client::getRoomAvatar(std::string roomId) {
return urlStr; return urlStr;
} }
std::string Client::getCanonicalAlias(std::string roomId) { std::string Client::getCanonicalAlias(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.canonical_alias", ""); json_t* ret = getStateEvent(roomId, "m.room.canonical_alias", "");
const char* aliasCStr = json_object_get_string_value(ret, "alias"); const char* aliasCStr = json_object_get_string_value(ret, "alias");
if (!aliasCStr) { if (!aliasCStr) {
@ -308,9 +309,9 @@ std::string Client::getCanonicalAlias(std::string roomId) {
return aliasStr; return aliasStr;
} }
void Client::sendReadReceipt(std::string roomId, std::string eventId) { void Client::sendReadReceipt(const std::string& roomId, const std::string& eventId) {
roomId = resolveRoom(roomId); std::string roomDecoded = resolveRoom(roomId);
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/receipt/m.read/" + urlencode(eventId); std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomDecoded) + "/receipt/m.read/" + urlencode(eventId);
json_t* ret = doRequest("POST", path); json_t* ret = doRequest("POST", path);
if (ret) { if (ret) {
json_decref(ret); json_decref(ret);
@ -319,14 +320,8 @@ void Client::sendReadReceipt(std::string roomId, std::string eventId) {
void Client::uploadKeys(json_t* body) { void Client::uploadKeys(json_t* body) {
std::string path = "/_matrix/client/v3/keys/upload"; std::string path = "/_matrix/client/v3/keys/upload";
/*char* jj = json_dumps(body, 0);
printf_top("%s\n", jj);
free(jj);*/
json_t* ret = doRequest("POST", path, body); json_t* ret = doRequest("POST", path, body);
if (ret) { if (ret) {
/*char* j = json_dumps(ret, 0);
printf_top("%s\n",j);
free(j);*/
json_decref(ret); json_decref(ret);
} }
} }
@ -343,7 +338,7 @@ void Client::setTyping(std::string roomId, bool typing, u32 timeout) {
json_decref(ret); json_decref(ret);
} }
std::string Client::sendEmote(std::string roomId, std::string text) { std::string Client::sendEmote(const std::string& roomId, const std::string& text) {
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "msgtype", json_string("m.emote")); json_object_set_new(request, "msgtype", json_string("m.emote"));
json_object_set_new(request, "body", json_string(text.c_str())); json_object_set_new(request, "body", json_string(text.c_str()));
@ -352,7 +347,7 @@ std::string Client::sendEmote(std::string roomId, std::string text) {
return eventId; return eventId;
} }
std::string Client::sendNotice(std::string roomId, std::string text) { std::string Client::sendNotice(const std::string& roomId, const std::string& text) {
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "msgtype", json_string("m.notice")); json_object_set_new(request, "msgtype", json_string("m.notice"));
json_object_set_new(request, "body", json_string(text.c_str())); json_object_set_new(request, "body", json_string(text.c_str()));
@ -361,7 +356,7 @@ std::string Client::sendNotice(std::string roomId, std::string text) {
return eventId; return eventId;
} }
std::string Client::sendText(std::string roomId, std::string text) { std::string Client::sendText(const std::string& roomId, const std::string& text) {
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "msgtype", json_string("m.text")); json_object_set_new(request, "msgtype", json_string("m.text"));
json_object_set_new(request, "body", json_string(text.c_str())); json_object_set_new(request, "body", json_string(text.c_str()));
@ -370,7 +365,7 @@ std::string Client::sendText(std::string roomId, std::string text) {
return eventId; return eventId;
} }
std::string Client::sendMessage(std::string roomId, json_t* content) { std::string Client::sendMessage(const std::string& roomId, json_t* content) {
return sendEvent(roomId, "m.room.message", content); return sendEvent(roomId, "m.room.message", content);
} }
@ -385,10 +380,10 @@ void Client::sendEventToDevice(const std::string& eventType, json_t* content){
if (ret) json_decref(ret); if (ret) json_decref(ret);
} }
std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) { std::string Client::sendEvent(const std::string& roomId, const std::string& eventType, json_t* content) {
roomId = resolveRoom(roomId); std::string resolvedId = resolveRoom(roomId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId++); std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++);
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid); std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid);
json_t* ret = doRequest("PUT", path, content, 5, nullptr, false); json_t* ret = doRequest("PUT", path, content, 5, nullptr, false);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
if (!eventIdCStr) { if (!eventIdCStr) {
@ -400,15 +395,15 @@ std::string Client::sendEvent(std::string roomId, std::string eventType, json_t*
return eventIdStr; return eventIdStr;
} }
json_t* Client::getStateEvent(std::string roomId, std::string type, std::string stateKey) { json_t* Client::getStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey) {
roomId = resolveRoom(roomId); std::string resolvedId = resolveRoom(roomId);
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey);
return doRequest("GET", path); return doRequest("GET", path);
} }
std::string Client::sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content) { std::string Client::sendStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey, json_t* content) {
roomId = resolveRoom(roomId); std::string resolvedId = resolveRoom(roomId);
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey);
json_t* ret = doRequest("PUT", path, content); json_t* ret = doRequest("PUT", path, content);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
if (!eventIdCStr) { if (!eventIdCStr) {
@ -420,14 +415,14 @@ std::string Client::sendStateEvent(std::string roomId, std::string type, std::st
return eventIdStr; return eventIdStr;
} }
std::string Client::redactEvent(std::string roomId, std::string eventId, std::string reason) { std::string Client::redactEvent(const std::string& roomId, const std::string& eventId, const std::string& reason) {
roomId = resolveRoom(roomId); std::string resolvedId = resolveRoom(roomId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId++); std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++);
json_t* content = json_object(); json_t* content = json_object();
if (!reason.empty()) { if (!reason.empty()) {
json_object_set_new(content, "reason", json_string(reason.c_str())); json_object_set_new(content, "reason", json_string(reason.c_str()));
} }
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/redact/" + urlencode(eventId) + "/" + txid; std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/redact/" + urlencode(eventId) + "/" + txid;
json_t* ret = doRequest("PUT", path, content, 5, nullptr, false); json_t* ret = doRequest("PUT", path, content, 5, nullptr, false);
json_decref(content); json_decref(content);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
@ -511,7 +506,7 @@ void Client::processSync(json_t* sync) {
if (!events) { if (!events) {
continue; continue;
} }
json_t* leaveEvent = NULL; json_t* leaveEvent = nullptr;
json_array_foreach(events, index, event) { json_array_foreach(events, index, event) {
// check if the event type is m.room.member // check if the event type is m.room.member
char* val; char* val;
@ -553,7 +548,7 @@ void Client::processSync(json_t* sync) {
if (!events) { if (!events) {
continue; continue;
} }
json_t* inviteEvent = NULL; json_t* inviteEvent = nullptr;
json_array_foreach(events, index, event) { json_array_foreach(events, index, event) {
// check if the event type is m.room.member // check if the event type is m.room.member
char* val; char* val;
@ -732,14 +727,14 @@ void Client::syncLoop() {
if (stopSyncing) { if (stopSyncing) {
return; return;
} }
std::string token = store->getSyncToken(); std::string syncToken = store->getSyncToken();
std::string filterId = store->getFilterId(); std::string filterId = store->getFilterId();
if (filterId == "") { if (filterId.empty()) {
registerFilter(); registerFilter();
filterId = store->getFilterId(); filterId = store->getFilterId();
} }
CURLcode res; CURLcode res;
json_t* ret = doSync(token, filterId, timeout, &res); json_t* ret = doSync(syncToken, filterId, timeout, &res);
if (ret) { if (ret) {
timeout = 60; timeout = 60;
// set the token for the next batch // set the token for the next batch
@ -761,14 +756,14 @@ void Client::syncLoop() {
} }
} }
json_t* Client::doSync(std::string token, std::string filter, u32 timeout, CURLcode* res) { json_t* Client::doSync(const std::string& syncToken, const std::string& filter, u32 timeout, CURLcode* res) {
// printf_top("Doing sync with token %s\n", token.c_str()); // printf_top("Doing sync with token %s\n", token.c_str());
std::string query = "?full_state=false&timeout=" + std::to_string(SYNC_TIMEOUT) + "&filter=" + urlencode(filter); std::string query = "?full_state=false&timeout=" + std::to_string(SYNC_TIMEOUT) + "&filter=" + urlencode(filter);
if (token != "") { if (!syncToken.empty()) {
query += "&since=" + token; query += "&since=" + syncToken;
} }
return doRequest("GET", "/_matrix/client/v3/sync" + query, NULL, timeout, res); return doRequest("GET", "/_matrix/client/v3/sync" + query, nullptr, timeout, res);
} }
size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *userp) { size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *userp) {
@ -780,7 +775,7 @@ size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *u
bool doingCurlRequest = false; bool doingCurlRequest = false;
bool doingHttpcRequest = false; bool doingHttpcRequest = false;
json_t* Client::doRequest(const char* method, std::string path, json_t* body, u32 timeout, CURLcode* retRes, bool needsRequest) { json_t* Client::doRequest(const char* method, const std::string& path, json_t* body, u32 timeout, CURLcode* retRes, bool needsRequest) {
std::string url = hsUrl + path; std::string url = hsUrl + path;
if(needsRequest) { if(needsRequest) {
requestId++; requestId++;
@ -792,8 +787,7 @@ CURLM* curl_multi_handle;
std::map<CURLM*, CURLcode> curl_handles_done; std::map<CURLM*, CURLcode> curl_handles_done;
Thread curl_multi_loop_thread; Thread curl_multi_loop_thread;
[[noreturn]] void curl_multi_loop(void* p) {
void curl_multi_loop(void* p) {
int openHandles = 0; int openHandles = 0;
while(true) { while(true) {
CURLMcode mc = curl_multi_perform(curl_multi_handle, &openHandles); CURLMcode mc = curl_multi_perform(curl_multi_handle, &openHandles);
@ -814,34 +808,34 @@ void curl_multi_loop(void* p) {
} }
} }
json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes) { json_t* Client::doRequestCurl(const char* method, const std::string& url, json_t* body, u32 timeout, CURLcode* retRes) {
// printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str()); // printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str());
if (!SOC_buffer) { if (!SOC_buffer) {
SOC_buffer = (u32*)memalign(0x1000, POST_BUFFERSIZE); SOC_buffer = (u32*)memalign(0x1000, POST_BUFFERSIZE);
if (!SOC_buffer) { if (!SOC_buffer) {
return NULL; return nullptr;
} }
if (socInit(SOC_buffer, POST_BUFFERSIZE) != 0) { if (socInit(SOC_buffer, POST_BUFFERSIZE) != 0) {
return NULL; return nullptr;
} }
curl_multi_handle = curl_multi_init(); curl_multi_handle = curl_multi_init();
s32 prio = 0; s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE); svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
curl_multi_loop_thread = threadCreate(curl_multi_loop, NULL, 8*1024, prio-1, -2, true); curl_multi_loop_thread = threadCreate(curl_multi_loop, nullptr, 8*1024, prio-1, -2, true);
} }
CURL* curl = curl_easy_init(); CURL* curl = curl_easy_init();
if (!curl) { if (!curl) {
printf_top("curl init failed\n"); printf_top("curl init failed\n");
return NULL; return nullptr;
} }
std::string readBuffer; std::string readBuffer;
struct curl_slist* headers = NULL; struct curl_slist* headers = nullptr;
if (token != "") { if (!token.empty()) {
headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str()); headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str());
} }
char* bodyStr = NULL; char* bodyStr = nullptr;
if (body) { if (body) {
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH); bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH);
@ -878,7 +872,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body,
if (retRes) *retRes = res; if (retRes) *retRes = res;
if (res != CURLE_OK) { if (res != CURLE_OK) {
printf_top("curl res not ok %d\n", res); printf_top("curl res not ok %d\n", res);
return NULL; return nullptr;
} }
// printf_top("++++\n%s\n", readBuffer.c_str()); // printf_top("++++\n%s\n", readBuffer.c_str());
@ -887,7 +881,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body,
json_t* content = json_loads(readBuffer.c_str(), 0, &error); json_t* content = json_loads(readBuffer.c_str(), 0, &error);
if (!content) { if (!content) {
printf_top("Failed to parse json\n"); printf_top("Failed to parse json\n");
return NULL; return nullptr;
} }
return content; return content;
} }