diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f5a1ad7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = space +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d579f20 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dlapp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c50d83d --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2014 Doubleleft + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40b1a5d --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +default: + clang -lstdc++ src/main.cpp src/cJSON.c src/Client.cpp src/Request.cpp src/Collection.cpp src/Dictionary.cpp src/Tools.cpp -o dlapp -lcurl + ./dlapp diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1d8f6a --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +hook-cpp +=== + +License +--- + +MIT diff --git a/build.sh b/build.sh deleted file mode 100644 index f408ef3..0000000 --- a/build.sh +++ /dev/null @@ -1,7 +0,0 @@ -# g++ src/main.c -o main.o -c -# g++ src/dlapi.c -o dlapi.o -c -# g++ -o dlapi main.o dlapi.o -lcurl - -clang -lstdc++ src/main.cpp src/cJSON.c src/Client.cpp src/Request.cpp src/Collection.cpp src/Dictionary.cpp src/Tools.cpp -o dlapp -lcurl - -./dlapp \ No newline at end of file diff --git a/dlapp b/dlapp index 281ba0c..69aebd0 100755 Binary files a/dlapp and b/dlapp differ diff --git a/src/Client.cpp b/src/Client.cpp index 23cb578..90fc7cd 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -1,43 +1,43 @@ #include "Client.h" -const std::string DLAPI::Method::GET = "GET"; -const std::string DLAPI::Method::POST = "POST"; -const std::string DLAPI::Method::PUT = "PUT"; -const std::string DLAPI::Method::DELETE = "DELETE"; +const std::string Hook::Method::GET = "GET"; +const std::string Hook::Method::POST = "POST"; +const std::string Hook::Method::PUT = "PUT"; +const std::string Hook::Method::DELETE = "DELETE"; -DLAPI::Client::Client(std::string url, std::string key, std::string appId) +Hook::Client::Client(std::string url, std::string key, std::string appId) { - init(url, key, appId); + init(url, key, appId); } -DLAPI::Client::~Client() +Hook::Client::~Client() { - + } -void DLAPI::Client::init(std::string url, std::string key, std::string appId) +void Hook::Client::init(std::string url, std::string key, std::string appId) { - DLAPI::Log("init - url:%s key:%s appId:%s", url.c_str(), key.c_str(), appId.c_str()); + Hook::Log("init - url:%s key:%s appId:%s", url.c_str(), key.c_str(), appId.c_str()); - this->url = url; - this->key = key; - this->appId = appId; + this->url = url; + this->key = key; + this->appId = appId; - headers.push_back("Content-Type: application/json"); - headers.push_back(DLAPI::Str::format("X-App-Id: %s", appId.c_str())); - headers.push_back(DLAPI::Str::format("X-App-Key: %s", key.c_str())); + headers.push_back("Content-Type: application/json"); + headers.push_back(Hook::Str::format("X-App-Id: %s", appId.c_str())); + headers.push_back(Hook::Str::format("X-App-Key: %s", key.c_str())); } -DLAPI::Request DLAPI::Client::request(std::string method, std::string segments, DLAPI::Dictionary* paramsDict, std::string query) +Hook::Request Hook::Client::request(std::string method, std::string segments, Hook::Dictionary* paramsDict, std::string query) { - DLAPI::Request request; - - request.query = query; - request.params = DLAPI::Str::format("{\"data\":%s}", paramsDict->toJSONString().c_str()); - request.url = DLAPI::Str::format("%s/%s", url.c_str(), segments.c_str()); - request.method = method; - request.headers = headers; - request.execute(); - - return request; + Hook::Request request; + + request.query = query; + request.params = Hook::Str::format("{\"data\":%s}", paramsDict->toJSONString().c_str()); + request.url = Hook::Str::format("%s/%s", url.c_str(), segments.c_str()); + request.method = method; + request.headers = headers; + request.execute(); + + return request; } diff --git a/src/Client.h b/src/Client.h index dc9af9d..20e8c00 100644 --- a/src/Client.h +++ b/src/Client.h @@ -1,5 +1,5 @@ -#ifndef dlapi_client_h -#define dlapi_client_h +#ifndef hook_client_h +#define hook_client_h #include #include @@ -8,25 +8,25 @@ #include "Request.h" #include "Tools.h" -namespace DLAPI +namespace Hook { - class Client - { - public: - Client(std::string url, std::string key, std::string appId); - ~Client(); + class Client + { + public: + Client(std::string url, std::string key, std::string appId); + ~Client(); - std::string url; - std::string key; - std::string appId; + std::string url; + std::string key; + std::string appId; - DLAPI::Request request(std::string method, std::string segments, DLAPI::Dictionary* paramsDict, std::string query); - - private: - std::vector headers; + Hook::Request request(std::string method, std::string segments, Hook::Dictionary* paramsDict, std::string query); - void init(std::string url, std::string key, std::string appId); - }; + private: + std::vector headers; + + void init(std::string url, std::string key, std::string appId); + }; } -#endif \ No newline at end of file +#endif diff --git a/src/Collection.cpp b/src/Collection.cpp index ea57e75..7355572 100755 --- a/src/Collection.cpp +++ b/src/Collection.cpp @@ -1,126 +1,126 @@ #include "Collection.h" -DLAPI::Collection::Collection() : name(""), client(NULL) +Hook::Collection::Collection() : name(""), client(NULL) { } -DLAPI::Collection::Collection(DLAPI::Client *client) : client(client), name(""){ +Hook::Collection::Collection(Hook::Client *client) : client(client), name(""){ } -DLAPI::Collection::~Collection() +Hook::Collection::~Collection() { - client = NULL; + client = NULL; } -void DLAPI::Collection::create() +void Hook::Collection::create() { - if (client == NULL) { - std::cout << "no client set. aborting" << std::endl; - return; - } - client->request(DLAPI::Method::POST, getSegments(), ¶ms, ""); + if (client == NULL) { + std::cout << "no client set. aborting" << std::endl; + return; + } + client->request(Hook::Method::POST, getSegments(), ¶ms, ""); } -void DLAPI::Collection::fetch() +void Hook::Collection::fetch() { - if (client == NULL) { - std::cout << "no client set. aborting" << std::endl; - return; - } - - result.resize(0); - DLAPI::Request request = client->request(DLAPI::Method::GET, getSegments(), ¶ms, getQuery()); - - cJSON* json = cJSON_Parse(request.response.c_str()); - if (json == NULL) - return; - - int size = cJSON_GetArraySize(json); - - for (int i = 0; i < size; i++) - { - cJSON* item = cJSON_GetArrayItem(json, i); - DLAPI::Dictionary dict; - dict.fromJSONString(std::string(cJSON_Print(item))); - result.push_back(dict); - } - - cJSON_Delete(json); - json = NULL; + if (client == NULL) { + std::cout << "no client set. aborting" << std::endl; + return; + } + + result.resize(0); + Hook::Request request = client->request(Hook::Method::GET, getSegments(), ¶ms, getQuery()); + + cJSON* json = cJSON_Parse(request.response.c_str()); + if (json == NULL) + return; + + int size = cJSON_GetArraySize(json); + + for (int i = 0; i < size; i++) + { + cJSON* item = cJSON_GetArrayItem(json, i); + Hook::Dictionary dict; + dict.fromJSONString(std::string(cJSON_Print(item))); + result.push_back(dict); + } + + cJSON_Delete(json); + json = NULL; } -void DLAPI::Collection::update() +void Hook::Collection::update() { - if (client == NULL) { - std::cout << "no client set. aborting" << std::endl; - return; - } - DLAPI::Request request = client->request(DLAPI::Method::POST, getSegmentsWithId(), ¶ms, getQuery()); + if (client == NULL) { + std::cout << "no client set. aborting" << std::endl; + return; + } + Hook::Request request = client->request(Hook::Method::POST, getSegmentsWithId(), ¶ms, getQuery()); } -std::string DLAPI::Collection::getSegments() +std::string Hook::Collection::getSegments() { - std::string service = DLAPI::Str::format("collection/%s", name.c_str()); - return service; + std::string service = Hook::Str::format("collection/%s", name.c_str()); + return service; } -std::string DLAPI::Collection::getSegmentsWithId() +std::string Hook::Collection::getSegmentsWithId() { - std::string service = DLAPI::Str::format("collection/%s/%s", name.c_str(), params.getString("_id").c_str()); - return service; + std::string service = Hook::Str::format("collection/%s/%s", name.c_str(), params.getString("_id").c_str()); + return service; } -void DLAPI::Collection::addQueryArg(std::string field, std::string operation, std::string value) +void Hook::Collection::addQueryArg(std::string field, std::string operation, std::string value) { - int size = queryArgs.size(); - queryArgs.setString(DLAPI::Str::format("qa%i", size), DLAPI::Str::format("[\"%s\",\"%s\",\"%s\"]", field.c_str(), operation.c_str(), value.c_str())); + int size = queryArgs.size(); + queryArgs.setString(Hook::Str::format("qa%i", size), Hook::Str::format("[\"%s\",\"%s\",\"%s\"]", field.c_str(), operation.c_str(), value.c_str())); } -void DLAPI::Collection::addQueryArg(std::string field, std::string operation, int value) +void Hook::Collection::addQueryArg(std::string field, std::string operation, int value) { - int size = queryArgs.size(); - queryArgs.setString(DLAPI::Str::format("qa%i", size), DLAPI::Str::format("[\"%s\",\"%s\",%i]", field.c_str(), operation.c_str(), value)); + int size = queryArgs.size(); + queryArgs.setString(Hook::Str::format("qa%i", size), Hook::Str::format("[\"%s\",\"%s\",%i]", field.c_str(), operation.c_str(), value)); } -void DLAPI::Collection::sort(std::string field, DLAPI::Collection::SortingOption operation) +void Hook::Collection::sort(std::string field, Hook::Collection::SortingOption operation) { - ordering.push_back(std::pair(field, operation)); + ordering.push_back(std::pair(field, operation)); } -std::string DLAPI::Collection::getQuery() +std::string Hook::Collection::getQuery() { - int size = queryArgs.size(); - std::string str = "{\"q\":["; - - // Query - for (int i = 0; i < size; i++) - { - if (i != 0) str = str.append(","); - std::string key = queryArgs.getKeyByIndex(i); - std::string val = queryArgs.getString(key); - str = str.append(val); - } - str = str.append("]"); - - // Sorting - if (ordering.size() > 0) { - str = str.append(", \"s\": ["); - for (int i = 0; i < ordering.size(); i++) - { - std::pair p = ordering[i]; - if (i != 0) str = str.append(","); - std::string key = p.first; - std::string val = p.second == Ascending ? "asc" : "desc"; - str = str.append(DLAPI::Str::format("[\"%s\",\"%s\"]", key.c_str(), val.c_str())); - } - str = str.append("]"); + int size = queryArgs.size(); + std::string str = "{\"q\":["; + + // Query + for (int i = 0; i < size; i++) + { + if (i != 0) str = str.append(","); + std::string key = queryArgs.getKeyByIndex(i); + std::string val = queryArgs.getString(key); + str = str.append(val); + } + str = str.append("]"); + + // Sorting + if (ordering.size() > 0) { + str = str.append(", \"s\": ["); + for (int i = 0; i < ordering.size(); i++) + { + std::pair p = ordering[i]; + if (i != 0) str = str.append(","); + std::string key = p.first; + std::string val = p.second == Ascending ? "asc" : "desc"; + str = str.append(Hook::Str::format("[\"%s\",\"%s\"]", key.c_str(), val.c_str())); } + str = str.append("]"); + } - str = str.append("}"); + str = str.append("}"); - return str; + return str; } diff --git a/src/Collection.h b/src/Collection.h index 78d82f2..290ea31 100755 --- a/src/Collection.h +++ b/src/Collection.h @@ -1,45 +1,45 @@ -#ifndef dlapi_collection_h -#define dlapi_collection_h +#ifndef hook_collection_h +#define hook_collection_h #include #include "Client.h" #include "Dictionary.h" #include "cJSON.h" -namespace DLAPI +namespace Hook { - class Collection - { - public: - - enum SortingOption{ - Ascending, - Descending - }; - - Collection(); - Collection(DLAPI::Client *client); - ~Collection(); - - std::string name; - DLAPI::Dictionary params; - DLAPI::Client* client; - std::vector result; - DLAPI::Dictionary queryArgs; - - std::string getSegments(); - std::string getSegmentsWithId(); - void create(); - void fetch(); - void update(); - void addQueryArg(std::string field, std::string operation, std::string value); - void addQueryArg(std::string field, std::string operation, int value); - void sort(std::string field, SortingOption order); - std::string getQuery(); - + class Collection + { + public: + + enum SortingOption{ + Ascending, + Descending + }; + + Collection(); + Collection(Hook::Client *client); + ~Collection(); + + std::string name; + Hook::Dictionary params; + Hook::Client* client; + std::vector result; + Hook::Dictionary queryArgs; + + std::string getSegments(); + std::string getSegmentsWithId(); + void create(); + void fetch(); + void update(); + void addQueryArg(std::string field, std::string operation, std::string value); + void addQueryArg(std::string field, std::string operation, int value); + void sort(std::string field, SortingOption order); + std::string getQuery(); + private: - std::vector > ordering; - }; + std::vector > ordering; + }; } #endif diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp index da5a655..975ceb8 100755 --- a/src/Dictionary.cpp +++ b/src/Dictionary.cpp @@ -1,131 +1,131 @@ #include "Dictionary.h" -DLAPI::Dictionary::Dictionary() +Hook::Dictionary::Dictionary() { - json = cJSON_CreateObject(); + json = cJSON_CreateObject(); } -DLAPI::Dictionary::~Dictionary() +Hook::Dictionary::~Dictionary() { - // if (json) cJSON_Delete(json); - // json = NULL; + // if (json) cJSON_Delete(json); + // json = NULL; } -void DLAPI::Dictionary::clear() +void Hook::Dictionary::clear() { - if (json) cJSON_Delete(json); - json = cJSON_CreateObject(); + if (json) cJSON_Delete(json); + json = cJSON_CreateObject(); } -std::string DLAPI::Dictionary::getKeyByIndex(int index) +std::string Hook::Dictionary::getKeyByIndex(int index) { - std::string r = ""; - int i = 0; - - cJSON* it = json->child; - while (it) - { - if (i == index) - { - r = std::string(it->string); - printf("%s\n", r.c_str()); - break; - } - i++; - it = it->next; - } - - return r; + std::string r = ""; + int i = 0; + + cJSON* it = json->child; + while (it) + { + if (i == index) + { + r = std::string(it->string); + printf("%s\n", r.c_str()); + break; + } + i++; + it = it->next; + } + + return r; } -void DLAPI::Dictionary::setString(std::string key, std::string value) +void Hook::Dictionary::setString(std::string key, std::string value) { - cJSON* j = cJSON_CreateString(value.c_str()); - cJSON_AddItemToObject(json, key.c_str(), j); -} + cJSON* j = cJSON_CreateString(value.c_str()); + cJSON_AddItemToObject(json, key.c_str(), j); +} -void DLAPI::Dictionary::setNumber(std::string key, double value) +void Hook::Dictionary::setNumber(std::string key, double value) { - cJSON* j = cJSON_CreateNumber(value); - cJSON_AddItemToObject(json, key.c_str(), j); + cJSON* j = cJSON_CreateNumber(value); + cJSON_AddItemToObject(json, key.c_str(), j); } -std::string DLAPI::Dictionary::getString(std::string key) +std::string Hook::Dictionary::getString(std::string key) { - std::string result = ""; - cJSON* j = cJSON_GetObjectItem(json, key.c_str()); - if (j && j->valuestring != NULL) result = std::string(j->valuestring); - return result; + std::string result = ""; + cJSON* j = cJSON_GetObjectItem(json, key.c_str()); + if (j && j->valuestring != NULL) result = std::string(j->valuestring); + return result; } -const char* DLAPI::Dictionary::getCString(std::string key) +const char* Hook::Dictionary::getCString(std::string key) { - return getString(key).c_str(); + return getString(key).c_str(); } -double DLAPI::Dictionary::getNumber(std::string key) +double Hook::Dictionary::getNumber(std::string key) { - double result = 0; - cJSON* j = cJSON_GetObjectItem(json, key.c_str()); - if (j) result = j->valuedouble; - return result; + double result = 0; + cJSON* j = cJSON_GetObjectItem(json, key.c_str()); + if (j) result = j->valuedouble; + return result; } -int DLAPI::Dictionary::getInt(std::string key) +int Hook::Dictionary::getInt(std::string key) { - return (int)getNumber(key); + return (int)getNumber(key); } -int DLAPI::Dictionary::size() +int Hook::Dictionary::size() { - int r = 0; + int r = 0; - cJSON* it = json->child; - while (it) - { - r++; - it = it->next; - } + cJSON* it = json->child; + while (it) + { + r++; + it = it->next; + } - return r; + return r; } -std::string DLAPI::Dictionary::toURLParams() +std::string Hook::Dictionary::toURLParams() { - std::string r = ""; - - cJSON* it = json->child; - while (it) - { - std::string str = ""; - const char* k = it->string; - - switch (it->type) - { - case cJSON_Number: - str = DLAPI::Str::format("%s=%d", it->string, it->valueint); - break; - case cJSON_String: - str = DLAPI::Str::format("%s=%s", it->string, it->valuestring); - break; - } - - r.append(str); - it = it->next; - if (it) r.append("&"); - } - - return r; + std::string r = ""; + + cJSON* it = json->child; + while (it) + { + std::string str = ""; + const char* k = it->string; + + switch (it->type) + { + case cJSON_Number: + str = Hook::Str::format("%s=%d", it->string, it->valueint); + break; + case cJSON_String: + str = Hook::Str::format("%s=%s", it->string, it->valuestring); + break; + } + + r.append(str); + it = it->next; + if (it) r.append("&"); + } + + return r; } -std::string DLAPI::Dictionary::toJSONString() +std::string Hook::Dictionary::toJSONString() { - return std::string(cJSON_Print(json)); + return std::string(cJSON_Print(json)); } -void DLAPI::Dictionary::fromJSONString(std::string str) +void Hook::Dictionary::fromJSONString(std::string str) { - if (json) cJSON_Delete(json); - json = cJSON_Parse(str.c_str()); - DLAPI::Log("fromJSONString %s\n", cJSON_Print(json)); -} \ No newline at end of file + if (json) cJSON_Delete(json); + json = cJSON_Parse(str.c_str()); + Hook::Log("fromJSONString %s\n", cJSON_Print(json)); +} diff --git a/src/Dictionary.h b/src/Dictionary.h index acc0dfe..60b7c2a 100644 --- a/src/Dictionary.h +++ b/src/Dictionary.h @@ -1,36 +1,36 @@ -#ifndef dlapi_dictionary_h -#define dlapi_dictionary_h +#ifndef hook_dictionary_h +#define hook_dictionary_h #include #include "cJSON.h" #include "Tools.h" -namespace DLAPI +namespace Hook { - class Dictionary - { - public: - Dictionary(); - ~Dictionary(); + class Dictionary + { + public: + Dictionary(); + ~Dictionary(); - void setString(std::string key, std::string value); - void setNumber(std::string key, double value); - void clear(); - int size(); + void setString(std::string key, std::string value); + void setNumber(std::string key, double value); + void clear(); + int size(); - std::string getKeyByIndex(int index); - std::string getString(std::string key); - const char* getCString(std::string key); - double getNumber(std::string key); - int getInt(std::string key); + std::string getKeyByIndex(int index); + std::string getString(std::string key); + const char* getCString(std::string key); + double getNumber(std::string key); + int getInt(std::string key); - std::string toURLParams(); - std::string toJSONString(); - void fromJSONString(std::string str); + std::string toURLParams(); + std::string toJSONString(); + void fromJSONString(std::string str); - private: - cJSON* json; - }; + private: + cJSON* json; + }; } #endif diff --git a/src/DLApi.h b/src/Hook.h similarity index 69% rename from src/DLApi.h rename to src/Hook.h index 3797664..bf271cf 100644 --- a/src/DLApi.h +++ b/src/Hook.h @@ -1,5 +1,5 @@ -#ifndef dlapi_dlapi_h -#define dlapi_dlapi_h +#ifndef hook_hook_h +#define hook_hook_h #include "Client.h" #include "Request.h" diff --git a/src/Request.cpp b/src/Request.cpp index 1ceb8c4..c76b2b2 100644 --- a/src/Request.cpp +++ b/src/Request.cpp @@ -2,83 +2,83 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { - ((std::string*)userp)->append((char*)contents, size * nmemb); - return size * nmemb; + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; } static std::string curlRequest(std::string method, std::string url, std::vector headers, std::string params, std::string query) { - std::string readBuffer = ""; + std::string readBuffer = ""; #ifdef __APPLE__ - CURL *curl; - CURLcode res; - - curl = curl_easy_init(); - - if (!curl) - { - DLAPI::Log("ERROR! Curl not initialized."); - return ""; - } - - curl_slist* headerList = NULL; - - for (int i = 0; i < headers.size(); i++) - { - const char* headerStr = headers[i].c_str(); - headerList = curl_slist_append(headerList, headerStr); - } - - const char* paramsStr = params.c_str(); - const char* paramsEsc = curl_easy_escape(curl, paramsStr, strlen(paramsStr)); - // printf("%s\n", paramsStr); - - const char* queryStr = query.c_str(); - const char* queryEsc = curl_easy_escape(curl, queryStr, strlen(queryStr)); - - if (method.compare(DLAPI::Method::GET) == 0) - { - url = DLAPI::Str::format("%s?%s", url.c_str(), queryEsc); - } - - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, paramsStr); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(paramsStr)); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str()); - - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - - res = curl_easy_perform(curl); - - curl_slist_free_all(headerList); - curl_easy_cleanup(curl); - - printf("\n"); + CURL *curl; + CURLcode res; + + curl = curl_easy_init(); + + if (!curl) + { + Hook::Log("ERROR! Curl not initialized."); + return ""; + } + + curl_slist* headerList = NULL; + + for (int i = 0; i < headers.size(); i++) + { + const char* headerStr = headers[i].c_str(); + headerList = curl_slist_append(headerList, headerStr); + } + + const char* paramsStr = params.c_str(); + const char* paramsEsc = curl_easy_escape(curl, paramsStr, strlen(paramsStr)); + // printf("%s\n", paramsStr); + + const char* queryStr = query.c_str(); + const char* queryEsc = curl_easy_escape(curl, queryStr, strlen(queryStr)); + + if (method.compare(Hook::Method::GET) == 0) + { + url = Hook::Str::format("%s?%s", url.c_str(), queryEsc); + } + + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, paramsStr); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(paramsStr)); + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str()); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + + res = curl_easy_perform(curl); + + curl_slist_free_all(headerList); + curl_easy_cleanup(curl); + + printf("\n"); #endif - return readBuffer; + return readBuffer; } -DLAPI::Request::Request() +Hook::Request::Request() { - data = NULL; - response = ""; - params = ""; - query = ""; + data = NULL; + response = ""; + params = ""; + query = ""; } -DLAPI::Request::~Request() +Hook::Request::~Request() { - + } -void DLAPI::Request::execute() +void Hook::Request::execute() { - DLAPI::Log("execute %s - url:%s params:%s query:%s", method.c_str(), url.c_str(), params.c_str(), query.c_str()); - response = curlRequest(method, url, headers, params, query); + Hook::Log("execute %s - url:%s params:%s query:%s", method.c_str(), url.c_str(), params.c_str(), query.c_str()); + response = curlRequest(method, url, headers, params, query); } diff --git a/src/Request.h b/src/Request.h index fa8f416..0da5820 100644 --- a/src/Request.h +++ b/src/Request.h @@ -1,5 +1,5 @@ -#ifndef dlapi_request_h -#define dlapi_request_h +#ifndef Hook_request_h +#define Hook_request_h #include #include @@ -7,25 +7,25 @@ #include "Dictionary.h" #include "Tools.h" -namespace DLAPI +namespace Hook { - class Request - { - public: - Request(); - ~Request(); + class Request + { + public: + Request(); + ~Request(); - std::string method; - std::string url; - std::string params; - std::string query; - std::vector headers; - DLAPI::Dictionary* data; - std::string response; - int error; + std::string method; + std::string url; + std::string params; + std::string query; + std::vector headers; + Hook::Dictionary* data; + std::string response; + int error; - void execute(); - }; + void execute(); + }; } #endif diff --git a/src/Tools.cpp b/src/Tools.cpp index 51c9348..f70624c 100644 --- a/src/Tools.cpp +++ b/src/Tools.cpp @@ -1,21 +1,21 @@ #include "Tools.h" -void DLAPI::Log(std::string str, ...) +void Hook::Log(std::string str, ...) { - char dest[2048 * 16]; - va_list argptr; - va_start(argptr, str); - vsprintf(dest, str.c_str(), argptr); - va_end(argptr); - printf("[DLAPI] %s\n", dest); + char dest[2048 * 16]; + va_list argptr; + va_start(argptr, str); + vsprintf(dest, str.c_str(), argptr); + va_end(argptr); + printf("[Hook] %s\n", dest); } -std::string DLAPI::Str::format(std::string str, ...) +std::string Hook::Str::format(std::string str, ...) { - char newStr[2048 * 16]; - va_list vl; - va_start(vl, str); - va_end(vl); - vsprintf(newStr, str.c_str(), vl); - return std::string(newStr); -} \ No newline at end of file + char newStr[2048 * 16]; + va_list vl; + va_start(vl, str); + va_end(vl); + vsprintf(newStr, str.c_str(), vl); + return std::string(newStr); +} diff --git a/src/Tools.h b/src/Tools.h index 3c0b701..356c324 100644 --- a/src/Tools.h +++ b/src/Tools.h @@ -1,26 +1,26 @@ -#ifndef dlapi_tools_h -#define dlapi_tools_h +#ifndef hook_tools_h +#define hook_tools_h #include #include -namespace DLAPI +namespace Hook { - namespace Method - { - extern const std::string GET; - extern const std::string POST; - extern const std::string PUT; - extern const std::string DELETE; - } - - extern void Log(std::string str, ...); + namespace Method + { + extern const std::string GET; + extern const std::string POST; + extern const std::string PUT; + extern const std::string DELETE; + } - class Str - { - public: - static std::string format(std::string str, ...); - }; + extern void Log(std::string str, ...); + + class Str + { + public: + static std::string format(std::string str, ...); + }; } -#endif \ No newline at end of file +#endif diff --git a/src/main.cpp b/src/main.cpp index 6a4550c..b73dc54 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,43 +1,42 @@ #include -#include "dlapi.h" - +#include "Hook.h" -int main(int argc, char** argv) +int main(int argc, char** argv) { - DLAPI::Client* client = new DLAPI::Client("http://dl-api.ddll.co", "q1uU7tFtXnLad6FIGGn2cB+gxcx64/uPoDhqe2Zn5AE=", "1"); - - DLAPI::Collection collection1; - collection1.name = "guarana_leaderboards"; - collection1.params.setString("name", "MDT"); - collection1.params.setString("fbid", "1235"); - collection1.client = client; - // collection1.create(); - - // DLAPI::Collection collection2; - // collection2.name = "guarana_leaderboards"; - // collection2.client = client; - // collection2.addQueryArg("fbid", "=", "1234"); - // collection2.fetch(); - - DLAPI::Collection collection3; - collection3.name = "guarana_leaderboards"; - collection3.params.setString("_id", "20"); - collection3.params.setString("name", "MDT"); - collection3.client = client; - collection3.update(); - - DLAPI::Collection collection2; - collection2.name = "guarana_leaderboards"; - collection2.client = client; - collection2.addQueryArg("fbid", "=", "1235"); - collection2.fetch(); - - // std::string args = "{\"q\":[[\"_id\",\"=\",93]]}"; - // collection2.fetch(args); - - // DLAPI::Dictionary result = collection2.result; - - // DLAPI::Log("result: %i", collection2.result.size()); - - return 0; -} \ No newline at end of file + Hook::Client* client = new Hook::Client("http://dl-api.ddll.co", "q1uU7tFtXnLad6FIGGn2cB+gxcx64/uPoDhqe2Zn5AE=", "1"); + + Hook::Collection collection1; + collection1.name = "guarana_leaderboards"; + collection1.params.setString("name", "MDT"); + collection1.params.setString("fbid", "1235"); + collection1.client = client; + // collection1.create(); + + // Hook::Collection collection2; + // collection2.name = "guarana_leaderboards"; + // collection2.client = client; + // collection2.addQueryArg("fbid", "=", "1234"); + // collection2.fetch(); + + Hook::Collection collection3; + collection3.name = "guarana_leaderboards"; + collection3.params.setString("_id", "20"); + collection3.params.setString("name", "MDT"); + collection3.client = client; + collection3.update(); + + Hook::Collection collection2; + collection2.name = "guarana_leaderboards"; + collection2.client = client; + collection2.addQueryArg("fbid", "=", "1235"); + collection2.fetch(); + + // std::string args = "{\"q\":[[\"_id\",\"=\",93]]}"; + // collection2.fetch(args); + + // Hook::Dictionary result = collection2.result; + + // Hook::Log("result: %i", collection2.result.size()); + + return 0; +}