using std::optional for 204 responses

This commit is contained in:
EntireTwix 2021-07-10 11:49:09 -07:00
parent cfdb2fe829
commit f87ec947b6
4 changed files with 18 additions and 30 deletions

View file

@ -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

View file

@ -1,20 +1,12 @@
#pragma once
#include <string>
#include <optional>
#include <drogon/HttpTypes.h>
#include <drogon/HttpResponse.h>
#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<drogon::HttpStatusCode, std::optional<std::string>>;
template <>
drogon::HttpResponsePtr drogon::toResponse(BankResponse &&data);

View file

@ -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()

View file

@ -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<HttpResponseImpl> res;
if (data.first == k204NoContent)
if (data.second)
{
res = std::make_shared<HttpResponseImpl>();
res->setStatusCode(data.first);
res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
res->setBody(std::move(*data.second));
}
else
{
res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
res->setBody(std::move(data.second));
res = std::make_shared<HttpResponseImpl>();
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;
}
}