mirror of
https://github.com/oatpp/oatpp.git
synced 2025-04-18 19:00:23 +08:00
ApiController: introduce 'BUNDLE' parameters to access request bundle data.
This commit is contained in:
parent
e3747736fc
commit
3d89dc4403
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
40
src/oatpp/codegen/api_controller/bundle_define.hpp
Normal file
40
src/oatpp/codegen/api_controller/bundle_define.hpp
Normal 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)
|
32
src/oatpp/codegen/api_controller/bundle_undef.hpp
Normal file
32
src/oatpp/codegen/api_controller/bundle_undef.hpp
Normal 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
|
@ -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) + "'.");
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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`.
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;.
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;.
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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));
|
||||
|
@ -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))
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user