diff --git a/docs/connected_services/how_to/endpoints.md b/docs/connected_services/how_to/endpoints.md index 875a77a..3450b45 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 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` | `N/A` | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | +| name | purpose | json input | path | HTTP Method | correct status | 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` | 200 | 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` | 200 | 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` | 200 | 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` | 204 | `N/A` | `N/A` | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | ### Usage enpoints errors | name | 400 | 401 | 404 | 405 | 406 | @@ -29,7 +29,7 @@ | SendFunds | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | VerifyPassword | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_check_mark: | -all error responses have JSON along with them to describe +all error responses have JSON string along with them to describe ### Usage endpoints support `v` denoting the API version diff --git a/include/bank_resp.h b/include/bank_resp.h index 4983cbd..f88d9a6 100644 --- a/include/bank_resp.h +++ b/include/bank_resp.h @@ -1,20 +1,12 @@ #pragma once #include +#include #include - #include #include <../src/HttpResponseImpl.h> #include <../src/HttpAppFrameworkImpl.h> -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; -}; +using BankResponse = std::pair>; template <> drogon::HttpResponsePtr drogon::toResponse(BankResponse &&data); \ No newline at end of file diff --git a/src/bank.cpp b/src/bank.cpp index c919603..7f7498d 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -226,7 +226,7 @@ BankResponse Bank::AddUser(const std::string &name, uint32_t init_bal, std::stri save_flag = true; #endif #endif - return {k204NoContent, ""}; + return {k204NoContent, std::nullopt}; } else { @@ -256,11 +256,11 @@ BankResponse Bank::DelUser(const std::string &name) noexcept save_flag = true; #endif #endif - return BankResponse(k204NoContent, ""); + return {k204NoContent, std::nullopt}; } else { - return BankResponse(k404NotFound, "\"User not found\""); + return {k404NotFound, "\"User not found\""}; } } void Bank::Save() diff --git a/src/bank_resp.cpp b/src/bank_resp.cpp index 01138ea..c38ae57 100644 --- a/src/bank_resp.cpp +++ b/src/bank_resp.cpp @@ -1,22 +1,18 @@ #include "bank_resp.h" -BankResponse::BankResponse() noexcept = default; -BankResponse::BankResponse(drogon::HttpStatusCode code, std::string &&str) noexcept : first(code), second(str) {} -BankResponse::BankResponse(drogon::HttpStatusCode code, const std::string &str) noexcept : first(code), second(str) {} - template <> drogon::HttpResponsePtr drogon::toResponse(BankResponse &&data) { std::shared_ptr res; - if (data.first == k204NoContent) + if (data.second) { - res = std::make_shared(); - res->setStatusCode(data.first); + res = std::make_shared(data.first, CT_APPLICATION_JSON); + res->setBody(std::move(*data.second)); } else { - res = std::make_shared(data.first, CT_APPLICATION_JSON); - res->setBody(std::move(data.second)); + res = std::make_shared(); + res->setStatusCode(data.first); } const auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices(); if (!advices.empty()) @@ -27,4 +23,4 @@ drogon::HttpResponsePtr drogon::toResponse(BankResponse &&data) } } return res; -} \ No newline at end of file +}