Files
moloapp/src/google/discovery/planner.cpp
T
retoorandClaude Sonnet 5.5 4ae0b8ec4b feat(clients): discovery: generic engine for all Google Discovery APIs
Directory and document cache with caps, parameter and body validation before
traffic, path/query/repeated/regional handling, key-or-OAuth choice, paging,
long-running wait, whole-directory checker script, demo at /google/discovery.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 15:03:40 +01:00

208 lines
7.5 KiB
C++

#include "google/discovery/planner.h"
#include <algorithm>
#include <set>
#include "clients/url.h"
#include "google/discovery/caps.h"
#include "google/discovery/validate.h"
#include "google/errors.h"
namespace molo::google::discovery {
using clients::body::Json;
namespace {
[[noreturn]] void bad(const std::string& why) { throw GoogleError::invalid(why); }
// Document-level query parameters a caller may set. Everything else the
// document declares globally (alt, key, access_token, oauth_token, callback,
// uploadType, upload_protocol, $.xgafv) either changes the reply format,
// carries a credential, or switches protocol: rejected by name.
bool allowed_global(const std::string& name) {
return name == "fields" || name == "quotaUser" || name == "prettyPrint";
}
} // namespace
const char* auth_mode_name(AuthMode m) {
switch (m) {
case AuthMode::kAuto: return "auto";
case AuthMode::kKey: return "key";
case AuthMode::kOAuth: return "oauth";
}
return "auto";
}
AuthMode auth_mode_from(const std::string& name) {
if (name.empty() || name == "auto") return AuthMode::kAuto;
if (name == "key") return AuthMode::kKey;
if (name == "oauth") return AuthMode::kOAuth;
bad("auth must be auto, key or oauth");
}
std::string Planner::encode_path_value(const std::string& value, bool reserved) {
if (!reserved) return clients::Url::encode_component(value);
std::string out;
std::size_t start = 0;
while (true) {
std::size_t end = value.find('/', start);
out += clients::Url::encode_component(
value.substr(start, end == std::string::npos ? std::string::npos : end - start));
if (end == std::string::npos) break;
out += '/';
start = end + 1;
}
return out;
}
Json PreparedCall::to_json() const {
Json o = Json::object();
o.set("api", Json(api_id));
o.set("method", Json(method_id));
o.set("httpMethod", Json(clients::method_name(http)));
o.set("url", Json(endpoint.base_url + path));
o.set("credential", Json(endpoint.prefer_key ? "key" : "oauth"));
o.set("hasBody", Json(has_body));
o.set("support", Json(support_name(support)));
o.set("pageable", Json(pageable));
o.set("operation", Json(operation));
Json n = Json::array();
for (const std::string& s : notes) n.push(Json(s));
o.set("notes", n);
return o;
}
PreparedCall Planner::plan(const Api& api, const Method& m, const CallArgs& args) {
if (m.support == Support::kRejected) {
std::string why;
for (const std::string& n : m.notes) why += (why.empty() ? "" : "; ") + n;
bad("method " + m.id + " cannot be called by this engine: " + why);
}
if (!args.params.is_object()) bad("params must be an object");
// Endpoint: the global host, or a documented regional one.
std::string root = api.root_url();
if (!args.location.empty()) {
auto it = std::find_if(api.endpoints().begin(), api.endpoints().end(),
[&](const EndpointRow& e) { return e.location == args.location; });
if (it == api.endpoints().end()) {
std::string known;
for (std::size_t i = 0; i < api.endpoints().size() && i < 8; ++i)
known += (i ? ", " : "") + api.endpoints()[i].location;
bad("api " + api.id() + " has no endpoint for location '" + args.location + "'" +
(known.empty() ? " (it documents no regional endpoints)" : " (e.g. " + known + ")"));
}
root = it->url;
}
std::string base = root + api.service_path();
while (!base.empty() && base.back() == '/') base.pop_back();
// Parameters.
std::set<std::string> seen;
std::vector<std::pair<std::string, std::string>> query;
std::map<std::string, std::string> path_values;
for (const auto& [name, value] : args.params.as_object()) {
auto it = m.params.find(name);
if (it == m.params.end()) {
const auto& g = api.global_params();
if (std::find(g.begin(), g.end(), name) != g.end()) {
if (!allowed_global(name))
bad("parameter " + name + " is reserved: it is set by the engine or changes the reply format");
Param p;
p.name = name;
p.location = "query";
p.type = name == "prettyPrint" ? "boolean" : "string";
for (const std::string& v : ParamValidator::values(p, value)) query.emplace_back(name, v);
seen.insert(name);
continue;
}
std::string known;
for (const auto& [n, p] : m.params) known += (known.empty() ? "" : ", ") + n;
bad("method " + m.id + " has no parameter '" + name + "' (accepts: " + known + ")");
}
const Param& p = it->second;
std::vector<std::string> vals = ParamValidator::values(p, value);
seen.insert(name);
if (p.location == "path") {
if (vals.size() != 1) bad("path parameter " + name + " takes one value");
path_values[name] = vals[0];
} else {
for (const std::string& v : vals) query.emplace_back(name, v);
}
}
for (const auto& [name, p] : m.params)
if (p.required && !seen.count(name)) bad("required parameter " + name + " is missing");
if (query.size() > DiscoveryCaps::kMaxQueryParams)
bad("more than " + std::to_string(DiscoveryCaps::kMaxQueryParams) + " query values");
// Path: substitute {x} and {+x}.
std::string path;
for (std::size_t i = 0; i < m.path.size(); ++i) {
if (m.path[i] != '{') {
path += m.path[i];
continue;
}
std::size_t close = m.path.find('}', i);
std::string var = m.path.substr(i + 1, close - i - 1);
bool reserved = !var.empty() && var[0] == '+';
if (reserved) var.erase(0, 1);
auto v = path_values.find(var);
if (v == path_values.end()) bad("required path parameter " + var + " is missing");
path += encode_path_value(v->second, reserved);
i = close;
}
std::string qs;
for (const auto& [k, v] : query)
qs += (qs.empty() ? "?" : "&") + clients::Url::encode_component(k) + "=" +
clients::Url::encode_component(v);
path = "/" + path + qs;
if (path.size() > DiscoveryCaps::kMaxPathChars)
bad("request target is longer than " + std::to_string(DiscoveryCaps::kMaxPathChars));
PreparedCall out;
out.api_id = api.id();
out.method_id = m.id;
out.support = m.support;
out.notes = m.notes;
out.path = path;
if (m.http_method == "GET") out.http = clients::Method::kGet;
else if (m.http_method == "POST") out.http = clients::Method::kPost;
else if (m.http_method == "PUT") out.http = clients::Method::kPut;
else if (m.http_method == "PATCH") out.http = clients::Method::kPatch;
else out.http = clients::Method::kDelete;
out.endpoint.name = api.name();
out.endpoint.base_url = base;
switch (args.auth) {
case AuthMode::kKey: out.endpoint.prefer_key = true; break;
case AuthMode::kOAuth: out.endpoint.prefer_key = false; break;
case AuthMode::kAuto: out.endpoint.prefer_key = !m.needs_oauth(); break;
}
out.oauth_scoped = m.needs_oauth();
// Body.
bool verb_has_body = out.http == clients::Method::kPost || out.http == clients::Method::kPut ||
out.http == clients::Method::kPatch;
if (m.request_ref.empty()) {
if (!args.body.is_null()) bad("method " + m.id + " takes no request body");
if (verb_has_body) {
out.body = Json::object();
out.has_body = true;
}
} else if (!verb_has_body) {
bad("method " + m.id + " declares a body on a " + m.http_method);
} else {
out.body = args.body.is_null() ? Json::object() : args.body;
BodyValidator(api).validate(m.request_ref, out.body);
out.has_body = true;
}
out.pageable = api.is_pageable(m);
out.operation = !m.response_ref.empty() && api.is_operation_schema(m.response_ref);
return out;
}
} // namespace molo::google::discovery