diff --git a/include/bank_f.h b/include/bank_f.h index fc3703f..5a87bae 100644 --- a/include/bank_f.h +++ b/include/bank_f.h @@ -6,12 +6,12 @@ using namespace drogon; #define req_args const HttpRequestPtr &req, std::function &&callback -class BankF : public HttpController +class api : public HttpController { Bank &bank; public: - BankF(Bank *b); + api(Bank *b); void Help(req_args) const; void Ping(req_args) const; void Close(req_args) const; @@ -27,31 +27,31 @@ public: void SetBal(req_args, const std::string &name, uint32_t amount) const; void AdminVerifyPass(req_args); void GetLog(req_args, const std::string &name); - + METHOD_LIST_BEGIN //Usage - METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); - METHOD_ADD(BankF::GetLog, "/{name}/log", Get, Options); - METHOD_ADD(BankF::SendFunds, "/{name}/send/{to}?amount={amount}", Post, Options); - METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Get, Options); + METHOD_ADD(api::GetBal, "/{name}/bal", Get, Options); + METHOD_ADD(api::GetLog, "/{name}/log", Get, Options); + METHOD_ADD(api::SendFunds, "/{name}/send/{to}?amount={amount}", Post, Options); + METHOD_ADD(api::VerifyPassword, "/{name}/pass/verify", Get, Options); //Meta Usage - METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); - METHOD_ADD(BankF::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options); + METHOD_ADD(api::ChangePassword, "/{name}/pass/change", Patch, Options); + METHOD_ADD(api::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options); //System Usage - METHOD_ADD(BankF::Help, "/help", Get, Options); - METHOD_ADD(BankF::Ping, "/ping", Get, Options); - METHOD_ADD(BankF::Close, "/admin/close", Post, Options); - METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); - METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Get, Options); + METHOD_ADD(api::Help, "/help", Get, Options); + METHOD_ADD(api::Ping, "/ping", Get, Options); + METHOD_ADD(api::Close, "/admin/close", Post, Options); + METHOD_ADD(api::Contains, "/contains/{name}", Get, Options); + METHOD_ADD(api::AdminVerifyPass, "/admin/verify", Get, Options); //User Managment - METHOD_ADD(BankF::AddUser, "/user/{name}", Post, Options); - METHOD_ADD(BankF::AdminAddUser, "/admin/user/{name}?init_bal={init_bal}", Post, Options); - METHOD_ADD(BankF::DelUser, "/user/{name}", Delete, Options); - METHOD_ADD(BankF::AdminDelUser, "/admin/user/{name}", Delete, Options); + METHOD_ADD(api::AddUser, "/user/{name}", Post, Options); + METHOD_ADD(api::AdminAddUser, "/admin/user/{name}?init_bal={init_bal}", Post, Options); + METHOD_ADD(api::DelUser, "/user/{name}", Delete, Options); + METHOD_ADD(api::AdminDelUser, "/admin/user/{name}", Delete, Options); METHOD_LIST_END }; \ No newline at end of file diff --git a/src/bank_f.cpp b/src/bank_f.cpp index 08f30fd..6594903 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -24,16 +24,16 @@ constexpr Json::Value JsonCast(T &&val) } } -BankF::BankF(Bank *b) : bank(*b) {} +api::api(Bank *b) : bank(*b) {} -void BankF::Help(req_args) const +void api::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); - resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal BankF/{name}/bal GET false returns the balance of a given user {name}
GetLog BankF/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds BankF/{name}/send/{to}?amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword BankF/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword BankF/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal BankF/admin/{name}/bal?amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help BankF/help GET false the page you’re looking at right now!
Ping BankF/ping GET false for pinging the server to see if its online
Close BankF/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains BankF/contains/{name} GET false returns 1 if the supplied user {name} exists
AdminVerifyPass BankF/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser BankF/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser BankF/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser BankF/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser BankF/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); + resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal api/{name}/bal GET false returns the balance of a given user {name}
GetLog api/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds api/{name}/send/{to}?amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword api/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword api/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal api/admin/{name}/bal?amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help api/help GET false the page you’re looking at right now!
Ping api/ping GET false for pinging the server to see if its online
Close api/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains api/contains/{name} GET false returns 1 if the supplied user {name} exists
AdminVerifyPass api/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser api/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser api/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser api/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser api/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); } -void BankF::Close(req_args) const +void api::Close(req_args) const { bool res; if (PASS_HEADER == bank.admin_pass) @@ -49,58 +49,58 @@ void BankF::Close(req_args) const } JSON(res); } -void BankF::Ping(req_args) const +void api::Ping(req_args) const { auto resp = HttpResponse::newHttpResponse(); resp->setBody("pong"); resp->setExpiredTime(0); callback(resp); } -void BankF::AddUser(req_args, const std::string &name) const +void api::AddUser(req_args, const std::string &name) const { JSON(bank.AddUser(std::move(name), PASS_HEADER)); } -void BankF::AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const +void api::AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const { JSON(bank.AdminAddUser(PASS_HEADER, std::move(name), init_bal, std::string(req->getBody()))); } -void BankF::DelUser(req_args, const std::string &name) const +void api::DelUser(req_args, const std::string &name) const { JSON(bank.DelUser(name, PASS_HEADER)); } -void BankF::AdminDelUser(req_args, const std::string &name) const +void api::AdminDelUser(req_args, const std::string &name) const { JSON(bank.AdminDelUser(name, PASS_HEADER)); } -void BankF::SendFunds(req_args, const std::string name, const std::string to, uint32_t amount) const +void api::SendFunds(req_args, const std::string name, const std::string to, uint32_t amount) const { JSON(bank.SendFunds(name, to, amount, PASS_HEADER)); } -void BankF::ChangePassword(req_args, const std::string &name) const +void api::ChangePassword(req_args, const std::string &name) const { JSON(bank.ChangePassword(name, PASS_HEADER, std::string(req->getBody()))); } -void BankF::Contains(req_args, const std::string &name) const +void api::Contains(req_args, const std::string &name) const { JSON(bank.Contains(name)); } -void BankF::GetBal(req_args, const std::string &name) const +void api::GetBal(req_args, const std::string &name) const { JSON(bank.GetBal(name)); } -void BankF::VerifyPassword(req_args, const std::string &name) const +void api::VerifyPassword(req_args, const std::string &name) const { JSON(bank.VerifyPassword(name, PASS_HEADER)); } -void BankF::SetBal(req_args, const std::string &name, uint32_t amount) const +void api::SetBal(req_args, const std::string &name, uint32_t amount) const { JSON(bank.SetBal(name, PASS_HEADER, amount)); } -void BankF::AdminVerifyPass(req_args) +void api::AdminVerifyPass(req_args) { JSON(bank.AdminVerifyPass(PASS_HEADER)); } -void BankF::GetLog(req_args, const std::string &name) +void api::GetLog(req_args, const std::string &name) { if constexpr (max_log_size > 0) {