121-call table checked against the OpenAI OpenAPI document, admin-key-only client, destructive guards, WebhookVerifier (Standard Webhooks HMAC-SHA256), demo routes under /ai/openai/admin, local and opt-in live tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
94 lines
2.7 KiB
C++
94 lines
2.7 KiB
C++
#include "openai/admin/query.h"
|
|
|
|
#include "openai/admin/caps.h"
|
|
#include "openai/admin/validate.h"
|
|
|
|
namespace molo::openai::admin {
|
|
|
|
namespace {
|
|
void check_name(const std::string& name) {
|
|
if (name.empty() || name.size() > 64) Validate::fail("bad query parameter name");
|
|
for (char c : name)
|
|
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
|
c == '_' || c == '[' || c == ']'))
|
|
Validate::fail("bad query parameter name");
|
|
}
|
|
constexpr std::size_t kMaxPairs = 256;
|
|
} // namespace
|
|
|
|
Query& Query::set(const std::string& name, const std::string& value) {
|
|
check_name(name);
|
|
Validate::text("query " + name, value, AdminCaps::kMaxCursorChars, true);
|
|
if (pairs_.size() >= kMaxPairs) Validate::fail("too many query parameters");
|
|
pairs_.emplace_back(name, value);
|
|
return *this;
|
|
}
|
|
|
|
Query& Query::set_int(const std::string& name, std::int64_t value) {
|
|
return set(name, std::to_string(value));
|
|
}
|
|
|
|
Query& Query::set_bool(const std::string& name, bool value) {
|
|
return set(name, value ? "true" : "false");
|
|
}
|
|
|
|
Query& Query::add_all(const std::string& name, const std::vector<std::string>& values) {
|
|
Validate::count(name, values.size(), AdminCaps::kMaxFilterValues, true);
|
|
for (const std::string& v : values) {
|
|
Validate::text(name, v, AdminCaps::kMaxFilterChars);
|
|
set(name, v);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
bool Query::has(const std::string& name) const {
|
|
for (const auto& p : pairs_)
|
|
if (p.first == name) return true;
|
|
return false;
|
|
}
|
|
|
|
void Query::check_against(Call call) const {
|
|
for (const auto& p : pairs_)
|
|
if (!CallTable::allows_query(call, p.first))
|
|
Validate::fail(std::string(CallTable::spec(call).key) + " has no query parameter " + p.first);
|
|
}
|
|
|
|
std::string Query::escape(const std::string& text) {
|
|
static const char* kHex = "0123456789ABCDEF";
|
|
std::string out;
|
|
for (unsigned char c : text) {
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' ||
|
|
c == '_' || c == '.' || c == '~') {
|
|
out += static_cast<char>(c);
|
|
} else {
|
|
out += '%';
|
|
out += kHex[c >> 4];
|
|
out += kHex[c & 15];
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string Query::encoded() const {
|
|
std::string out;
|
|
for (const auto& p : pairs_) out += (out.empty() ? "?" : "&") + escape(p.first) + "=" + escape(p.second);
|
|
return out;
|
|
}
|
|
|
|
Query ListOptions::to_query() const {
|
|
Query q;
|
|
if (limit) {
|
|
Validate::range("limit", *limit, 1, AdminCaps::kMaxPageLimit);
|
|
q.set_int("limit", *limit);
|
|
}
|
|
if (!after.empty()) q.set("after", after);
|
|
if (!before.empty()) q.set("before", before);
|
|
if (!order.empty()) {
|
|
Validate::one_of("order", order, {"asc", "desc"});
|
|
q.set("order", order);
|
|
}
|
|
return q;
|
|
}
|
|
|
|
} // namespace molo::openai::admin
|