Tested
This commit is contained in:
commit
a6d188960f
21
.vscode/settings.json
vendored
Normal file
21
.vscode/settings.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
42
lib/api.hpp
Normal file
42
lib/api.hpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#ifndef _API_
|
||||||
|
#define _API_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class defapi {
|
||||||
|
public:
|
||||||
|
vector<string> options;
|
||||||
|
vector<string> keys;
|
||||||
|
|
||||||
|
map<string, vector<string>> val_matrix;
|
||||||
|
|
||||||
|
defapi(const vector<string> _options, const vector<string> _keys);
|
||||||
|
void necessary(const string _option, const vector<string> _keys);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class api {
|
||||||
|
public:
|
||||||
|
defapi *def;
|
||||||
|
|
||||||
|
string option;
|
||||||
|
map<string, string> object;
|
||||||
|
string body;
|
||||||
|
|
||||||
|
api(defapi *_def, const string _option, const map<string, string> _object);
|
||||||
|
api(defapi *_def, const string _body);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool validate();
|
||||||
|
void parse(); // čitaj api
|
||||||
|
void format(); // šalji api
|
||||||
|
|
||||||
|
// ~api();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
87
src/api.cpp
Normal file
87
src/api.cpp
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
#include "../lib/api.hpp"
|
||||||
|
|
||||||
|
defapi::defapi(const vector<string> _options, const vector<string> _keys) {
|
||||||
|
options = _options;
|
||||||
|
keys = _keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
void defapi::necessary(const string _option, const vector<string> _keys) {
|
||||||
|
val_matrix[_option].insert( val_matrix[_option].end(), _keys.begin(), _keys.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
api::api(defapi *_def, const string _option, const map<string, string> _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; i<def->val_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; i<def->options.size(); i++) {
|
||||||
|
if (body.find("/"+def->options[i]+"/") < body.length()) {
|
||||||
|
option = def->options[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint i=0; i<def->keys.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; i<def->keys.size(); i++) {
|
||||||
|
if (!object[def->keys[i]].empty()) {
|
||||||
|
body += def->keys[i] + "=" + object[def->keys[i]] + "&";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body.pop_back();
|
||||||
|
body += " HTTP/1.1";
|
||||||
|
|
||||||
|
}
|
1
test/compile.sh
Normal file
1
test/compile.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
g++ test.cpp ../src/* -o test.o
|
25
test/test.cpp
Normal file
25
test/test.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#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"];
|
||||||
|
|
||||||
|
|
||||||
|
}
|
BIN
test/test.o
Executable file
BIN
test/test.o
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user