blob: 70641cdd6f8d37a6b75f77b35a7e59070a7738be [file] [log] [blame] [edit]
#include "bmc/redfish.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "bmc/http_connection.h"
#include "absl/functional/any_invocable.h"
#include "absl/functional/bind_front.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "boost/asio/ip/tcp.hpp" // NOLINT(readability/boost)
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
namespace safepower_agent {
namespace Redfish {
namespace beast = boost::beast;
namespace http = beast::http;
static void ParseJson(
absl::AnyInvocable<void(absl::StatusOr<nlohmann::json>) &&> callback,
absl::StatusOr<std::string> res) {
if (!res.ok()) {
std::move(callback)(res.status());
return;
}
nlohmann::json js = nlohmann::json::parse(*res, nullptr, false);
if (js.is_discarded()) {
std::move(callback)(absl::InvalidArgumentError(
absl::StrCat("Failed to parse JSON from BMC: ", *res)));
return;
}
std::move(callback)(js);
}
void Get(absl::string_view target,
absl::AnyInvocable<void(absl::StatusOr<nlohmann::json>) &&> callback,
absl::string_view ip, uint16_t port) {
auto connection = std::make_shared<HttpConnection>();
connection->PerformConnection(
http::verb::get, target,
absl::bind_front(&ParseJson, std::move(callback)), "", ip, port);
}
void Post(absl::string_view target,
absl::AnyInvocable<void(absl::StatusOr<nlohmann::json>) &&> callback,
const nlohmann::json& body, absl::string_view ip, uint16_t port) {
auto connection = std::make_shared<HttpConnection>();
connection->PerformConnection(
http::verb::post, target,
absl::bind_front(&ParseJson, std::move(callback)), body.dump(), ip, port);
}
} // namespace Redfish
} // namespace safepower_agent