#include #include #include "PetManager.h" #include "NetClient.h" #include "Helpers.h" #include "Error.h" #include "RequestInfo.h" using namespace std; using namespace Tizen::ArtikCloud; PetManager::PetManager() { } PetManager::~PetManager() { } static gboolean __PetManagerresponseHandler(gpointer data) { RequestInfo *request = static_cast(data); g_thread_join(request->thread); // invoke the callback function bool retval = request->processor(*(request->p_chunk), *(request->code), request->errormsg, request->userData, request->handler); delete request; return FALSE; } static gpointer __PetManagerthreadFunc(gpointer data) { RequestInfo *request = static_cast(data); // handle the request NetClient::easycurl(request->host, request->path, request->method, request->queryParams, request->mBody, request->headerList, request->p_chunk, request->code, request->errormsg); request->thread = g_thread_self(); g_idle_add(__PetManagerresponseHandler, static_cast(request)); return NULL; } static bool addPetProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; if (code >= 200 && code < 300) { Error error(code, string("No Error")); handler(error, userData); return true; } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(error, userData); return false; } } static bool addPetHelper(char * accessToken, Pet body, void(* handler)(Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/json"); headerList = curl_slist_append(headerList, "Content-Type: application/xml"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; if (isprimitive("Pet")) { node = converttoJson(&body, "Pet", ""); } char *jsonStr = body.toJson(); node = json_from_string(jsonStr, NULL); g_free(static_cast(jsonStr)); char *jsonStr1 = json_to_string(node, false); mBody.append(jsonStr1); g_free(static_cast(jsonStr1)); string url("/pet"); int pos; //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("POST"); if(strcmp("PUT", "POST") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = addPetProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), addPetProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::addPetAsync(char * accessToken, Pet body, void(* handler)(Error, void* ) , void* userData) { return addPetHelper(accessToken, body, handler, userData, true); } bool PetManager::addPetSync(char * accessToken, Pet body, void(* handler)(Error, void* ) , void* userData) { return addPetHelper(accessToken, body, handler, userData, false); } static bool deletePetProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; if (code >= 200 && code < 300) { Error error(code, string("No Error")); handler(error, userData); return true; } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(error, userData); return false; } } static bool deletePetHelper(char * accessToken, long long petId, std::string apiKey, void(* handler)(Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; { string headerString("api_key: "); headerString.append(stringify(&apiKey, "std::string")); headerList = curl_slist_append(headerList, headerString.c_str()); } string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/json"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/pet/{petId}"); int pos; string s_petId("{"); s_petId.append("petId"); s_petId.append("}"); pos = url.find(s_petId); url.erase(pos, s_petId.length()); url.insert(pos, stringify(&petId, "long long")); //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("DELETE"); if(strcmp("PUT", "DELETE") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = deletePetProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), deletePetProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::deletePetAsync(char * accessToken, long long petId, std::string apiKey, void(* handler)(Error, void* ) , void* userData) { return deletePetHelper(accessToken, petId, apiKey, handler, userData, true); } bool PetManager::deletePetSync(char * accessToken, long long petId, std::string apiKey, void(* handler)(Error, void* ) , void* userData) { return deletePetHelper(accessToken, petId, apiKey, handler, userData, false); } static bool findPetsByStatusProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(std::list, Error, void* ) = reinterpret_cast, Error, void* )> (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; std::list out; if (code >= 200 && code < 300) { Error error(code, string("No Error")); pJson = json_from_string(data, NULL); JsonArray * jsonarray = json_node_get_array (pJson); guint length = json_array_get_length (jsonarray); for(guint i = 0; i < length; i++){ JsonNode* myJson = json_array_get_element (jsonarray, i); char * singlenodestr = json_to_string(myJson, false); Pet singlemodel; singlemodel.fromJson(singlenodestr); out.push_front(singlemodel); g_free(static_cast(singlenodestr)); json_node_free(myJson); } json_array_unref (jsonarray); json_node_free(pJson); } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(out, error, userData); return false; } } static bool findPetsByStatusHelper(char * accessToken, std::list status, void(* handler)(std::list, Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/json"); map queryParams; string itemAtq; for (std::list ::iterator queryIter = status.begin(); queryIter != status.end(); ++queryIter) { string itemAt = stringify(&(*queryIter), "std::string"); queryParams.insert(pair("status", itemAt)); } string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/pet/findByStatus"); int pos; //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("GET"); if(strcmp("PUT", "GET") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = findPetsByStatusProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), findPetsByStatusProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::findPetsByStatusAsync(char * accessToken, std::list status, void(* handler)(std::list, Error, void* ) , void* userData) { return findPetsByStatusHelper(accessToken, status, handler, userData, true); } bool PetManager::findPetsByStatusSync(char * accessToken, std::list status, void(* handler)(std::list, Error, void* ) , void* userData) { return findPetsByStatusHelper(accessToken, status, handler, userData, false); } static bool findPetsByTagsProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(std::list, Error, void* ) = reinterpret_cast, Error, void* )> (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; std::list out; if (code >= 200 && code < 300) { Error error(code, string("No Error")); pJson = json_from_string(data, NULL); JsonArray * jsonarray = json_node_get_array (pJson); guint length = json_array_get_length (jsonarray); for(guint i = 0; i < length; i++){ JsonNode* myJson = json_array_get_element (jsonarray, i); char * singlenodestr = json_to_string(myJson, false); Pet singlemodel; singlemodel.fromJson(singlenodestr); out.push_front(singlemodel); g_free(static_cast(singlenodestr)); json_node_free(myJson); } json_array_unref (jsonarray); json_node_free(pJson); } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(out, error, userData); return false; } } static bool findPetsByTagsHelper(char * accessToken, std::list tags, void(* handler)(std::list, Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/json"); map queryParams; string itemAtq; for (std::list ::iterator queryIter = tags.begin(); queryIter != tags.end(); ++queryIter) { string itemAt = stringify(&(*queryIter), "std::string"); queryParams.insert(pair("tags", itemAt)); } string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/pet/findByTags"); int pos; //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("GET"); if(strcmp("PUT", "GET") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = findPetsByTagsProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), findPetsByTagsProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::findPetsByTagsAsync(char * accessToken, std::list tags, void(* handler)(std::list, Error, void* ) , void* userData) { return findPetsByTagsHelper(accessToken, tags, handler, userData, true); } bool PetManager::findPetsByTagsSync(char * accessToken, std::list tags, void(* handler)(std::list, Error, void* ) , void* userData) { return findPetsByTagsHelper(accessToken, tags, handler, userData, false); } static bool getPetByIdProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Pet, Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; Pet out; if (code >= 200 && code < 300) { Error error(code, string("No Error")); if (isprimitive("Pet")) { pJson = json_from_string(data, NULL); jsonToValue(&out, pJson, "Pet", "Pet"); json_node_free(pJson); if ("Pet" == "std::string") { string* val = (std::string*)(&out); if (val->empty() && p_chunk.size>4) { *val = string(p_chunk.memory, p_chunk.size); } } } else { out.fromJson(data); char *jsonStr = out.toJson(); printf("\n%s\n", jsonStr); g_free(static_cast(jsonStr)); } handler(out, error, userData); return true; //TODO: handle case where json parsing has an error } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(out, error, userData); return false; } } static bool getPetByIdHelper(char * accessToken, long long petId, void(* handler)(Pet, Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/json"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/pet/{petId}"); int pos; string s_petId("{"); s_petId.append("petId"); s_petId.append("}"); pos = url.find(s_petId); url.erase(pos, s_petId.length()); url.insert(pos, stringify(&petId, "long long")); //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("GET"); if(strcmp("PUT", "GET") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = getPetByIdProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), getPetByIdProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::getPetByIdAsync(char * accessToken, long long petId, void(* handler)(Pet, Error, void* ) , void* userData) { return getPetByIdHelper(accessToken, petId, handler, userData, true); } bool PetManager::getPetByIdSync(char * accessToken, long long petId, void(* handler)(Pet, Error, void* ) , void* userData) { return getPetByIdHelper(accessToken, petId, handler, userData, false); } static bool updatePetProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; if (code >= 200 && code < 300) { Error error(code, string("No Error")); handler(error, userData); return true; } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(error, userData); return false; } } static bool updatePetHelper(char * accessToken, Pet body, void(* handler)(Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/json"); headerList = curl_slist_append(headerList, "Content-Type: application/xml"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; if (isprimitive("Pet")) { node = converttoJson(&body, "Pet", ""); } char *jsonStr = body.toJson(); node = json_from_string(jsonStr, NULL); g_free(static_cast(jsonStr)); char *jsonStr1 = json_to_string(node, false); mBody.append(jsonStr1); g_free(static_cast(jsonStr1)); string url("/pet"); int pos; //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("PUT"); if(strcmp("PUT", "PUT") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = updatePetProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), updatePetProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::updatePetAsync(char * accessToken, Pet body, void(* handler)(Error, void* ) , void* userData) { return updatePetHelper(accessToken, body, handler, userData, true); } bool PetManager::updatePetSync(char * accessToken, Pet body, void(* handler)(Error, void* ) , void* userData) { return updatePetHelper(accessToken, body, handler, userData, false); } static bool updatePetWithFormProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; if (code >= 200 && code < 300) { Error error(code, string("No Error")); handler(error, userData); return true; } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(error, userData); return false; } } static bool updatePetWithFormHelper(char * accessToken, long long petId, std::string name, std::string status, void(* handler)(Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: application/x-www-form-urlencoded"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/pet/{petId}"); int pos; string s_petId("{"); s_petId.append("petId"); s_petId.append("}"); pos = url.find(s_petId); url.erase(pos, s_petId.length()); url.insert(pos, stringify(&petId, "long long")); //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("POST"); if(strcmp("PUT", "POST") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = updatePetWithFormProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), updatePetWithFormProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::updatePetWithFormAsync(char * accessToken, long long petId, std::string name, std::string status, void(* handler)(Error, void* ) , void* userData) { return updatePetWithFormHelper(accessToken, petId, name, status, handler, userData, true); } bool PetManager::updatePetWithFormSync(char * accessToken, long long petId, std::string name, std::string status, void(* handler)(Error, void* ) , void* userData) { return updatePetWithFormHelper(accessToken, petId, name, status, handler, userData, false); } static bool uploadFileProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(ApiResponse, Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; ApiResponse out; if (code >= 200 && code < 300) { Error error(code, string("No Error")); if (isprimitive("ApiResponse")) { pJson = json_from_string(data, NULL); jsonToValue(&out, pJson, "ApiResponse", "ApiResponse"); json_node_free(pJson); if ("ApiResponse" == "std::string") { string* val = (std::string*)(&out); if (val->empty() && p_chunk.size>4) { *val = string(p_chunk.memory, p_chunk.size); } } } else { out.fromJson(data); char *jsonStr = out.toJson(); printf("\n%s\n", jsonStr); g_free(static_cast(jsonStr)); } handler(out, error, userData); return true; //TODO: handle case where json parsing has an error } else { Error error; if (errormsg != NULL) { error = Error(code, string(errormsg)); } else if (p_chunk.memory != NULL) { error = Error(code, string(p_chunk.memory)); } else { error = Error(code, string("Unknown Error")); } handler(out, error, userData); return false; } } static bool uploadFileHelper(char * accessToken, long long petId, std::string additionalMetadata, std::string file, void(* handler)(ApiResponse, Error, void* ) , void* userData, bool isAsync) { //TODO: maybe delete headerList after its used to free up space? struct curl_slist *headerList = NULL; string accessHeader = "Authorization: Bearer "; accessHeader.append(accessToken); headerList = curl_slist_append(headerList, accessHeader.c_str()); headerList = curl_slist_append(headerList, "Content-Type: multipart/form-data"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/pet/{petId}/uploadImage"); int pos; string s_petId("{"); s_petId.append("petId"); s_petId.append("}"); pos = url.find(s_petId); url.erase(pos, s_petId.length()); url.insert(pos, stringify(&petId, "long long")); //TODO: free memory of errormsg, memorystruct MemoryStruct_s* p_chunk = new MemoryStruct_s(); long code; char* errormsg = NULL; string myhttpmethod("POST"); if(strcmp("PUT", "POST") == 0){ if(strcmp("", mBody.c_str()) == 0){ mBody.append("{}"); } } if(!isAsync){ NetClient::easycurl(PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = uploadFileProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast(handler)); curl_slist_free_all(headerList); if (p_chunk) { if(p_chunk->memory) { free(p_chunk->memory); } delete (p_chunk); } if (errormsg) { free(errormsg); } return retval; } else{ GThread *thread = NULL; RequestInfo *requestInfo = NULL; requestInfo = new(nothrow) RequestInfo (PetManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), uploadFileProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __PetManagerthreadFunc, static_cast(requestInfo)); return true; } } bool PetManager::uploadFileAsync(char * accessToken, long long petId, std::string additionalMetadata, std::string file, void(* handler)(ApiResponse, Error, void* ) , void* userData) { return uploadFileHelper(accessToken, petId, additionalMetadata, file, handler, userData, true); } bool PetManager::uploadFileSync(char * accessToken, long long petId, std::string additionalMetadata, std::string file, void(* handler)(ApiResponse, Error, void* ) , void* userData) { return uploadFileHelper(accessToken, petId, additionalMetadata, file, handler, userData, false); }