Skip to content

Commit 23e3b7a

Browse files
committed
refactor: move /info input/ouput to oatpp::dto
1 parent 566e5fb commit 23e3b7a

File tree

4 files changed

+120
-7
lines changed

4 files changed

+120
-7
lines changed

src/http/controller.hpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
#include <vector>
2727
#include <iostream>
2828

29+
#include <boost/lexical_cast.hpp>
30+
2931
#include "oatpp/web/server/api/ApiController.hpp"
3032
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
3133
#include "oatpp/core/macro/codegen.hpp"
3234
#include "oatpp/core/macro/component.hpp"
3335

36+
#include "apidata.h"
3437
#include "oatppjsonapi.h"
38+
#include "http/dto/info.hpp"
3539

3640
#include OATPP_CODEGEN_BEGIN(ApiController)
3741

@@ -61,11 +65,31 @@ class DedeController : public oatpp::web::server::api::ApiController
6165
}
6266
ENDPOINT("GET", "info", get_info, QUERIES(QueryParams, queryParams))
6367
{
64-
// TODO(sileht): why do serialize the query string to char*
65-
// to later get again a APIData...
66-
std::string jsonstr = _oja->uri_query_to_json(queryParams);
67-
auto janswer = _oja->info(jsonstr);
68-
return _oja->jdoc_to_response(janswer);
68+
auto info_resp = InfoResponse::createShared();
69+
info_resp->head = InfoHead::createShared();
70+
info_resp->head->services = {};
71+
72+
auto qs_status = queryParams.get("status");
73+
bool status = false;
74+
if (qs_status)
75+
status = boost::lexical_cast<bool>(qs_status->std_str());
76+
77+
auto hit = _oja->_mlservices.begin();
78+
while (hit != _oja->_mlservices.end())
79+
{
80+
// TODO(sileht): update visitor_info to return directly a Service()
81+
JDoc jd;
82+
jd.SetObject();
83+
mapbox::util::apply_visitor(dd::visitor_info(status), (*hit).second)
84+
.toJDoc(jd);
85+
auto json_str = _oja->jrender(jd);
86+
auto service_info
87+
= getDefaultObjectMapper()->readFromString<oatpp::Object<Service>>(
88+
json_str.c_str());
89+
info_resp->head->services->emplace_back(service_info);
90+
++hit;
91+
}
92+
return createDtoResponse(Status::CODE_200, info_resp);
6993
}
7094

7195
ENDPOINT_INFO(get_service)

src/http/dto/info.hpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* DeepDetect
3+
* Copyright (c) 2020 Jolibrain SASU
4+
* Author: Mehdi Abaakouk <mehdi.abaakouk@jolibrain.com>
5+
*
6+
* This file is part of deepdetect.
7+
*
8+
* deepdetect is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* deepdetect is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef HTTP_DTO_INFO_H
23+
#define HTTP_DTO_INFO_H
24+
25+
#include "dd_config.h"
26+
#include "oatpp/core/Types.hpp"
27+
#include "oatpp/core/macro/codegen.hpp"
28+
29+
#include OATPP_CODEGEN_BEGIN(DTO) ///< Begin DTO codegen section
30+
31+
class Service : public oatpp::DTO
32+
{
33+
DTO_INIT(Service, DTO /* extends */)
34+
35+
DTO_FIELD(String, name);
36+
DTO_FIELD(String, description);
37+
DTO_FIELD(String, mllib);
38+
DTO_FIELD(String, mltype);
39+
DTO_FIELD(Boolean, predict) = false;
40+
DTO_FIELD(Boolean, training) = false;
41+
};
42+
43+
class InfoHead : public oatpp::DTO
44+
{
45+
DTO_INIT(InfoHead, DTO /* extends */)
46+
DTO_FIELD(String, method) = "/info";
47+
48+
// Why this is not in body ?
49+
DTO_FIELD(String, build_type, "build-type") = BUILD_TYPE;
50+
DTO_FIELD(String, version) = GIT_VERSION;
51+
DTO_FIELD(String, branch) = GIT_BRANCH;
52+
DTO_FIELD(String, commit) = GIT_COMMIT_HASH;
53+
DTO_FIELD(String, compile_flags) = COMPLIE_FLAGS;
54+
DTO_FIELD(String, deps_version) = DEPS_VERSION;
55+
DTO_FIELD(List<Object<Service>>, services);
56+
};
57+
58+
class InfoBody : public oatpp::DTO
59+
{
60+
DTO_INIT(InfoBody, DTO /* extends */)
61+
};
62+
63+
class Status : public oatpp::DTO
64+
{
65+
DTO_INIT(Status, DTO /* extends */)
66+
DTO_FIELD(Int32, code);
67+
DTO_FIELD(String, msg);
68+
DTO_FIELD(Int32, dd_code);
69+
DTO_FIELD(String, dd_msg);
70+
};
71+
72+
class InfoResponse : public oatpp::DTO
73+
{
74+
DTO_INIT(InfoResponse, DTO /* extends */)
75+
DTO_FIELD(String, dd_msg);
76+
DTO_FIELD(Object<Status>, status);
77+
DTO_FIELD(Object<InfoHead>, head);
78+
DTO_FIELD(Object<InfoBody>, body);
79+
};
80+
81+
#include OATPP_CODEGEN_END(DTO) ///< End DTO codegen section
82+
83+
#endif // HTTP_DTO_INFO_H

src/oatppjsonapi.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ namespace dd
264264

265265
auto router = components.httpRouter.getObject();
266266

267-
auto dedeController = DedeController::createShared(this);
267+
std::shared_ptr<oatpp::data::mapping::ObjectMapper> defaultObjectMapper
268+
= oatpp::parser::json::mapping::ObjectMapper::createShared();
269+
auto dedeController
270+
= DedeController::createShared(this, defaultObjectMapper);
268271
dedeController->addEndpointsToRouter(router);
269272

270273
auto docEndpoints = oatpp::swagger::Controller::Endpoints::createShared();

tests/ut-oatpp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ class DedeControllerTest : public oatpp::test::UnitTest
140140
dd::OatppJsonAPI oja;
141141
TestComponent component;
142142
oatpp::test::web::ClientServerTestRunner runner;
143-
runner.addController(std::make_shared<DedeController>(&oja, nullptr));
143+
std::shared_ptr<oatpp::data::mapping::ObjectMapper> defaultObjectMapper
144+
= oatpp::parser::json::mapping::ObjectMapper::createShared();
145+
runner.addController(
146+
std::make_shared<DedeController>(&oja, defaultObjectMapper));
144147
runner.run(
145148
[this, &runner] {
146149
OATPP_COMPONENT(

0 commit comments

Comments
 (0)