提交 149571e8 编写于 作者: A Aaron Xiao 提交者: Dong Li

Common: Move json_util and http_client to common util.

上级 8be428fa
......@@ -186,4 +186,44 @@ cc_test(
],
)
cc_library(
name = "json_util",
srcs = [
"json_util.cc",
],
hdrs = [
"json_util.h",
],
deps = [
"//modules/common:log",
"//third_party/json",
"@com_google_protobuf//:protobuf",
],
)
cc_test(
name = "json_util_test",
size = "small",
srcs = [
"json_util_test.cc",
],
deps = [
":json_util",
"//modules/common/proto:error_code_proto",
"@gtest//:main",
],
)
cc_library(
name = "http_client",
srcs = ["http_client.cc"],
hdrs = ["http_client.h"],
deps = [
"//modules/common:log",
"//modules/common/status",
"//third_party/json",
"@curlpp//:curlpp",
],
)
cpplint()
......@@ -14,7 +14,7 @@
* limitations under the License.
*****************************************************************************/
#include "modules/dreamview/backend/util/http_client.h"
#include "modules/common/util/http_client.h"
#include <curlpp/Easy.hpp>
#include <curlpp/Exception.hpp>
......@@ -24,11 +24,9 @@
#include "modules/common/log.h"
namespace apollo {
namespace dreamview {
namespace common {
namespace util {
using apollo::common::ErrorCode;
using apollo::common::Status;
using Json = nlohmann::json;
Status HttpClient::Post(const std::string &url, const Json &json,
......@@ -73,5 +71,5 @@ Status HttpClient::Post(const std::string &url, const Json &json,
}
} // namespace util
} // namespace dreamview
} // namespace common
} // namespace apollo
......@@ -14,8 +14,8 @@
* limitations under the License.
*****************************************************************************/
#ifndef MODULES_DREAMVIEW_BACKEND_UTIL_HTTP_CLIENT_H_
#define MODULES_DREAMVIEW_BACKEND_UTIL_HTTP_CLIENT_H_
#ifndef MODULES_COMMON_UTIL_HTTP_CLIENT_H_
#define MODULES_COMMON_UTIL_HTTP_CLIENT_H_
#include <string>
......@@ -23,7 +23,7 @@
#include "third_party/json/json.hpp"
namespace apollo {
namespace dreamview {
namespace common {
namespace util {
class HttpClient {
......@@ -31,20 +31,18 @@ class HttpClient {
/**
* @brief post a json to target url, get response as string.
*/
static apollo::common::Status Post(const std::string &url,
const nlohmann::json &json,
std::string *result = nullptr);
static Status Post(const std::string &url, const nlohmann::json &json,
std::string *result = nullptr);
/**
* @brief post a json to target url, get response as json.
*/
static apollo::common::Status Post(const std::string &url,
const nlohmann::json &json,
nlohmann::json *result);
static Status Post(const std::string &url, const nlohmann::json &json,
nlohmann::json *result);
};
} // namespace util
} // namespace dreamview
} // namespace common
} // namespace apollo
#endif // MODULES_DREAMVIEW_BACKEND_UTIL_HTTP_CLIENT_H_
#endif // MODULES_COMMON_UTIL_HTTP_CLIENT_H_
......@@ -14,13 +14,13 @@
* limitations under the License.
*****************************************************************************/
#include "modules/dreamview/backend/util/json_util.h"
#include "modules/common/util/json_util.h"
#include "google/protobuf/util/json_util.h"
#include "modules/common/log.h"
namespace apollo {
namespace dreamview {
namespace common {
namespace util {
namespace {
......@@ -92,5 +92,5 @@ bool JsonUtil::GetStringVectorFromJson(const Json &json, const std::string &key,
}
} // namespace util
} // namespace dreamview
} // namespace common
} // namespace apollo
......@@ -14,8 +14,8 @@
* limitations under the License.
*****************************************************************************/
#ifndef MODULES_DREAMVIEW_BACKEND_UTIL_JSON_UTIL_H_
#define MODULES_DREAMVIEW_BACKEND_UTIL_JSON_UTIL_H_
#ifndef MODULES_COMMON_UTIL_JSON_UTIL_H_
#define MODULES_COMMON_UTIL_JSON_UTIL_H_
#include <string>
#include <vector>
......@@ -24,7 +24,7 @@
#include "third_party/json/json.hpp"
namespace apollo {
namespace dreamview {
namespace common {
namespace util {
class JsonUtil {
......@@ -53,7 +53,7 @@ class JsonUtil {
};
} // namespace util
} // namespace dreamview
} // namespace common
} // namespace apollo
#endif // MODULES_DREAMVIEW_BACKEND_UTIL_JSON_UTIL_H_
#endif // MODULES_COMMON_UTIL_JSON_UTIL_H_
......@@ -14,27 +14,27 @@
* limitations under the License.
*****************************************************************************/
#include "modules/dreamview/backend/util/json_util.h"
#include "modules/common/util/json_util.h"
#include "google/protobuf/util/json_util.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "modules/dreamview/proto/hmi_status.pb.h"
#include "modules/common/proto/error_code.pb.h"
namespace apollo {
namespace dreamview {
namespace common {
namespace util {
using Json = nlohmann::json;
TEST(JsonUtilTest, ProtoToTypedJson) {
HMIStatus status;
status.set_current_map("MapA");
StatusPb status;
status.set_msg("MsgA");
const std::string json_str = JsonUtil::ProtoToTypedJson("TypeA", status);
Json json_obj = Json::parse(json_str.begin(), json_str.end());
EXPECT_EQ("TypeA", json_obj["type"]);
EXPECT_EQ("MapA", json_obj["data"]["currentMap"]);
EXPECT_EQ("MsgA", json_obj["data"]["msg"]);
}
TEST(JsonUtilTest, GetStringFromJson) {
......@@ -95,5 +95,5 @@ TEST(JsonUtilTest, GetStringVectorFromJson) {
}
} // namespace util
} // namespace dreamview
} // namespace common
} // namespace apollo
......@@ -28,13 +28,13 @@ cc_library(
deps = [
":vehicle_manager",
"//modules/common/adapters:adapter_manager",
"//modules/common/util:http_client",
"//modules/common/util:json_util",
"//modules/common/util:map_util",
"//modules/data/proto:task_proto",
"//modules/dreamview/backend/common:dreamview_gflags",
"//modules/dreamview/backend/handlers:websocket",
"//modules/dreamview/backend/map:map_service",
"//modules/dreamview/backend/util:http_client",
"//modules/dreamview/backend/util:json_util",
"//modules/dreamview/proto:hmi_config_proto",
"//modules/dreamview/proto:hmi_status_proto",
"@com_google_protobuf//:protobuf",
......
......@@ -22,6 +22,8 @@
#include "gflags/gflags.h"
#include "modules/common/adapters/adapter_manager.h"
#include "modules/common/util/http_client.h"
#include "modules/common/util/json_util.h"
#include "modules/common/util/map_util.h"
#include "modules/common/util/string_tokenizer.h"
#include "modules/common/util/string_util.h"
......@@ -30,8 +32,6 @@
#include "modules/data/proto/task.pb.h"
#include "modules/dreamview/backend/common/dreamview_gflags.h"
#include "modules/dreamview/backend/hmi/vehicle_manager.h"
#include "modules/dreamview/backend/util/http_client.h"
#include "modules/dreamview/backend/util/json_util.h"
#include "modules/monitor/proto/system_status.pb.h"
DEFINE_string(global_flagfile, "modules/common/data/global_flagfile.txt",
......@@ -55,10 +55,10 @@ using apollo::canbus::Chassis;
using apollo::common::adapter::AdapterManager;
using apollo::common::util::FindOrNull;
using apollo::common::util::GetProtoFromASCIIFile;
using apollo::common::util::JsonUtil;
using apollo::common::util::StringTokenizer;
using apollo::control::DrivingAction;
using apollo::data::VehicleInfo;
using apollo::dreamview::util::JsonUtil;
using google::protobuf::Map;
using Json = WebSocketHandler::Json;
......@@ -404,8 +404,8 @@ void HMI::CheckOTAUpdates() {
ota_request["tag"] = std::getenv("DOCKER_IMG");
Json ota_response;
const auto status = util::HttpClient::Post(FLAGS_ota_service_url,
ota_request, &ota_response);
const auto status = apollo::common::util::HttpClient::Post(
FLAGS_ota_service_url, ota_request, &ota_response);
if (status.ok()) {
CHECK(JsonUtil::GetStringFromJson(ota_response, "tag",
status_.mutable_ota_update()));
......
......@@ -14,7 +14,7 @@ cc_library(
"-lboost_thread",
],
deps = [
"//modules/dreamview/backend/util:json_util",
"//modules/common/util:json_util",
"//modules/map/hdmap:hdmap_util",
"//modules/map/pnc_map",
"//third_party/json",
......
......@@ -18,14 +18,15 @@
#include <algorithm>
#include "modules/common/util/json_util.h"
#include "modules/common/util/string_util.h"
#include "modules/map/hdmap/hdmap_util.h"
#include "modules/dreamview/backend/util/json_util.h"
namespace apollo {
namespace dreamview {
using apollo::common::PointENU;
using apollo::common::util::JsonUtil;
using apollo::hdmap::Map;
using apollo::hdmap::Id;
using apollo::hdmap::LaneInfoConstPtr;
......@@ -41,7 +42,6 @@ using apollo::hdmap::RouteSegments;
using apollo::hdmap::SimMapFile;
using apollo::routing::RoutingResponse;
using apollo::routing::RoutingRequest;
using apollo::dreamview::util::JsonUtil;
namespace {
......
......@@ -16,9 +16,9 @@
#include "modules/dreamview/backend/simulation_world/simulation_world_updater.h"
#include "modules/common/util/json_util.h"
#include "modules/common/util/map_util.h"
#include "modules/dreamview/backend/common/dreamview_gflags.h"
#include "modules/dreamview/backend/util/json_util.h"
#include "modules/map/hdmap/hdmap_util.h"
namespace apollo {
......@@ -26,8 +26,9 @@ namespace dreamview {
using apollo::common::adapter::AdapterManager;
using apollo::common::monitor::MonitorMessageItem;
using apollo::common::util::GetProtoFromASCIIFile;
using apollo::common::util::ContainsKey;
using apollo::common::util::GetProtoFromASCIIFile;
using apollo::common::util::JsonUtil;
using apollo::hdmap::EndWayPointFile;
using apollo::routing::RoutingRequest;
using Json = nlohmann::json;
......@@ -49,7 +50,7 @@ SimulationWorldUpdater::SimulationWorldUpdater(WebSocketHandler *websocket,
MapElementIds map_element_ids(*iter);
auto retrieved = map_service_->RetrieveMapElements(map_element_ids);
websocket_->SendData(
conn, util::JsonUtil::ProtoToTypedJson("MapData", retrieved));
conn, JsonUtil::ProtoToTypedJson("MapData", retrieved));
}
});
......
......@@ -2,46 +2,6 @@ load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "json_util",
srcs = [
"json_util.cc",
],
hdrs = [
"json_util.h",
],
deps = [
"//modules/common:log",
"//third_party/json",
"@com_google_protobuf//:protobuf",
],
)
cc_test(
name = "json_util_test",
size = "small",
srcs = [
"json_util_test.cc",
],
deps = [
":json_util",
"//modules/dreamview/proto:hmi_status_proto",
"@gtest//:main",
],
)
cc_library(
name = "http_client",
srcs = ["http_client.cc"],
hdrs = ["http_client.h"],
deps = [
"//modules/common:log",
"//modules/common/status",
"//third_party/json",
"@curlpp//:curlpp",
],
)
cc_library(
name = "trajectory_point_collector",
srcs = [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册