ApiController: introduce 'BUNDLE' parameters to access request bundle data.

This commit is contained in:
lganzzzo 2021-10-14 23:18:16 +03:00
parent e3747736fc
commit 3d89dc4403
17 changed files with 246 additions and 1 deletions

View File

@ -11,6 +11,8 @@ add_library(oatpp
oatpp/codegen/api_controller/base_undef.hpp
oatpp/codegen/api_controller/auth_define.hpp
oatpp/codegen/api_controller/auth_undef.hpp
oatpp/codegen/api_controller/bundle_define.hpp
oatpp/codegen/api_controller/bundle_undef.hpp
oatpp/codegen/api_controller/cors_define.hpp
oatpp/codegen/api_controller/cors_undef.hpp
oatpp/codegen/ApiController_define.hpp

View File

@ -47,4 +47,5 @@
#include "./api_controller/base_define.hpp"
#include "./api_controller/auth_define.hpp"
#include "./api_controller/bundle_define.hpp"
#include "./api_controller/cors_define.hpp"

View File

@ -44,4 +44,5 @@
#include "./api_controller/base_undef.hpp"
#include "./api_controller/auth_undef.hpp"
#include "./api_controller/bundle_undef.hpp"
#include "./api_controller/cors_undef.hpp"

View File

@ -0,0 +1,40 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
* Benedikt-Alexander Mokroß <bam@icognize.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#define BUNDLE(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_BUNDLE, OATPP_MACRO_API_CONTROLLER_BUNDLE_INFO, TYPE, (__VA_ARGS__))
// BUNDLE MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_BUNDLE_1(TYPE, NAME) \
TYPE NAME = __request->getBundleData<TYPE>(#NAME);
#define OATPP_MACRO_API_CONTROLLER_BUNDLE_2(TYPE, NAME, QUALIFIER) \
TYPE NAME = __request->getBundleData<TYPE>(QUALIFIER);
#define OATPP_MACRO_API_CONTROLLER_BUNDLE(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_BUNDLE_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
#define OATPP_MACRO_API_CONTROLLER_BUNDLE_INFO(TYPE, PARAM_LIST)

View File

@ -0,0 +1,32 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
* Benedikt-Alexander Mokroß <bam@icognize.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#undef BUNDLE
#undef OATPP_MACRO_API_CONTROLLER_BUNDLE_1
#undef OATPP_MACRO_API_CONTROLLER_BUNDLE_2
#undef OATPP_MACRO_API_CONTROLLER_BUNDLE
#undef OATPP_MACRO_API_CONTROLLER_BUNDLE_INFO

View File

@ -45,7 +45,8 @@ public:
}
if(it->second.getValueType() != WrapperType::Class::getType()) {
throw std::runtime_error("[oatpp::data::Bundle::get()]: Error. Type mismatch. Stored '" +
throw std::runtime_error("[oatpp::data::Bundle::get()]: Error. Type mismatch for key '" + *key +
"'. Stored '" +
std::string(it->second.getValueType()->classId.name) +
"' vs requested '" + std::string(WrapperType::Class::getType()->classId.name) + "'.");
}

View File

@ -130,6 +130,14 @@ oatpp::String Request::getPathTail() const {
return m_pathVariables.getTail();
}
void Request::putBundleData(const oatpp::String& key, const oatpp::Void& polymorph) {
m_bundle.put(key, polymorph);
}
const data::Bundle& Request::getBundle() const {
return m_bundle;
}
void Request::transferBody(data::stream::WriteCallback* writeCallback) const {
m_bodyDecoder->decode(m_headers, m_bodyStream.get(), writeCallback);
}

View File

@ -29,6 +29,7 @@
#include "oatpp/web/protocol/http/incoming/BodyDecoder.hpp"
#include "oatpp/web/url/mapping/Pattern.hpp"
#include "oatpp/network/Url.hpp"
#include "oatpp/core/data/Bundle.hpp"
namespace oatpp { namespace web { namespace protocol { namespace http { namespace incoming {
@ -56,6 +57,8 @@ private:
mutable bool m_queryParamsParsed; // used for lazy parsing of QueryParams
mutable http::QueryParams m_queryParams;
data::Bundle m_bundle;
public:
Request(const std::shared_ptr<oatpp::data::stream::IOStream>& connection,
@ -208,6 +211,30 @@ public:
*/
oatpp::String getPathTail() const;
/**
* Put data to bundle.
* @param key
* @param polymorph
*/
void putBundleData(const oatpp::String& key, const oatpp::Void& polymorph);
/**
* Get data from bundle by key.
* @tparam WrapperType
* @param key
* @return
*/
template<typename WrapperType>
WrapperType getBundleData(const oatpp::String& key) const {
return m_bundle.template get<WrapperType>(key);
}
/**
* Get bundle object.
* @return
*/
const data::Bundle& getBundle() const;
/**
* Transfer body. <br>
* Read body chunk by chunk and pass chunks to the `writeCallback`.

View File

@ -87,6 +87,14 @@ oatpp::String Response::getHeader(const oatpp::data::share::StringKeyLabelCI& he
return m_headers.get(headerName);
}
void Response::putBundleData(const oatpp::String& key, const oatpp::Void& polymorph) {
m_bundle.put(key, polymorph);
}
const data::Bundle& Response::getBundle() const {
return m_bundle;
}
std::shared_ptr<oatpp::data::stream::InputStream> Response::getBodyStream() const {
return m_bodyStream;
}

View File

@ -27,6 +27,7 @@
#include "oatpp/web/protocol/http/Http.hpp"
#include "oatpp/web/protocol/http/incoming/BodyDecoder.hpp"
#include "oatpp/core/data/Bundle.hpp"
namespace oatpp { namespace web { namespace protocol { namespace http { namespace incoming {
@ -51,6 +52,8 @@ private:
std::shared_ptr<const http::incoming::BodyDecoder> m_bodyDecoder;
std::shared_ptr<oatpp::data::stream::IOStream> m_connection;
data::Bundle m_bundle;
public:
/**
@ -154,6 +157,30 @@ public:
*/
oatpp::String getHeader(const oatpp::data::share::StringKeyLabelCI& headerName) const;
/**
* Put data to bundle.
* @param key
* @param polymorph
*/
void putBundleData(const oatpp::String& key, const oatpp::Void& polymorph);
/**
* Get data from bundle by key.
* @tparam WrapperType
* @param key
* @return
*/
template<typename WrapperType>
WrapperType getBundleData(const oatpp::String& key) const {
return m_bundle.template get<WrapperType>(key);
}
/**
* Get bundle object.
* @return
*/
const data::Bundle& getBundle() const;
/**
* Get raw body stream.
* @return - raw body stream as &id:oatpp::data::stream::InputStream;.

View File

@ -89,6 +89,14 @@ oatpp::String Request::getHeader(const oatpp::data::share::StringKeyLabelCI& hea
return m_headers.get(headerName);
}
void Request::putBundleData(const oatpp::String& key, const oatpp::Void& polymorph) {
m_bundle.put(key, polymorph);
}
const data::Bundle& Request::getBundle() const {
return m_bundle;
}
std::shared_ptr<Body> Request::getBody() {
return m_body;
}

View File

@ -27,6 +27,7 @@
#include "oatpp/web/protocol/http/outgoing/Body.hpp"
#include "oatpp/web/protocol/http/Http.hpp"
#include "oatpp/core/data/Bundle.hpp"
namespace oatpp { namespace web { namespace protocol { namespace http { namespace outgoing {
@ -47,6 +48,7 @@ private:
oatpp::data::share::StringKeyLabel m_path;
Headers m_headers;
std::shared_ptr<Body> m_body;
data::Bundle m_bundle;
public:
/**
@ -147,6 +149,30 @@ public:
*/
oatpp::String getHeader(const oatpp::data::share::StringKeyLabelCI& headerName) const;
/**
* Put data to bundle.
* @param key
* @param polymorph
*/
void putBundleData(const oatpp::String& key, const oatpp::Void& polymorph);
/**
* Get data from bundle by key.
* @tparam WrapperType
* @param key
* @return
*/
template<typename WrapperType>
WrapperType getBundleData(const oatpp::String& key) const {
return m_bundle.template get<WrapperType>(key);
}
/**
* Get bundle object.
* @return
*/
const data::Bundle& getBundle() const;
/**
* Get http body.
* @return - &id:oatpp::web::protocol::http::outgoing::Body;.

View File

@ -81,6 +81,14 @@ oatpp::String Response::getHeader(const oatpp::data::share::StringKeyLabelCI& he
return m_headers.get(headerName);
}
void Response::putBundleData(const oatpp::String& key, const oatpp::Void& polymorph) {
m_bundle.put(key, polymorph);
}
const data::Bundle& Response::getBundle() const {
return m_bundle;
}
void Response::setConnectionUpgradeHandler(const std::shared_ptr<oatpp::network::ConnectionHandler>& handler) {
m_connectionUpgradeHandler = handler;
}

View File

@ -34,6 +34,8 @@
#include "oatpp/core/async/Coroutine.hpp"
#include "oatpp/core/data/stream/BufferStream.hpp"
#include "oatpp/core/data/Bundle.hpp"
namespace oatpp { namespace web { namespace protocol { namespace http { namespace outgoing {
/**
@ -60,6 +62,7 @@ private:
std::shared_ptr<Body> m_body;
std::shared_ptr<ConnectionHandler> m_connectionUpgradeHandler;
std::shared_ptr<const ConnectionHandler::ParameterMap> m_connectionUpgradeParameters;
data::Bundle m_bundle;
public:
/**
* Constructor.
@ -148,6 +151,30 @@ public:
*/
oatpp::String getHeader(const oatpp::data::share::StringKeyLabelCI& headerName) const;
/**
* Put data to bundle.
* @param key
* @param polymorph
*/
void putBundleData(const oatpp::String& key, const oatpp::Void& polymorph);
/**
* Get data from bundle by key.
* @tparam WrapperType
* @param key
* @return
*/
template<typename WrapperType>
WrapperType getBundleData(const oatpp::String& key) const {
return m_bundle.template get<WrapperType>(key);
}
/**
* Get bundle object.
* @return
*/
const data::Bundle& getBundle() const;
/**
* Set connection upgreade header. <br>
* Use it together with corresponding headers being set when Response is created as: <br>

View File

@ -495,8 +495,20 @@ void FullTest::onRun() {
OATPP_LOGV("i", "%d, tick=%d", i + 1, ticks);
}
{ // test bundle
auto response = client->getBundle(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto dto = response->readBodyToDto<oatpp::Object<app::TestDto>>(objectMapper.get());
OATPP_ASSERT(dto);
OATPP_ASSERT(dto->testValue == "str-param");
OATPP_ASSERT(dto->testValueInt == 32000);
}
}
std::this_thread::sleep_for(std::chrono::minutes(10));
}, std::chrono::minutes(10));
std::this_thread::sleep_for(std::chrono::seconds(1));

View File

@ -86,6 +86,8 @@ public:
}
API_CALL("GET", "default_headers/{param}", getDefaultHeaders2, PATH(String, param))
API_CALL("GET", "bundle", getBundle)
API_CALL_ASYNC("GET", "/", getRootAsync)
API_CALL_ASYNC("GET", "/", getRootAsyncWithCKA, HEADER(String, connection, "Connection"))
API_CALL_ASYNC("GET", "params/{param}", getWithParamsAsync, PATH(String, param))

View File

@ -385,6 +385,21 @@ public:
}
return createResponse(Status::CODE_400, "");
}
ENDPOINT_INTERCEPTOR(getBundle, middleware) {
request->putBundleData("str_param", oatpp::String("str-param"));
request->putBundleData("int_param", oatpp::Int32(32000));
return (this->*intercepted)(request);
}
ENDPOINT("GET", "bundle", getBundle,
BUNDLE(String, str_param),
BUNDLE(Int32, a, "int_param"))
{
auto dto = TestDto::createShared();
dto->testValue = str_param;
dto->testValueInt = a;
return createDtoResponse(Status::CODE_200, dto);
}
};