Delete API definition

namespace
marcelb 1 year ago
parent 3a7b5bd688
commit 4fdb494596
  1. 20
      lib/api.hpp
  2. 7
      lib/http.hpp
  3. 44
      src/api.cpp
  4. 26
      test/test.cpp
  5. BIN
      test/test.o

@ -13,22 +13,8 @@ using namespace std;
class http_request;
class http_response;
class defapi {
public:
vector<string> methods;
vector<string> paths;
vector<string> keys;
map<string, vector<string>> val_matrix;
defapi(const vector<string> _methods, const vector<string> _paths, const vector<string> _keys);
void necessary(const string _path, const vector<string> _keys);
};
class api {
public:
defapi* def;
string method;
string path;
map<string, string> params;
@ -36,11 +22,9 @@ class api {
string body;
// odlazni api zahtjev
api(defapi* _def, const string _method, const string _path, const map<string, string> _params = {}, const string _body = {});
api(const string _method, const string _path, const map<string, string> _params = {}, const string _body = {});
// dolazni api zahjtev
api(defapi* _def, const http_request _req);
// bool validate();
api(const http_request _req);
};
#endif

@ -9,8 +9,6 @@
using namespace std;
class defapi;
class api;
class http_request {
@ -28,7 +26,6 @@ class http_request {
//dolazni
http_request(const string _raw);
//bool validate();
void putheader(const string _key, const string _value);
void setheaders(const map<string, string> _headers);
void parse();
@ -49,10 +46,6 @@ class http_response {
void send(const string _body);
// dolaznih
void get(const string _raw);
//bool validate();
// void putheader(const string _key, const string _value);
// void putheaders(const map<string, string> _headers);
void mold(); // za slanje
void parse(); // čitaj http

@ -1,17 +1,6 @@
#include "../lib/api.hpp"
defapi::defapi(const vector<string> _methods, const vector<string> _paths, const vector<string> _keys) {
methods = _methods;
paths = _paths;
keys = _keys;
}
void defapi::necessary(const string _path, const vector<string> _keys) {
val_matrix[_path].insert( val_matrix[_path].end(), _keys.begin(), _keys.end());
}
api::api(defapi* _def, const string _method, const string _path, const map<string, string> _params, const string _body) {
def = _def;
api::api(const string _method, const string _path, const map<string, string> _params, const string _body) {
method = _method;
path = _path;
url = path;
@ -26,13 +15,9 @@ api::api(defapi* _def, const string _method, const string _path, const map<strin
body = _body;
// if (!validate()) {
// throw string("[ERROR] The API is not correct ");
// }
}
api::api(defapi* _def, const http_request _req) {
def = _def;
api::api(const http_request _req) {
method = _req.method;
//path = _path;
url = _req.url;
@ -56,27 +41,4 @@ api::api(defapi* _def, const http_request _req) {
body = _req.body;
// if (!validate()) {
// cout << "Nije ispravan API" << endl;
// }
}
// bool api::validate() {
// bool isValidate = true;
// cout << "Validiramo " << endl;
// // api validacija ključeva
// for (uint i=0; i<def->val_matrix[path].size(); i++) {
// cout << def->val_matrix[path][i] << " " << params[def->val_matrix[path][i]] << endl;
// if (params[def->val_matrix[path][i]].empty()) {
// cout << "Ptazan " << endl;
// isValidate = false;
// break;
// }
// }
// return isValidate;
// }
}

@ -6,19 +6,11 @@ using namespace std;
int main() {
defapi myApi({"GET"}, {"add", "delete", "update"}, {"id", "key", "value"});
myApi.necessary("add", {"id", "key", "value"});
myApi.necessary("delete", {"id"});
myApi.necessary("update", {"id"});
myApi.necessary("update", {"value"});
try {
api uf(&myApi, "GET", "delete", {make_pair("id", "4")}, "bay");
api uf("GET", "/delete", {make_pair("id", "4")}, "bay");
} catch (string err) {
cout << err << endl;
}
// http myHttp(&myApi, "GET /fghfhf HTTP/1.1\r\nBaba");
// //http myHttp(&myApi, "GET /hello/?id=4&post=99 HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\nHost: www.tutorialspoint.com\r\nAccept-Language: en-us\r\nAccept-Encoding: gzip, deflate\r\nConnection: Keep-Alive\r\n\r\nHELLO WORLD\r\n");
@ -32,10 +24,10 @@ int main() {
// cout << mojzahtjev.method << " " << mojzahtjev.url << endl << mojzahtjev.raw;
// http_request myres(&uf);
// myres.putheader("Content-type", "text/plain");
http_request myres(&uf);
myres.putheader("Content-type", "text/plain");
http_request myres("GET /delete?id=4 HTTP/1.1\r\nContent-type: text/plain\r\n\n\rBAY");
// http_request myres();
// myres.get("HTTP/1.1 200 OK\r\n\r\nnotauth");
// myres.get("HTTP/1.1 200 OK");
@ -43,11 +35,15 @@ int main() {
// api uf(&myApi, myres);
// cout << uf.method << " " << uf.path << " " << uf.body << endl;// << myres.raw;
// cout << myres.method << " " << myres.url << " " << myres.body << endl;// << myres.raw;
cout << uf.method << " " << uf.path << " " << uf.body << endl;
cout << myres.method << " " << myres.url << " " << myres.body << endl << myres.raw << endl;
// for(auto i : uf.params)
// cout << i.first << " " << i.second << endl;
} catch (string err) {
cout << err << endl;
}
}

Binary file not shown.
Loading…
Cancel
Save