feat(clients): workspace: 20 Workspace APIs from Discovery, delegation sub claim, demo

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-25 15:03:45 +01:00
co-authored by Claude Sonnet 5.5
parent 3db67fcf1f
commit 54b564f9b9
24 changed files with 3949 additions and 3 deletions
+135
View File
@@ -0,0 +1,135 @@
#include "google/workspace/workspace_api.h"
#include "google/errors.h"
#include "google/workspace/caps.h"
namespace molo::google::workspace {
namespace {
ServiceEndpoint endpoint_for(const std::string& api) {
const ApiSpec* spec = find_api(api);
if (!spec) throw GoogleError::invalid("unknown Workspace API " + api);
return ServiceEndpoint{spec->name, spec->base_url, false};
}
const char* kind_name(Kind k) {
switch (k) {
case Kind::kRead: return "read";
case Kind::kWrite: return "write";
case Kind::kDestroy: return "destroy";
case Kind::kSend: return "send";
}
return "?";
}
Json without_token(const Json& params) {
Json out = Json::object();
for (const auto& [k, v] : params.as_object())
if (k != "pageToken") out.set(k, v);
return out;
}
} // namespace
WorkspaceApi::WorkspaceApi(std::shared_ptr<GoogleClient> client, const std::string& api)
: Service(std::move(client), endpoint_for(api)), api_(find_api(api)) {}
Json WorkspaceApi::make_params(const ParamList& list) {
Json out = Json::object();
for (const auto& [k, v] : list) out.set(k, v);
return out;
}
PreparedCall WorkspaceApi::prepare(const std::string& id, const Json& params,
const Json* body) const {
const CallSpec* spec = find_call_in(api_->name, id);
if (!spec) {
const CallSpec* other = find_call(id);
if (other) throw GoogleError::invalid(id + " belongs to " + other->api + ", not " + api_->name);
throw GoogleError::invalid("unknown call " + id);
}
if (has_flag(*spec, 'b'))
throw GoogleError::invalid(id + " answers bytes, not JSON; not supported");
return PreparedCall::build(*spec, params, body);
}
Json WorkspaceApi::run(const std::string& id, Kind want, const Json& params,
const Json* body) const {
PreparedCall call = prepare(id, params, body);
Kind have = kind_of(*call.spec);
if (have != want)
throw GoogleError::invalid(id + " is a " + kind_name(have) + " call, use " + kind_name(have) + "()");
return client_->request(call.method, endpoint_, call.path, call.has_body ? &call.body : nullptr);
}
Json WorkspaceApi::read(const std::string& id, const Json& params, const Json* body) const {
return run(id, Kind::kRead, params, body);
}
Json WorkspaceApi::write(const std::string& id, const Json& params, const Json* body,
bool confirm) const {
prepare(id, params, body); // validation errors first
if (!confirm) throw GoogleError::invalid("write " + id + " needs confirm=true");
return run(id, Kind::kWrite, params, body);
}
Json WorkspaceApi::destroy(const std::string& id, const Json& params, bool confirm) const {
prepare(id, params, nullptr);
if (!confirm) throw GoogleError::invalid("destroy " + id + " needs confirm=true");
return run(id, Kind::kDestroy, params, nullptr);
}
Json WorkspaceApi::send(const std::string& id, const Json& params, const Json* body,
bool confirm) const {
prepare(id, params, body);
if (!confirm) throw GoogleError::invalid("send " + id + " needs confirm=true");
return run(id, Kind::kSend, params, body);
}
std::vector<Json> WorkspaceApi::pages(const std::string& id, const Json& params,
int max_pages) const {
if (max_pages < 1 || max_pages > WorkspaceCaps::kMaxPages)
throw GoogleError::invalid("max_pages must be 1 to 20");
if (!params.is_object()) throw GoogleError::invalid("params must be a JSON object");
PreparedCall first = prepare(id, without_token(params));
if (kind_of(*first.spec) != Kind::kRead)
throw GoogleError::invalid(id + " is not a read call, pages() walks reads only");
const Json* start = params.find("pageToken");
std::vector<Json> out;
std::string token = start && start->is_string() ? start->as_string() : "";
for (int i = 0; i < max_pages; ++i) {
std::string path = token.empty() ? first.path : first.path_with_token(token);
out.push_back(client_->request(first.method, endpoint_, path, nullptr));
const Json* next = out.back().find("nextPageToken");
if (!next || !next->is_string() || next->as_string().empty()) break;
token = next->as_string();
}
return out;
}
Listing WorkspaceApi::list(const std::string& id, const Json& params, int max_pages) const {
const CallSpec* spec = find_call_in(api_->name, id);
if (!spec) throw GoogleError::invalid("unknown call " + id);
if (!*spec->items_key) throw GoogleError::invalid(id + " has no single items array");
Listing out;
std::vector<Json> docs = pages(id, params, max_pages);
for (const Json& doc : docs) {
++out.pages;
const Json* arr = doc.find(spec->items_key);
if (arr && arr->is_array())
for (const Json& item : arr->as_array()) {
if (out.items.size() >= WorkspaceCaps::kMaxItems) {
out.truncated = true;
break;
}
out.items.push_back(item);
}
const Json* next = doc.find("nextPageToken");
out.next_page_token = next && next->is_string() ? next->as_string() : "";
}
if (!out.next_page_token.empty()) out.truncated = true;
return out;
}
} // namespace molo::google::workspace