Comments (4)
Hello @c-lgrant !
Thanks you for the question.
You have to create separate method for your business logic processing, and call it from your endpoint mappings.
Example:
/**
* Process request
*/
std::shared_ptr<OutgoingResponse> handleRequest(String param, String body) {
OATPP_ASSERT_HTTP(param && body, Status::CODE_404, "param and body should not be null");
return createResponse(Status::CODE_200, param + body);
}
/**
* Add endpoint mapping
*/
ENDPOINT("GET", "endpoint1/{param}", endpoint1, PATH(String, param), BODY_STRING(String, body)) {
return handleRequest(param, body);
}
/**
* Add endpoint mapping
*/
ENDPOINT("GET", "endpoint2/{param}", endpoint2, PATH(String, param), BODY_STRING(String, body)) {
return handleRequest(param, body);
}
Best Regards,
Leonid
from oatpp.
Hello,
So there is no way to made two end points to one function?
In the example above, if the logic was different for each endpoint how could the handleRequest function decide which action to take.
Can you confirm if there is a better way to get the url than performing.
this->m_endpointInfo.find(__FUNCTION__).operator*().second.get()->path
Thanks
Conor
from oatpp.
Hey @c-lgrant
It would be helpful, if you could describe what you are trying to achieve in a bit more details.
From what I can guess, it can be achieved in many different ways.
From the code snippet that you've posted:
this->m_endpointInfo.find(__FUNCTION__).operator*().second.get()->path
I would not recommend to do it this way, as it will be hard to maintain such code in case of endpoint name changed or path of the endpoint changed.
As a simplest example I can propose next:
- Variant one:
static const int ACTION_START = 0;
static const int ACTION_STOP = 1;
/**
* Process request
*/
std::shared_ptr<OutgoingResponse> handleRequest(String mapGuid, int action) {
OATPP_ASSERT_HTTP(mapGuid, Status::CODE_404, "mapGuid should not be null");
switch(action) {
case ACTION_START:
// TODO maps.start;
break;
case ACTION_STOP:
// TODO maps.stop;
break;
default: throw std::runtime_error("unknown action");
}
return createResponse(Status::CODE_200, "OK");
}
/**
* Add endpoint mapping
*/
ENDPOINT("GET", "/app/maps/{mapGuid}/start", start, PATH(String, mapGuid)) {
return handleRequest(mapGuid, ACTION_START);
}
/**
* Add endpoint mapping
*/
ENDPOINT("GET", "/app/maps/{mapGuid}/stop", stop, PATH(String, mapGuid)) {
return handleRequest(mapGuid, ACTION_STOP);
}
- Variant two (better way):
Move map-related common functionality to Map class.
/**
* Inject map processing component
*/
OATPP_COMPONENT(std::shared_ptr<Map>, m_map);
/**
* Add endpoint mapping
*/
ENDPOINT("GET", "/app/maps/{mapGuid}/start", start, PATH(String, mapGuid)) {
m_map->start(mapGuid);
return createResponse(Status::CODE_200, "started");
}
/**
* Add endpoint mapping
*/
ENDPOINT("GET", "/app/maps/{mapGuid}/stop", stop, PATH(String, mapGuid)) {
m_map->stop(mapGuid);
return createResponse(Status::CODE_200, "stopped");
}
Best Regards,
Leonid
from oatpp.
You may also consider to do it this way:
ENDPOINT("GET", "/app/maps/{mapGuid}/{action}", map,
PATH(String, mapGuid),
PATH(String, action)) {
OATPP_ASSERT_HTTP(action && (action == "start" || action == "stop"), Status::CODE_400, "Invalid Action");
// do things based on mapGuid and action
return createResponse(Status::CODE_200, "OK");
}
Regards,
Leonid
from oatpp.
Related Issues (20)
- Regarding oatpp::async::CoroutineWithResult HOT 9
- How to add default values to the QUERY parameters of an ENDPOINT?
- Enhance Deserializer's "Error. Unknown field"
- How can I use OATPP to handle web browser request download file. HOT 2
- Memory leak in Object mapping HOT 1
- Deserialize json when not include required field only gives error when the field is specified as null
- [Question] readBodyIntoDTO when there's a list in the body
- API question
- how to make dynamic sql use oatpp-sqlite HOT 1
- LazyStringMapTemplate::get returns Nullptr wich is not in oatpp.io Documentation
- Please add support for CMake 3.16
- Cannot test if required integer query parameter is empty
- [Question] Is it possible during compilation to write out the generated oas-3.0.0.json file?
- May memory leak
- How should I make my process send requests to the target when using OATPP HOT 4
- Stack corruption issue in ConnectionProvider.cpp
- Can not build project successfully
- Is it possible make oatpp::network::tcp::client::ConnectionProvider support bind client IP?
- specific async pattern implementation query HOT 4
- io_uring support request HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oatpp.