text: #include <glib-object.h>
  #include <json-glib/json-glib.h>
  
  #include "BerryFlavorManager.h"
  #include "NetClient.h"
  #include "Helpers.h"
  #include "Error.h"
  #include "RequestInfo.h"
  
  using namespace std;
  using namespace Tizen::ArtikCloud;
  
  
  BerryFlavorManager::BerryFlavorManager()
  {
  
  }
  
  BerryFlavorManager::~BerryFlavorManager()
  {
  
  }
  
  static gboolean __BerryFlavorManagerresponseHandler(gpointer data)
  {
  	RequestInfo *request = static_cast<RequestInfo*>(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 __BerryFlavorManagerthreadFunc(gpointer data)
  {
  	RequestInfo *request = static_cast<RequestInfo*>(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(__BerryFlavorManagerresponseHandler, static_cast<gpointer>(request));
  
  	return NULL;
  }
  
  
  static bool berryFlavorListProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
  	void(* voidHandler)())
  {
  	void(* handler)(std::string, Error, void* )
  	= reinterpret_cast<void(*)(std::string, Error, void* )> (voidHandler);
  	
  	JsonNode* pJson;
  	char * data = p_chunk.memory;
  
  	
  	std::string out;
  
  	if (code >= 200 && code < 300) {
  		Error error(code, string("No Error"));
  
  
  
  
  		if (isprimitive("std::string")) {
  			pJson = json_from_string(data, NULL);
  			jsonToValue(&out, pJson, "std::string", "std::string");
  			json_node_free(pJson);
  
  			if ("std::string" == "std::string") {
  				string* val = (std::string*)(&out);
  				if (val->empty() && p_chunk.size>4) {
  					*val = string(p_chunk.memory, p_chunk.size);
  				}
  			}
  		} else {
  			
  		}
  		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 berryFlavorListHelper(char * accessToken,
  	int limit, int offset, 
  	void(* handler)(std::string, 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 <string, string> queryParams;
  	string itemAtq;
  	
  
  	itemAtq = stringify(&limit, "int");
  	queryParams.insert(pair<string, string>("limit", itemAtq));
  	if( itemAtq.empty()==true){
  		queryParams.erase("limit");
  	}
  
  
  	itemAtq = stringify(&offset, "int");
  	queryParams.insert(pair<string, string>("offset", itemAtq));
  	if( itemAtq.empty()==true){
  		queryParams.erase("offset");
  	}
  
  	string mBody = "";
  	JsonNode* node;
  	JsonArray* json_array;
  
  	string url("/api/v2/berry-flavor/");
  	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(BerryFlavorManager::getBasePath(), url, myhttpmethod, queryParams,
  			mBody, headerList, p_chunk, &code, errormsg);
  		bool retval = berryFlavorListProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(handler));
  
  		curl_slist_freeList_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 (BerryFlavorManager::getBasePath(), url, myhttpmethod, queryParams,
  			mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), berryFlavorListProcessor);;
  		if(requestInfo == NULL)
  			return false;
  
  		thread = g_thread_new(NULL, __BerryFlavorManagerthreadFunc, static_cast<gpointer>(requestInfo));
  		return true;
  	}
  }
  
  
  
  
  bool BerryFlavorManager::berryFlavorListAsync(char * accessToken,
  	int limit, int offset, 
  	void(* handler)(std::string, Error, void* )
  	, void* userData)
  {
  	return berryFlavorListHelper(accessToken,
  	limit, offset, 
  	handler, userData, true);
  }
  
  bool BerryFlavorManager::berryFlavorListSync(char * accessToken,
  	int limit, int offset, 
  	void(* handler)(std::string, Error, void* )
  	, void* userData)
  {
  	return berryFlavorListHelper(accessToken,
  	limit, offset, 
  	handler, userData, false);
  }
  
  static bool berryFlavorReadProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
  	void(* voidHandler)())
  {
  	void(* handler)(std::string, Error, void* )
  	= reinterpret_cast<void(*)(std::string, Error, void* )> (voidHandler);
  	
  	JsonNode* pJson;
  	char * data = p_chunk.memory;
  
  	
  	std::string out;
  
  	if (code >= 200 && code < 300) {
  		Error error(code, string("No Error"));
  
  
  
  
  		if (isprimitive("std::string")) {
  			pJson = json_from_string(data, NULL);
  			jsonToValue(&out, pJson, "std::string", "std::string");
  			json_node_free(pJson);
  
  			if ("std::string" == "std::string") {
  				string* val = (std::string*)(&out);
  				if (val->empty() && p_chunk.size>4) {
  					*val = string(p_chunk.memory, p_chunk.size);
  				}
  			}
  		} else {
  			
  		}
  		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 berryFlavorReadHelper(char * accessToken,
  	int id, 
  	void(* handler)(std::string, 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 <string, string> queryParams;
  	string itemAtq;
  	
  	string mBody = "";
  	JsonNode* node;
  	JsonArray* json_array;
  
  	string url("/api/v2/berry-flavor/{id}/");
  	int pos;
  
  	string s_id("{");
  	s_id.append("id");
  	s_id.append("}");
  	pos = url.find(s_id);
  	url.erase(pos, s_id.length());
  	url.insert(pos, stringify(&id, "int"));
  
  	//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(BerryFlavorManager::getBasePath(), url, myhttpmethod, queryParams,
  			mBody, headerList, p_chunk, &code, errormsg);
  		bool retval = berryFlavorReadProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(handler));
  
  		curl_slist_freeList_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 (BerryFlavorManager::getBasePath(), url, myhttpmethod, queryParams,
  			mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), berryFlavorReadProcessor);;
  		if(requestInfo == NULL)
  			return false;
  
  		thread = g_thread_new(NULL, __BerryFlavorManagerthreadFunc, static_cast<gpointer>(requestInfo));
  		return true;
  	}
  }
  
  
  
  
  bool BerryFlavorManager::berryFlavorReadAsync(char * accessToken,
  	int id, 
  	void(* handler)(std::string, Error, void* )
  	, void* userData)
  {
  	return berryFlavorReadHelper(accessToken,
  	id, 
  	handler, userData, true);
  }
  
  bool BerryFlavorManager::berryFlavorReadSync(char * accessToken,
  	int id, 
  	void(* handler)(std::string, Error, void* )
  	, void* userData)
  {
  	return berryFlavorReadHelper(accessToken,
  	id, 
  	handler, userData, false);
  }
  
  