commit a6d188960fcfa189ae4f92d1f5f6e6627f856dd1 Author: marcelb Date: Sun May 21 09:10:54 2023 +0200 Tested diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fe45693 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,21 @@ +{ + "files.associations": { + "string": "cpp", + "string_view": "cpp", + "array": "cpp", + "atomic": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "functional": "cpp", + "random": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "new": "cpp", + "ostream": "cpp", + "streambuf": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/lib/api.hpp b/lib/api.hpp new file mode 100644 index 0000000..b118e9e --- /dev/null +++ b/lib/api.hpp @@ -0,0 +1,42 @@ +#ifndef _API_ +#define _API_ + +#include +#include +#include +#include + +using namespace std; + +class defapi { + public: + vector options; + vector keys; + + map> val_matrix; + + defapi(const vector _options, const vector _keys); + void necessary(const string _option, const vector _keys); + +}; + +class api { + public: + defapi *def; + + string option; + map object; + string body; + + api(defapi *_def, const string _option, const map _object); + api(defapi *_def, const string _body); + + private: + bool validate(); + void parse(); // čitaj api + void format(); // šalji api + + // ~api(); +}; + +#endif diff --git a/src/api.cpp b/src/api.cpp new file mode 100644 index 0000000..b354080 --- /dev/null +++ b/src/api.cpp @@ -0,0 +1,87 @@ +#include "../lib/api.hpp" + +defapi::defapi(const vector _options, const vector _keys) { + options = _options; + keys = _keys; +} + +void defapi::necessary(const string _option, const vector _keys) { + val_matrix[_option].insert( val_matrix[_option].end(), _keys.begin(), _keys.end()); +} + +api::api(defapi *_def, const string _option, const map _object) { + def = _def; + object = _object; + option = _option; + + if (!validate()) { + cout << "Validate API error" << endl; + } + format(); + +} + +api::api(defapi *_def, const string _body) { + def = _def; + body = _body; + + parse(); + if (!validate()) { + cout << "Validate API error" << endl; + } + +} + +bool api::validate() { + bool isValidate = true; + + for (uint i=0; ival_matrix[option].size(); i++) { + def->val_matrix[option][i]; + if (object[def->val_matrix[option][i]].empty()) { + isValidate = false; + break; + } + } + + return isValidate; +} + +void api::parse() { + + for (uint i=0; ioptions.size(); i++) { + if (body.find("/"+def->options[i]+"/") < body.length()) { + option = def->options[i]; + } + } + + for (uint i=0; ikeys.size(); i++) { + string key = def->keys[i]+"="; + string value; + if (body.find(key) < body.length()) { + value = body.substr(body.find(key)+key.length(), body.find('&',body.find(key)+key.length())-body.find(key)-key.length() < body.find(' ',body.find(key)+key.length())-body.find(key)-key.length() ? body.find('&',body.find(key)+key.length())-body.find(key)-key.length() : body.find(' ',body.find(key)+key.length())-body.find(key)-key.length()); + } + object[def->keys[i]] = value; + } + +} + +void api::format() { + + body = "GET /"; + + if (!option.empty()) { + body += option + '/'; + } + + body += '?'; + + for (uint i=0; ikeys.size(); i++) { + if (!object[def->keys[i]].empty()) { + body += def->keys[i] + "=" + object[def->keys[i]] + "&"; + } + } + + body.pop_back(); + body += " HTTP/1.1"; + +} \ No newline at end of file diff --git a/test/compile.sh b/test/compile.sh new file mode 100644 index 0000000..9a28838 --- /dev/null +++ b/test/compile.sh @@ -0,0 +1 @@ +g++ test.cpp ../src/* -o test.o \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..a050777 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,25 @@ +#include + +#include "../lib/api.hpp" + +using namespace std; + +int main() { + + defapi myDef({"add", "delete", "update"}, {"id", "key", "value"}); + + // cout << myDef.keys[2]; + + myDef.necessary("add", {"id", "key", "value"}); + myDef.necessary("delete", {"id"}); + myDef.necessary("update", {"id"}); + myDef.necessary("update", {"value"}); + + // cout << myDef.val_matrix["delete"].empty(); + + api myApi(&myDef, "GET /update/?id=1&value=true HTTP/1.1"); + + cout << myApi.object["value"]; + + +} \ No newline at end of file diff --git a/test/test.o b/test/test.o new file mode 100755 index 0000000..2896458 Binary files /dev/null and b/test/test.o differ