refacor media widget, apply clang format rule
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <curl/curl.h>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
@@ -17,20 +18,20 @@ size_t write_to_string(void *contents, size_t size, size_t nmemb, void *userp) {
|
||||
std::string trim(std::string value) {
|
||||
auto not_space = [](unsigned char c) { return std::isspace(c) == 0; };
|
||||
value.erase(value.begin(),
|
||||
std::find_if(value.begin(), value.end(), not_space));
|
||||
value.erase(std::find_if(value.rbegin(), value.rend(), not_space).base(),
|
||||
std::ranges::find_if(value, not_space));
|
||||
value.erase(std::ranges::find_if(std::ranges::reverse_view(value), not_space).base(),
|
||||
value.end());
|
||||
return value;
|
||||
}
|
||||
|
||||
size_t header_to_map(char *buffer, size_t size, size_t nitems, void *userdata) {
|
||||
size_t total = size * nitems;
|
||||
size_t total = size * nitems;
|
||||
auto *header_map = static_cast<std::map<std::string, std::string> *>(userdata);
|
||||
|
||||
std::string line(buffer, total);
|
||||
auto colon = line.find(':');
|
||||
if (colon != std::string::npos) {
|
||||
auto key = trim(line.substr(0, colon));
|
||||
auto key = trim(line.substr(0, colon));
|
||||
auto value = trim(line.substr(colon + 1));
|
||||
if (!key.empty()) {
|
||||
header_map->insert_or_assign(std::move(key), std::move(value));
|
||||
@@ -38,19 +39,19 @@ size_t header_to_map(char *buffer, size_t size, size_t nitems, void *userdata) {
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
HttpResponse HttpConnection::get(const std::string &url,
|
||||
const std::map<std::string, std::string> &headers,
|
||||
long timeout_ms) {
|
||||
const std::map<std::string, std::string> &headers,
|
||||
long timeout_ms) {
|
||||
return performRequest("GET", url, std::string(), headers, std::string(), timeout_ms);
|
||||
}
|
||||
|
||||
HttpResponse HttpConnection::post(const std::string &url,
|
||||
const std::string &body,
|
||||
const std::map<std::string, std::string> &headers,
|
||||
const std::string &content_type,
|
||||
long timeout_ms) {
|
||||
const std::string &body,
|
||||
const std::map<std::string, std::string> &headers,
|
||||
const std::string &content_type,
|
||||
long timeout_ms) {
|
||||
return performRequest("POST", url, body, headers, content_type, timeout_ms);
|
||||
}
|
||||
|
||||
@@ -91,12 +92,12 @@ HttpResponse HttpConnection::performRequest(const std::string &method,
|
||||
struct curl_slist *header_list = nullptr;
|
||||
for (const auto &pair : headers) {
|
||||
std::string header = pair.first + ": " + pair.second;
|
||||
header_list = curl_slist_append(header_list, header.c_str());
|
||||
header_list = curl_slist_append(header_list, header.c_str());
|
||||
}
|
||||
|
||||
if (method == "POST" && !content_type.empty()) {
|
||||
std::string content_header = "Content-Type: " + content_type;
|
||||
header_list = curl_slist_append(header_list, content_header.c_str());
|
||||
header_list = curl_slist_append(header_list, content_header.c_str());
|
||||
}
|
||||
|
||||
if (header_list) {
|
||||
|
||||
Reference in New Issue
Block a user