#include #include #include "StoreManager.h" #include "NetClient.h" #include "Helpers.h" #include "Error.h" #include "RequestInfo.h" using namespace std; using namespace Tizen::ArtikCloud; StoreManager::StoreManager() { } StoreManager::~StoreManager() { } static gboolean __StoreManagerresponseHandler(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 __StoreManagerthreadFunc(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(__StoreManagerresponseHandler, static_cast(request)); return NULL; } static bool deleteOrderProcessor(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 deleteOrderHelper(char * accessToken, std::string orderId, 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"); map queryParams; string itemAtq; string mBody = ""; JsonNode* node; JsonArray* json_array; string url("/store/order/{orderId}"); int pos; string s_orderId("{"); s_orderId.append("orderId"); s_orderId.append("}"); pos = url.find(s_orderId); url.erase(pos, s_orderId.length()); url.insert(pos, stringify(&orderId, "std::string")); //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(StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = deleteOrderProcessor(*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 (StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), deleteOrderProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __StoreManagerthreadFunc, static_cast(requestInfo)); return true; } } bool StoreManager::deleteOrderAsync(char * accessToken, std::string orderId, void(* handler)(Error, void* ) , void* userData) { return deleteOrderHelper(accessToken, orderId, handler, userData, true); } bool StoreManager::deleteOrderSync(char * accessToken, std::string orderId, void(* handler)(Error, void* ) , void* userData) { return deleteOrderHelper(accessToken, orderId, handler, userData, false); } static bool getInventoryProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(std::map, Error, void* ) = reinterpret_cast, Error, void* )> (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; std::map out; if (code >= 200 && code < 300) { Error error(code, string("No 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 getInventoryHelper(char * accessToken, void(* handler)(std::map, 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("/store/inventory"); 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(StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = getInventoryProcessor(*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 (StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), getInventoryProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __StoreManagerthreadFunc, static_cast(requestInfo)); return true; } } bool StoreManager::getInventoryAsync(char * accessToken, void(* handler)(std::map, Error, void* ) , void* userData) { return getInventoryHelper(accessToken, handler, userData, true); } bool StoreManager::getInventorySync(char * accessToken, void(* handler)(std::map, Error, void* ) , void* userData) { return getInventoryHelper(accessToken, handler, userData, false); } static bool getOrderByIdProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Order, Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; Order out; if (code >= 200 && code < 300) { Error error(code, string("No Error")); if (isprimitive("Order")) { pJson = json_from_string(data, NULL); jsonToValue(&out, pJson, "Order", "Order"); json_node_free(pJson); if ("Order" == "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 getOrderByIdHelper(char * accessToken, long long orderId, void(* handler)(Order, 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("/store/order/{orderId}"); int pos; string s_orderId("{"); s_orderId.append("orderId"); s_orderId.append("}"); pos = url.find(s_orderId); url.erase(pos, s_orderId.length()); url.insert(pos, stringify(&orderId, "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(StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = getOrderByIdProcessor(*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 (StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), getOrderByIdProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __StoreManagerthreadFunc, static_cast(requestInfo)); return true; } } bool StoreManager::getOrderByIdAsync(char * accessToken, long long orderId, void(* handler)(Order, Error, void* ) , void* userData) { return getOrderByIdHelper(accessToken, orderId, handler, userData, true); } bool StoreManager::getOrderByIdSync(char * accessToken, long long orderId, void(* handler)(Order, Error, void* ) , void* userData) { return getOrderByIdHelper(accessToken, orderId, handler, userData, false); } static bool placeOrderProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData, void(* voidHandler)()) { void(* handler)(Order, Error, void* ) = reinterpret_cast (voidHandler); JsonNode* pJson; char * data = p_chunk.memory; Order out; if (code >= 200 && code < 300) { Error error(code, string("No Error")); if (isprimitive("Order")) { pJson = json_from_string(data, NULL); jsonToValue(&out, pJson, "Order", "Order"); json_node_free(pJson); if ("Order" == "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 placeOrderHelper(char * accessToken, Order order, void(* handler)(Order, 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; if (isprimitive("Order")) { node = converttoJson(&order, "Order", ""); } char *jsonStr = order.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("/store/order"); 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(StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg); bool retval = placeOrderProcessor(*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 (StoreManager::getBasePath(), url, myhttpmethod, queryParams, mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast(handler), placeOrderProcessor);; if(requestInfo == NULL) return false; thread = g_thread_new(NULL, __StoreManagerthreadFunc, static_cast(requestInfo)); return true; } } bool StoreManager::placeOrderAsync(char * accessToken, Order order, void(* handler)(Order, Error, void* ) , void* userData) { return placeOrderHelper(accessToken, order, handler, userData, true); } bool StoreManager::placeOrderSync(char * accessToken, Order order, void(* handler)(Order, Error, void* ) , void* userData) { return placeOrderHelper(accessToken, order, handler, userData, false); }