Skip to content

Commit

Permalink
http spans
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Aug 8, 2024
1 parent cdf4bae commit abd111d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/neutrino_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,47 @@
************************************************************************************/
#include <string>
#include <ssod/neutrino_api.h>
#include <ssod/sentry.h>

neutrino::neutrino(dpp::cluster* cl, const std::string& userid, const std::string& apikey) : cluster(cl), user_id(userid), api_key(apikey) {
}

void neutrino::contains_bad_word(const std::string& text, swear_filter_event_t callback) {
std::string request = nlohmann::json({
{ "content", text },
{ "censor-character", "#" },
{ "catalog", "strict" },
{ "output-format", "JSON" },
}).dump();
this->cluster->request("https://neutrinoapi.net/bad-word-filter", dpp::m_post, [this, text, callback](const auto& rv) {
swear_filter_t sf;
nlohmann::json j;
std::string request = nlohmann::json({
{ "content", text },
{ "censor-character", "#" },
{ "catalog", "strict" },
{ "output-format", "JSON" },
}).dump();
void* tx = sentry::get_user_transaction();
void* span = sentry::http_span(tx, "https://neutrinoapi.net/bad-word-filter", "POST");
this->cluster->request("https://neutrinoapi.net/bad-word-filter", dpp::m_post, [this, text, callback, span](const auto& rv) {
swear_filter_t sf;
nlohmann::json j;
sf.clean = true;
sf.censored_content = text;
if (rv.error == dpp::h_success && !rv.body.empty()) {
try {
j = nlohmann::json::parse(rv.body);
if (j.contains("is-bad") && j.at("is-bad").get<bool>()) {
sf.clean = false;
sf.censored_content = j.at("censored-content").get<std::string>();
}
}
catch (const std::exception &e) {
callback(sf);
}
}
callback(sf);
},
request,
"application/json",
{
{ "Content-Type", "application/json" },
{ "Accept", "application/json" },
{ "user-id", this->user_id },
{ "api-key", this->api_key },
}
);
sentry::end_http_span(span, rv.status);
if (rv.error == dpp::h_success && !rv.body.empty()) {
try {
j = nlohmann::json::parse(rv.body);
if (j.contains("is-bad") && j.at("is-bad").get<bool>()) {
sf.clean = false;
sf.censored_content = j.at("censored-content").get<std::string>();
}
}
catch (const std::exception &e) {
callback(sf);
}
}
callback(sf);
},
request,
"application/json",
{
{ "Content-Type", "application/json" },
{ "Accept", "application/json" },
{ "user-id", this->user_id },
{ "api-key", this->api_key },
}
);
}
3 changes: 3 additions & 0 deletions src/sentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ namespace sentry {

void* http_span(void* tx, const std::string& url, const std::string& method) {
void* s = span(tx, method + " " + url, "http.client");
if (s) {
sentry_transaction_set_data((sentry_transaction_t*)s, "http.request.method", sentry_value_new_string(method.c_str()));
}
return s;
}

Expand Down

0 comments on commit abd111d

Please sign in to comment.