From 1f0a3dc28dbfd64eceefaac63f04d96a816bd1e1 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 9 Jul 2021 19:51:26 -0700 Subject: [PATCH] :racehorse::hammer: modified endpoints to better adhere to JSON API conventions --- docs/connected_services/how_to/endpoints.md | 12 ++++++------ include/bank.h | 2 +- include/bank_resp.h | 6 +++--- src/bank.cpp | 13 +++++-------- src/bank_resp.cpp | 13 +++++++++++-- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/docs/connected_services/how_to/endpoints.md b/docs/connected_services/how_to/endpoints.md index 1fe1187..31ec54a 100644 --- a/docs/connected_services/how_to/endpoints.md +++ b/docs/connected_services/how_to/endpoints.md @@ -14,12 +14,12 @@ :heavy_multiplication_x: ### Usage endpoints -| name | purpose | json input | path | HTTP Method | return type | return value | Jresp | Jreq | A | U | -| :------------- | ------------------------------------------------------------------------------ | -------------------------------- | ------------------------------- | :---------: | :------------: | :--------------------------------------------: | :----------------: | :----------------------: | :----------------------: | :----------------------: | -| GetBal | retrieving the balance of a given user, `{name}` | `N/A` | api/v1/user/balance?name={name} | `GET` | uint32 | the user's balance | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_multiplication_x: | -| GetLog | retrieves the logs of a given user, length varies by server configuration | `N/A` | api/v1/user/log | `GET` | array of jsons | [{"to":string, "amount":uint32, "time":int64}] | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | -| SendFunds | sends funds from the authenticated user to the user `{name}` given in the json | {"name":string, "amount":uint32} | api/v1/user/transfer | `POST` | bool | true | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: | -| VerifyPassword | verifies the credentials, used for connected services for ease of use | `N/A` | api/v1/user/verify_password | `POST` | bool | true | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | +| name | purpose | json input | path | HTTP Method | return type | return value | Jresp | Jreq | A | U | +| :------------- | ------------------------------------------------------------------------------ | -------------------------------- | ------------------------------- | :---------: | :--------------: | :--------------------------------------------: | :----------------: | :----------------------: | :----------------------: | :----------------------: | +| GetBal | retrieving the balance of a given user, `{name}` | `N/A` | api/v1/user/balance?name={name} | `GET` | uint32 | the user's balance | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_multiplication_x: | +| GetLog | retrieves the logs of a given user, length varies by server configuration | `N/A` | api/v1/user/log | `GET` | array of objects | [{"to":string, "amount":uint32, "time":int64}] | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | +| SendFunds | sends funds from the authenticated user to the user `{name}` given in the json | {"name":string, "amount":uint32} | api/v1/user/transfer | `POST` | uint32 | the user's balance after the transaction | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: | +| VerifyPassword | verifies the credentials, used for connected services for ease of use | `N/A` | api/v1/user/verify_password | `POST` | `N/A` | null | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | ### Usage enpoints errors | name | 400 | 401 | 404 | 405 | 406 | diff --git a/include/bank.h b/include/bank.h index 5b45923..242e9b6 100644 --- a/include/bank.h +++ b/include/bank.h @@ -60,7 +60,7 @@ public: bool Contains(const std::string &name) const noexcept; bool AdminVerifyAccount(const std::string &name) noexcept; - BankResponse AddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept; + BankResponse AddUser(const std::string &name, uint32_t init_bal, std::string &&init_pass) noexcept; BankResponse DelUser(const std::string &name) noexcept; void Save(); diff --git a/include/bank_resp.h b/include/bank_resp.h index 341f33c..4983cbd 100644 --- a/include/bank_resp.h +++ b/include/bank_resp.h @@ -11,9 +11,9 @@ struct BankResponse drogon::HttpStatusCode first = drogon::k200OK; std::string second; - BankResponse() noexcept ; - BankResponse(drogon::HttpStatusCode code, std::string &&str) noexcept ; - BankResponse(drogon::HttpStatusCode code, const std::string &str) noexcept ; + BankResponse() noexcept; + BankResponse(drogon::HttpStatusCode code, std::string &&str) noexcept; + BankResponse(drogon::HttpStatusCode code, const std::string &str) noexcept; }; template <> diff --git a/src/bank.cpp b/src/bank.cpp index 39c8e61..c2b6949 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -104,10 +104,8 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam std::shared_lock lock{save_lock}; //about 10% of this function's cost #if MAX_LOG_SIZE > 0 static thread_local Transaction temp(a_name, b_name, amount); - if (!users.modify_if(a_name, [&state, amount](User &a) { -#else - if (!users.modify_if(a_name, [&state, amount](User &a) { #endif + if (!users.modify_if(a_name, [&state, amount](User &a) { //if A can afford it if (a.balance < amount) { @@ -119,7 +117,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam #if MAX_LOG_SIZE > 0 a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost #endif - state = BankResponse(k200OK, "true"); + state = BankResponse(k200OK, std::to_string(a.balance)); } })) { @@ -211,8 +209,7 @@ bool Bank::AdminVerifyAccount(const std::string &name) noexcept { return (name == admin_account); } - -BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept +BankResponse Bank::AddUser(const std::string &name, uint32_t init_bal, std::string &&init_pass) noexcept { if (!ValidUsrname(name)) { @@ -229,7 +226,7 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string && save_flag = true; #endif #endif - return {k200OK, "true"}; + return {k204NoContent, nullptr}; } else { @@ -259,7 +256,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept save_flag = true; #endif #endif - return BankResponse(k200OK, "true"); + return BankResponse(k204NoContent, nullptr); } else { diff --git a/src/bank_resp.cpp b/src/bank_resp.cpp index 21a6bab..693fa50 100644 --- a/src/bank_resp.cpp +++ b/src/bank_resp.cpp @@ -7,8 +7,17 @@ BankResponse::BankResponse(drogon::HttpStatusCode code, const std::string &str) template <> drogon::HttpResponsePtr drogon::toResponse(BankResponse &&data) { - const auto &res = std::make_shared(data.first, CT_APPLICATION_JSON); - res->setBody(std::move(data.second)); + std::shared_ptr res; + if (data.first == k204NoContent) + { + res = std::make_shared(data.first, CT_APPLICATION_JSON); + res->setBody(std::move(data.second)); + } + else + { + res = std::make_shared(); + res->setStatusCode(data.first); + } const auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices(); if (!advices.empty()) {