119 if (response_body.empty())
126 Poco::JSON::Parser parser;
127 Poco::Dynamic::Var parsed = parser.parse(response_body);
128 const auto object = parsed.extract<Poco::JSON::Object::Ptr>();
130 if (object->has(
"error"))
132 const auto error_var =
object->get(
"error");
133 if (error_var.type() ==
typeid(Poco::JSON::Object::Ptr))
135 const auto error_object = error_var.extract<Poco::JSON::Object::Ptr>();
136 if (error_object->has(
"message"))
138 return error_object->getValue<std::string>(
"message");
141 else if (error_var.type() ==
typeid(std::string))
143 return error_var.convert<std::string>();
147 if (object->has(
"message"))
149 return object->getValue<std::string>(
"message");
152 catch (
const std::exception&)
156 return response_body;
170 Poco::JSON::Object::Ptr
process(Poco::JSON::Object& body_json, std::string& uri)
173 Poco::URI uri_obj(uri);
176 std::ostringstream body_stream;
177 body_json.stringify(body_stream);
180 Poco::Net::HTTPRequest request(
method_, uri_obj.getPath());
182 request.setContentType(
"application/json");
183 request.setContentLength(body_stream.str().size());
198 RCLCPP_WARN(
node_->get_logger(),
"unsupported auth type: %s",
auth_type_.c_str());
203 std::unique_ptr<Poco::Net::HTTPClientSession> session_ptr;
206 if (uri_obj.getScheme() ==
"https")
209 Poco::Net::Context::Params params;
213 params.verificationMode = Poco::Net::Context::VERIFY_NONE;
217 params.verificationMode = Poco::Net::Context::VERIFY_STRICT;
218 params.caLocation =
"/etc/ssl/certs";
221 Poco::Net::Context::Ptr context =
new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, params);
224 session_ptr = std::make_unique<Poco::Net::HTTPSClientSession>(uri_obj.getHost(), uri_obj.getPort(), context);
225 RCLCPP_DEBUG(
node_->get_logger(),
"secure session created");
230 session_ptr = std::make_unique<Poco::Net::HTTPClientSession>(uri_obj.getHost(), uri_obj.getPort());
232 RCLCPP_WARN(
node_->get_logger(),
"insecure session created");
238 std::ostream& os = session_ptr->sendRequest(request);
240 body_json.stringify(os);
242 catch (
const Poco::Net::NetException& e)
244 RCLCPP_ERROR(
node_->get_logger(),
"network error: %s", e.what());
249 Poco::Net::HTTPResponse response;
250 std::istream& rs = session_ptr->receiveResponse(response);
253 if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK)
255 std::ostringstream error_stream;
256 error_stream << rs.rdbuf();
257 const std::string response_body = error_stream.str();
260 if (!description.empty())
262 RCLCPP_ERROR(
node_->get_logger(),
"HTTP Error: %i %s. Description: %s", response.getStatus(),
263 response.getReason().c_str(), description.c_str());
267 RCLCPP_ERROR(
node_->get_logger(),
"HTTP Error: %i %s", response.getStatus(), response.getReason().c_str());
270 std::string message =
"HTTP Error: " + std::to_string(response.getStatus()) +
" " + response.getReason();
271 if (!description.empty())
273 message +=
" - " + description;
282 if (response.getContentType() ==
"text/event-stream" || response.getContentType() ==
"application/x-ndjson")
287 RCLCPP_ERROR(
node_->get_logger(),
"HTTP streaming not supported");
292 if (response.getChunkedTransferEncoding())
295 RCLCPP_ERROR(
node_->get_logger(),
"HTTP Chunked Transfer Encoding not supported");
300 Poco::JSON::Parser parser;
301 Poco::Dynamic::Var result = parser.parse(rs);
302 return result.extract<Poco::JSON::Object::Ptr>();
314 virtual const Poco::JSON::Object
handle_options(std::vector<prompt::PromptOption>& options)
317 Poco::JSON::Object result;
322 if (option.type == prompt_msgs::msg::ModelOption::STRING_TYPE)
324 result.set(option.key, option.value);
329 if (option.type == prompt_msgs::msg::ModelOption::BOOL_TYPE)
331 result.set(option.key, (option.value ==
"true") ? true :
false);
336 if (option.type == prompt_msgs::msg::ModelOption::INT_TYPE)
338 result.set(option.key, std::stoi(option.value));
343 if (option.type == prompt_msgs::msg::ModelOption::REAL_TYPE)
345 result.set(option.key, std::stod(option.value));
350 result.set(option.key, option.value);