Files
openapi-generator/samples/client/petstore/cpp-tizen/src/UserManager.cpp
2022-02-22 16:27:34 +08:00

1184 lines
28 KiB
C++

#include <glib-object.h>
#include <json-glib/json-glib.h>
#include "UserManager.h"
#include "NetClient.h"
#include "Helpers.h"
#include "Error.h"
#include "RequestInfo.h"
using namespace std;
using namespace Tizen::ArtikCloud;
UserManager::UserManager()
{
}
UserManager::~UserManager()
{
}
static gboolean __UserManagerresponseHandler(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 __UserManagerthreadFunc(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(__UserManagerresponseHandler, static_cast<gpointer>(request));
return NULL;
}
static bool createUserProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(Error, void* ) = reinterpret_cast<void(*)(Error, void* )> (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 createUserHelper(char * accessToken,
User 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");
map <string, string> queryParams;
string itemAtq;
string mBody = "";
JsonNode* node;
JsonArray* json_array;
if (isprimitive("User")) {
node = converttoJson(&body, "User", "");
}
char *jsonStr = body.toJson();
node = json_from_string(jsonStr, NULL);
g_free(static_cast<gpointer>(jsonStr));
char *jsonStr1 = json_to_string(node, false);
mBody.append(jsonStr1);
g_free(static_cast<gpointer>(jsonStr1));
string url("/user");
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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = createUserProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), createUserProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::createUserAsync(char * accessToken,
User body,
void(* handler)(Error, void* ) , void* userData)
{
return createUserHelper(accessToken,
body,
handler, userData, true);
}
bool UserManager::createUserSync(char * accessToken,
User body,
void(* handler)(Error, void* ) , void* userData)
{
return createUserHelper(accessToken,
body,
handler, userData, false);
}
static bool createUsersWithArrayInputProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(Error, void* ) = reinterpret_cast<void(*)(Error, void* )> (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 createUsersWithArrayInputHelper(char * accessToken,
std::list<User> 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");
map <string, string> queryParams;
string itemAtq;
string mBody = "";
JsonNode* node;
JsonArray* json_array;
//TODO: Map Container
if (isprimitive("User")) {
node = converttoJson(&body, "User", "array");
} else {
node = json_node_alloc();
json_array = json_array_new();
for (std::list
<User>::iterator bodyIter = body.begin(); bodyIter != body.end(); ++bodyIter) {
User itemAt = (*bodyIter);
char *jsonStr = itemAt.toJson();
JsonNode *node_temp = json_from_string(jsonStr, NULL);
g_free(static_cast<gpointer>(jsonStr));
json_array_add_element(json_array, node_temp);
}
json_node_init_array(node, json_array);
json_array_unref(json_array);
}
char *jsonStr1 = json_to_string(node, false);
mBody.append(jsonStr1);
g_free(static_cast<gpointer>(jsonStr1));
string url("/user/createWithArray");
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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = createUsersWithArrayInputProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), createUsersWithArrayInputProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::createUsersWithArrayInputAsync(char * accessToken,
std::list<User> body,
void(* handler)(Error, void* ) , void* userData)
{
return createUsersWithArrayInputHelper(accessToken,
body,
handler, userData, true);
}
bool UserManager::createUsersWithArrayInputSync(char * accessToken,
std::list<User> body,
void(* handler)(Error, void* ) , void* userData)
{
return createUsersWithArrayInputHelper(accessToken,
body,
handler, userData, false);
}
static bool createUsersWithListInputProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(Error, void* ) = reinterpret_cast<void(*)(Error, void* )> (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 createUsersWithListInputHelper(char * accessToken,
std::list<User> 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");
map <string, string> queryParams;
string itemAtq;
string mBody = "";
JsonNode* node;
JsonArray* json_array;
//TODO: Map Container
if (isprimitive("User")) {
node = converttoJson(&body, "User", "array");
} else {
node = json_node_alloc();
json_array = json_array_new();
for (std::list
<User>::iterator bodyIter = body.begin(); bodyIter != body.end(); ++bodyIter) {
User itemAt = (*bodyIter);
char *jsonStr = itemAt.toJson();
JsonNode *node_temp = json_from_string(jsonStr, NULL);
g_free(static_cast<gpointer>(jsonStr));
json_array_add_element(json_array, node_temp);
}
json_node_init_array(node, json_array);
json_array_unref(json_array);
}
char *jsonStr1 = json_to_string(node, false);
mBody.append(jsonStr1);
g_free(static_cast<gpointer>(jsonStr1));
string url("/user/createWithList");
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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = createUsersWithListInputProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), createUsersWithListInputProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::createUsersWithListInputAsync(char * accessToken,
std::list<User> body,
void(* handler)(Error, void* ) , void* userData)
{
return createUsersWithListInputHelper(accessToken,
body,
handler, userData, true);
}
bool UserManager::createUsersWithListInputSync(char * accessToken,
std::list<User> body,
void(* handler)(Error, void* ) , void* userData)
{
return createUsersWithListInputHelper(accessToken,
body,
handler, userData, false);
}
static bool deleteUserProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(Error, void* ) = reinterpret_cast<void(*)(Error, void* )> (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 deleteUserHelper(char * accessToken,
std::string username,
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 <string, string> queryParams;
string itemAtq;
string mBody = "";
JsonNode* node;
JsonArray* json_array;
string url("/user/{username}");
int pos;
string s_username("{");
s_username.append("username");
s_username.append("}");
pos = url.find(s_username);
url.erase(pos, s_username.length());
url.insert(pos, stringify(&username, "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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = deleteUserProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), deleteUserProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::deleteUserAsync(char * accessToken,
std::string username,
void(* handler)(Error, void* ) , void* userData)
{
return deleteUserHelper(accessToken,
username,
handler, userData, true);
}
bool UserManager::deleteUserSync(char * accessToken,
std::string username,
void(* handler)(Error, void* ) , void* userData)
{
return deleteUserHelper(accessToken,
username,
handler, userData, false);
}
static bool getUserByNameProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(User, Error, void* )
= reinterpret_cast<void(*)(User, Error, void* )> (voidHandler);
JsonNode* pJson;
char * data = p_chunk.memory;
User out;
if (code >= 200 && code < 300) {
Error error(code, string("No Error"));
if (isprimitive("User")) {
pJson = json_from_string(data, NULL);
jsonToValue(&out, pJson, "User", "User");
json_node_free(pJson);
if ("User" == "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<gpointer>(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 getUserByNameHelper(char * accessToken,
std::string username,
void(* handler)(User, 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("/user/{username}");
int pos;
string s_username("{");
s_username.append("username");
s_username.append("}");
pos = url.find(s_username);
url.erase(pos, s_username.length());
url.insert(pos, stringify(&username, "std::string"));
//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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = getUserByNameProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), getUserByNameProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::getUserByNameAsync(char * accessToken,
std::string username,
void(* handler)(User, Error, void* )
, void* userData)
{
return getUserByNameHelper(accessToken,
username,
handler, userData, true);
}
bool UserManager::getUserByNameSync(char * accessToken,
std::string username,
void(* handler)(User, Error, void* )
, void* userData)
{
return getUserByNameHelper(accessToken,
username,
handler, userData, false);
}
static bool loginUserProcessor(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 loginUserHelper(char * accessToken,
std::string username, std::string password,
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(&username, "std::string");
queryParams.insert(pair<string, string>("username", itemAtq));
itemAtq = stringify(&password, "std::string");
queryParams.insert(pair<string, string>("password", itemAtq));
string mBody = "";
JsonNode* node;
JsonArray* json_array;
string url("/user/login");
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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = loginUserProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), loginUserProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::loginUserAsync(char * accessToken,
std::string username, std::string password,
void(* handler)(std::string, Error, void* )
, void* userData)
{
return loginUserHelper(accessToken,
username, password,
handler, userData, true);
}
bool UserManager::loginUserSync(char * accessToken,
std::string username, std::string password,
void(* handler)(std::string, Error, void* )
, void* userData)
{
return loginUserHelper(accessToken,
username, password,
handler, userData, false);
}
static bool logoutUserProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(Error, void* ) = reinterpret_cast<void(*)(Error, void* )> (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 logoutUserHelper(char * accessToken,
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 <string, string> queryParams;
string itemAtq;
string mBody = "";
JsonNode* node;
JsonArray* json_array;
string url("/user/logout");
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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = logoutUserProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), logoutUserProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::logoutUserAsync(char * accessToken,
void(* handler)(Error, void* ) , void* userData)
{
return logoutUserHelper(accessToken,
handler, userData, true);
}
bool UserManager::logoutUserSync(char * accessToken,
void(* handler)(Error, void* ) , void* userData)
{
return logoutUserHelper(accessToken,
handler, userData, false);
}
static bool updateUserProcessor(MemoryStruct_s p_chunk, long code, char* errormsg, void* userData,
void(* voidHandler)())
{
void(* handler)(Error, void* ) = reinterpret_cast<void(*)(Error, void* )> (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 updateUserHelper(char * accessToken,
std::string username, User 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");
map <string, string> queryParams;
string itemAtq;
string mBody = "";
JsonNode* node;
JsonArray* json_array;
if (isprimitive("User")) {
node = converttoJson(&body, "User", "");
}
char *jsonStr = body.toJson();
node = json_from_string(jsonStr, NULL);
g_free(static_cast<gpointer>(jsonStr));
char *jsonStr1 = json_to_string(node, false);
mBody.append(jsonStr1);
g_free(static_cast<gpointer>(jsonStr1));
string url("/user/{username}");
int pos;
string s_username("{");
s_username.append("username");
s_username.append("}");
pos = url.find(s_username);
url.erase(pos, s_username.length());
url.insert(pos, stringify(&username, "std::string"));
//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(UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg);
bool retval = updateUserProcessor(*p_chunk, code, errormsg, userData,reinterpret_cast<void(*)()>(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 (UserManager::getBasePath(), url, myhttpmethod, queryParams,
mBody, headerList, p_chunk, &code, errormsg, userData, reinterpret_cast<void(*)()>(handler), updateUserProcessor);;
if(requestInfo == NULL)
return false;
thread = g_thread_new(NULL, __UserManagerthreadFunc, static_cast<gpointer>(requestInfo));
return true;
}
}
bool UserManager::updateUserAsync(char * accessToken,
std::string username, User body,
void(* handler)(Error, void* ) , void* userData)
{
return updateUserHelper(accessToken,
username, body,
handler, userData, true);
}
bool UserManager::updateUserSync(char * accessToken,
std::string username, User body,
void(* handler)(Error, void* ) , void* userData)
{
return updateUserHelper(accessToken,
username, body,
handler, userData, false);
}