Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 46 additions & 45 deletions atos/modules/RESTBridge/src/restbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,63 @@ using namespace ROSChannels;
using namespace std::chrono_literals;
using namespace std::placeholders;

RESTBridge::RESTBridge()
: Module(moduleName),
customCommandActionMsgSub(
*this, std::bind(&RESTBridge::onCustomCommandAction, this, _1)) {
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
RESTBridge::RESTBridge() :
Module(moduleName),
customCommandActionMsgSub(*this, std::bind(&RESTBridge::onCustomCommandAction, this, _1)) {
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
}

RESTBridge::~RESTBridge() {
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
}

void RESTBridge::onCustomCommandAction(
const atos_interfaces::msg::CustomCommandAction::SharedPtr msg) {
if (msg->type == atos_interfaces::msg::CustomCommandAction::POST_JSON) {
RCLCPP_INFO(get_logger(), "Received POST_JSON command: %s",
msg->content.c_str());
json jsonData = parseJsonData(msg->content);
POST(jsonData["endpoint"].get<std::string>(), jsonData["data"]);
}
void RESTBridge::onCustomCommandAction(const atos_interfaces::msg::CustomCommandAction::SharedPtr msg) {
if (msg->type == atos_interfaces::msg::CustomCommandAction::POST) {
RCLCPP_INFO(get_logger(), "Received POST command: %s", msg->content.c_str());
json jsonData = parseJsonData(msg->content);
POST(jsonData["endpoint"].get<std::string>(), jsonData["data"]);
}
}

json RESTBridge::parseJsonData(std::string &msg) {
// Parse the message and return the REST API message
std::replace(msg.begin(), msg.end(), '\'',
'\"'); // Replace single quotes with double quotes to be able to
// parse the message
json j = json::parse(msg);
return j;
json RESTBridge::parseJsonData(std::string& msg) {
// Parse the message and return the REST API message
std::replace(msg.begin(),
msg.end(),
'\'',
'\"'); // Replace single quotes with double quotes to be able to
// parse the message
json j = json::parse(msg);
return j;
}

void RESTBridge::POST(const std::string &endpoint, const json &data) {
// Send A POST request to the specified endpoint with the specified data, Use
// hardcoded headers for now
CURLcode res;
void RESTBridge::POST(const std::string& endpoint, const json& data) {
// Send A POST request to the specified endpoint with the specified data, Use
// hardcoded headers for now
CURLcode res;

if (curl_handle) {
std::string json_str = data.dump(); // Store the JSON string
const char *json_data = json_str.c_str(); // Get the C-string pointer
curl_easy_setopt(curl_handle, CURLOPT_URL, endpoint.c_str());
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, json_data);
if (curl_handle) {
std::string json_str = data.dump(); // Store the JSON string
const char* json_data = json_str.c_str(); // Get the C-string pointer
curl_easy_setopt(curl_handle, CURLOPT_URL, endpoint.c_str());
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, json_data);

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charset: utf-8");
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charset: utf-8");
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);

// Perform the request, res will get the return code
res = curl_easy_perform(curl_handle);
// Perform the request, res will get the return code
res = curl_easy_perform(curl_handle);

// Check for errors
if (res != CURLE_OK) {
RCLCPP_ERROR(get_logger(), "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
}
// Check for errors
if (res != CURLE_OK) {
RCLCPP_ERROR(get_logger(), "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_slist_free_all(headers);
} else {
RCLCPP_ERROR(get_logger(), "curl_handle is NULL, cannot send POST request");
}
}
2 changes: 1 addition & 1 deletion atos_interfaces
1 change: 1 addition & 0 deletions docker-compose-bridge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
- "443:443"
- "9090:9090"
- "8765:8765"
- "8000:8000"
command: bash -c "source /root/atos_ws/install/setup.sh ; ros2 launch atos launch_basic.py insecure:=True"
foxglove-studio:
image: ghcr.io/foxglove/studio:1.76
Expand Down
47 changes: 47 additions & 0 deletions docs/Usage/Modules/RESTBridge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# RESTBridge

## About the module
RESTBridge is a module that allows you to send HTTP requests to a REST API through ATOS. The module utilize OpenScenario CustomCommandActions to specify the type of request (Currently POST and DELETE are supported) and the content of the request.

## Example OpenScenario

```xml
<Event name="web_event" priority="parallel" maximumExecutionCount="1">
<Action name="Send web request">
<UserDefinedAction>
<CustomCommandAction type="POST">{"endpoint": "http://web_server/set_some_value", "data": {"some_key": "some_value"}}</CustomCommandAction>
</UserDefinedAction>
</Action>
<StartTrigger>
<ConditionGroup>
<Condition name="Send web request" delay="0.0" conditionEdge="none">
<ByEntityCondition>
<TriggeringEntities triggeringEntitiesRule="any">
<EntityRef entityRef="entity_name"/>
</TriggeringEntities>
<EntityCondition>
<ReachPositionCondition tolerance="1.0">
<Position>
<LanePosition roadId="5" laneId="-1" s="45.0" offset="0.0"/>
</Position>
</ReachPositionCondition>
</EntityCondition>
</ByEntityCondition>
</Condition>
</ConditionGroup>
</StartTrigger>
</Event>
```

### CustomCommandAction

type: POST or DELETE
data field:

```json
{
"endpoint": "http://web_server/set_some_value", # Required. The endpoint to send the request to.
"data": {"some_key": "some_value"} # Optional. The data to send in the request.
}
```

1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav:
- "Usage/Modules/IntegrationTesting.md"
- "Usage/Modules/JournalControl.md"
- "Usage/Modules/MQTTBridge.md"
- "Usage/Modules/RESTBridge.md"
- "Usage/Modules/OSIAdapter.md"
- "Usage/Modules/ObjectControl.md"
- "Usage/Modules/PointcloudPublisher.md"
Expand Down
Loading