Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
royshil committed Jul 29, 2023
1 parent 00deed9 commit bedaf91
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 220 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakStringLiterals: false # apparently unpredictable
ColumnLimit: 80
ColumnLimit: 100
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 8
Expand All @@ -53,7 +53,7 @@ Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
FixNamespaceComments: false
ForEachMacros:
ForEachMacros:
- 'json_object_foreach'
- 'json_object_foreach_safe'
- 'json_array_foreach'
Expand Down
3 changes: 1 addition & 2 deletions src/plugin-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ extern struct obs_source_info url_source;

bool obs_module_load(void)
{
obs_log(LOG_INFO, "plugin loaded successfully (version %s)",
PLUGIN_VERSION);
obs_log(LOG_INFO, "plugin loaded successfully (version %s)", PLUGIN_VERSION);
obs_register_source(&url_source);
return true;
}
Expand Down
50 changes: 17 additions & 33 deletions src/request-data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
#include <util/base.h>
#include <nlohmann/json.hpp>

static const std::string USER_AGENT =
std::string(PLUGIN_NAME) + "/" + std::string(PLUGIN_VERSION);
static const std::string USER_AGENT = std::string(PLUGIN_NAME) + "/" + std::string(PLUGIN_VERSION);

std::size_t writeFunctionStdString(void *ptr, std::size_t size, size_t nmemb,
std::string *data)
std::size_t writeFunctionStdString(void *ptr, std::size_t size, size_t nmemb, std::string *data)
{
data->append(static_cast<char *>(ptr), size * nmemb);
return size * nmemb;
}

struct request_data_handler_response
request_data_handler(url_source_request_data *request_data)
struct request_data_handler_response request_data_handler(url_source_request_data *request_data)
{
// Build the request with libcurl
CURL *curl = curl_easy_init();
Expand All @@ -39,10 +36,8 @@ request_data_handler(url_source_request_data *request_data)
// Add request headers
struct curl_slist *headers = NULL;
for (auto header : request_data->headers) {
std::string header_string =
header.first + ": " + header.second;
headers = curl_slist_append(headers,
header_string.c_str());
std::string header_string = header.first + ": " + header.second;
headers = curl_slist_append(headers, header_string.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
Expand All @@ -54,8 +49,7 @@ request_data_handler(url_source_request_data *request_data)
code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (code != CURLE_OK) {
obs_log(LOG_INFO, "Failed to send request: %s",
curl_easy_strerror(code));
obs_log(LOG_INFO, "Failed to send request: %s", curl_easy_strerror(code));
// Return an error response
struct request_data_handler_response responseFail;
responseFail.error_message = curl_easy_strerror(code);
Expand All @@ -74,8 +68,7 @@ request_data_handler(url_source_request_data *request_data)
try {
json = nlohmann::json::parse(responseBody);
} catch (nlohmann::json::parse_error &e) {
obs_log(LOG_INFO, "Failed to parse JSON response: %s",
e.what());
obs_log(LOG_INFO, "Failed to parse JSON response: %s", e.what());
// Return an error response
struct request_data_handler_response responseFail;
responseFail.error_message = e.what();
Expand All @@ -87,16 +80,12 @@ request_data_handler(url_source_request_data *request_data)
try {
response.body_parsed =
json.at(nlohmann::json::json_pointer(
request_data
->output_json_path))
request_data->output_json_path))
.get<std::string>();
} catch (nlohmann::json::exception &e) {
obs_log(LOG_INFO,
"Failed to get JSON value: %s",
e.what());
obs_log(LOG_INFO, "Failed to get JSON value: %s", e.what());
// Return an error response
struct request_data_handler_response
responseFail;
struct request_data_handler_response responseFail;
responseFail.error_message = e.what();
responseFail.status_code = -1;
return responseFail;
Expand Down Expand Up @@ -135,17 +124,15 @@ std::string serialize_request_data(url_source_request_data *request_data)
return json.dump();
}

url_source_request_data
unserialize_request_data(std::string serialized_request_data)
url_source_request_data unserialize_request_data(std::string serialized_request_data)
{
obs_log(LOG_INFO, "Unserializing request data");
// Unserialize the request data from a string using JSON
nlohmann::json json;
try {
json = nlohmann::json::parse(serialized_request_data);
} catch (nlohmann::json::parse_error &e) {
obs_log(LOG_INFO, "Failed to parse JSON request data: %s",
e.what());
obs_log(LOG_INFO, "Failed to parse JSON request data: %s", e.what());
// Return an empty request data object
url_source_request_data request_data;
return request_data;
Expand All @@ -156,19 +143,16 @@ unserialize_request_data(std::string serialized_request_data)
request_data.method = json["method"].get<std::string>();
request_data.body = json["body"].get<std::string>();
request_data.output_type = json["output_type"].get<std::string>();
request_data.output_json_path =
json["output_json_path"].get<std::string>();
request_data.output_json_path = json["output_json_path"].get<std::string>();
request_data.output_xpath = json["output_xpath"].get<std::string>();
request_data.output_regex = json["output_regex"].get<std::string>();
request_data.output_regex_flags =
json["output_regex_flags"].get<std::string>();
request_data.output_regex_group =
json["output_regex_group"].get<std::string>();
request_data.output_regex_flags = json["output_regex_flags"].get<std::string>();
request_data.output_regex_group = json["output_regex_group"].get<std::string>();

nlohmann::json headers_json = json["headers"];
for (auto header : headers_json.items()) {
request_data.headers.push_back(std::make_pair(
header.key(), header.value().get<std::string>()));
request_data.headers.push_back(
std::make_pair(header.key(), header.value().get<std::string>()));
}

return request_data;
Expand Down
6 changes: 2 additions & 4 deletions src/request-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ struct request_data_handler_response {
std::string error_message;
};

struct request_data_handler_response
request_data_handler(url_source_request_data *request_data);
struct request_data_handler_response request_data_handler(url_source_request_data *request_data);

std::string serialize_request_data(url_source_request_data *request_data);

url_source_request_data
unserialize_request_data(std::string serialized_request_data);
url_source_request_data unserialize_request_data(std::string serialized_request_data);

#endif // REQUEST_DATA_H
Loading

0 comments on commit bedaf91

Please sign in to comment.