Merge branch 'worktree-agent-af2efb541856af61f'

This commit is contained in:
2026-10-25 15:24:27 +01:00
31 changed files with 3947 additions and 3 deletions
+620 -3
View File
@@ -1,13 +1,630 @@
#include "../GoogleDemos.hpp" #include "../GoogleDemos.hpp"
#include <cstdlib>
#include "../../fw/Router.hpp" #include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "google/cloudai/calls.h"
#include "google/cloudai/caps.h"
#include "google/cloudai/document_ai.h"
#include "google/cloudai/long_audio.h"
#include "google/cloudai/natural_language.h"
#include "google/cloudai/speech_v2.h"
#include "google/cloudai/translation.h"
#include "google/cloudai/video_intelligence.h"
#include "google/cloudai/vision.h"
#include "google/credentials.h"
namespace molo::example::app { namespace molo::example::app {
// Translation, Vision, Natural Language, Video Intelligence, Document AI. Stub: the package agent fills this in. // Cloud AI: Translation v3, Vision, Natural Language, Video Intelligence,
// Document AI, Speech v2 and long-audio Text-to-Speech over the real
// src/google/cloudai part. GET routes need no credential. Every route that
// calls Google is a POST: 422 on bad input (before anything else), 503
// google_not_configured without a credential. Paid calls (translate, annotate,
// analyze, process, recognize, synthesize) and every write or delete are dry
// runs that show the request unless the body says "confirm":true. Long
// running starts answer the operation and never wait.
namespace {
namespace ca = google::cloudai;
using Json = clients::body::Json;
constexpr std::size_t kMaxBody = 12 * 1024 * 1024;
constexpr std::size_t kMaxShownBytes = 1024 * 1024;
constexpr std::size_t kMaxPreviewString = 200;
const char kPage[] = R"CLOUDAI({% extends "base.html" %}
{% block content %}
<h1>Cloud AI</h1>
<p class="hint">Translation, Vision, Natural Language, Video Intelligence, Document AI,
Speech v2 and long-audio Text-to-Speech. Reads run at once. Paid calls and every write are
dry runs that show the request until the body says <code>"confirm":true</code>.</p>
<p id="ca-config" class="hint">Checking Google credentials...</p>
<section class="ca">
<label>Call
<select id="ca-route"></select></label>
<label>Body (JSON object) <textarea id="ca-body" rows="9"></textarea></label>
<div class="ca-actions"><button id="ca-send" type="button" class="primary">Send</button></div>
<p id="ca-error" class="error" role="alert"></p>
<pre id="ca-out" class="ca-out"></pre>
</section>
<style>
.ca label{display:block;margin:.5rem 0}
.ca select,.ca textarea{width:100%;box-sizing:border-box}
.ca-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
.ca-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
</style>
<script src="/embedded/google_cloudai.js"></script>
{% endblock %}
)CLOUDAI";
fw::Response json_out(const Json& j, int status = 200) {
std::string dumped = j.dump();
if (dumped.size() > kMaxShownBytes) {
Json cut = Json::object();
cut.set("truncated", Json(true));
cut.set("bytes", Json(static_cast<double>(dumped.size())));
cut.set("preview", Json(dumped.substr(0, 64 * 1024)));
fw::Response res = fw::JsonResponse(cut.dump(), status);
res.headers.set("Cache-Control", "no-store");
return res;
}
fw::Response res = fw::JsonResponse(dumped, status);
res.headers.set("Cache-Control", "no-store");
return res;
}
[[noreturn]] void rethrow(const google::GoogleError& e) {
if (e.http_status() == 503 && e.status() == "NOT_CONFIGURED")
throw fw::HttpError(503, "google_not_configured", e.message());
if (e.service().empty() && e.status() == "INVALID_ARGUMENT")
throw fw::HttpError(422, "validation_error", e.message());
throw fw::HttpError(502, "google_error", e.status() + ": " + e.message());
}
template <typename F>
auto guarded(F&& f) -> decltype(f()) {
try {
return f();
} catch (const google::GoogleError& e) {
rethrow(e);
}
}
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
Json parse_body(const fw::Request& req) {
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
if (req.body.empty()) return Json::object();
try {
Json j = Json::parse(req.body);
if (!j.is_object()) bad("body must be a JSON object");
return j;
} catch (const clients::body::JsonError&) {
bad("body is not valid JSON");
}
}
std::string text(const Json& body, const char* key, bool required = false) {
const Json* v = body.find(key);
if (!v || v->is_null()) {
if (required) bad(std::string(key) + " is required");
return "";
}
if (!v->is_string()) bad(std::string(key) + " must be a string");
return v->as_string();
}
bool flag(const Json& body, const char* key, bool fallback = false) {
const Json* v = body.find(key);
if (!v || v->is_null()) return fallback;
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
return v->as_bool();
}
int number(const Json& body, const char* key, int lo, int hi, int fallback) {
const Json* v = body.find(key);
if (!v || v->is_null()) return fallback;
if (!v->is_number()) bad(std::string(key) + " must be a number");
int n = static_cast<int>(v->as_int());
if (n < lo || n > hi) bad(std::string(key) + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
return n;
}
std::string pick(const Json& body, const char* key, std::initializer_list<const char*> allowed) {
std::string v = text(body, key, true);
std::string list;
for (const char* a : allowed) {
if (v == a) return v;
list += (list.empty() ? "" : ", ") + std::string(a);
}
bad(std::string(key) + " must be one of " + list);
}
std::vector<std::string> strings(const Json& body, const char* key, bool required) {
std::vector<std::string> out;
const Json* v = body.find(key);
if (!v || v->is_null()) {
if (required) bad(std::string(key) + " is required");
return out;
}
if (!v->is_array()) bad(std::string(key) + " must be an array of strings");
for (const Json& s : v->as_array()) {
if (!s.is_string()) bad(std::string(key) + " must be an array of strings");
out.push_back(s.as_string());
}
return out;
}
// Long strings (base64 documents) are not echoed back in a dry run.
Json redacted(const Json& j) {
if (j.is_string() && j.as_string().size() > kMaxPreviewString)
return Json("<" + std::to_string(j.as_string().size()) + " characters>");
if (j.is_array()) {
Json a = Json::array();
for (const Json& x : j.as_array()) a.push(redacted(x));
return a;
}
if (j.is_object()) {
Json o = Json::object();
for (const auto& [k, v] : j.as_object()) o.set(k, redacted(v));
return o;
}
return j;
}
Json dry_run(const char* call, const Json& request) {
Json o = Json::object();
o.set("dry_run", Json(true));
o.set("call", Json(call));
o.set("request", redacted(request));
o.set("hint", Json("send confirm:true to run it"));
return o;
}
Json operation_json(const google::Operation& op) {
Json o = Json::object();
o.set("operation", Json(op.name));
o.set("done", Json(op.done));
if (op.failed()) o.set("error", Json(op.error_message));
if (op.done && op.response.is_object()) o.set("response", op.response);
return o;
}
// Everything a route needs: the client plus the validated project / location.
class Rig {
public:
explicit Rig(std::shared_ptr<google::GoogleClient> client) : client_(std::move(client)) {}
google::CloudContext context(const Json& body, const char* default_location) const {
google::CloudContext c;
c.project = google::Credentials::env("GOOGLE_CLOUD_PROJECT");
c.location = text(body, "location");
if (c.location.empty()) c.location = default_location;
if (!c.valid()) bad("GOOGLE_CLOUD_PROJECT is missing or the location is malformed");
return c;
}
template <typename S>
S make(const Json& body, const char* default_location) const {
return S(client_, context(body, default_location));
}
const std::shared_ptr<google::GoogleClient>& client() const { return client_; }
private:
std::shared_ptr<google::GoogleClient> client_;
};
ca::NlDocument nl_document(const Json& b) {
ca::NlDocument d;
d.content = text(b, "content");
d.gcs_uri = text(b, "gcsUri");
std::string type = text(b, "type");
if (!type.empty()) d.type = type;
d.language = text(b, "language");
return d;
}
std::vector<ca::VisionFeature> vision_features(const Json& b) {
std::vector<ca::VisionFeature> out;
const Json* v = b.find("features");
if (!v || !v->is_array() || v->as_array().empty()) bad("features is required (array of {type,maxResults})");
for (const Json& f : v->as_array()) {
if (f.is_string()) {
out.push_back({f.as_string(), 0});
continue;
}
if (!f.is_object()) bad("each feature is a type string or {type,maxResults}");
out.push_back({text(f, "type", true), number(f, "maxResults", 0, ca::CloudAiCaps::kMaxResults, 0)});
}
return out;
}
} // namespace
void register_google_cloudai_demo(fw::Router& router, Services& services) { void register_google_cloudai_demo(fw::Router& router, Services& services) {
(void)router; if (!services.google) services.google = google::GoogleClient::from_env();
(void)services; auto client = services.google;
fw::Environment* env = services.templates.get();
services.templates->add_template("google_cloudai.html", kPage);
services.add_feature("google-cloudai", "/google/cloudai");
auto rig = std::make_shared<Rig>(client);
router.get("/google/cloudai", "google-cloudai-page", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Cloud AI");
return fw::Response(200, env->render("google_cloudai.html", ctx), "text/html; charset=utf-8");
});
router.get("/google/cloudai/status", "google-cloudai-status", [client](fw::Request req) {
(void)req;
Json o = Json::object();
o.set("configured", Json(client->configured()));
o.set("project_set", Json(!google::Credentials::env("GOOGLE_CLOUD_PROJECT").empty()));
o.set("calls", Json(static_cast<int>(ca::call_count())));
Json apis = Json::array();
for (const char* a : {"translate v3", "vision v1", "language v2", "videointelligence v1",
"documentai v1", "speech v2", "texttospeech v1"})
apis.push(Json(a));
o.set("apis", apis);
o.set("not_in_discovery", Json("mediatranslation"));
Json caps = Json::object();
caps.set("max_texts", Json(static_cast<int>(ca::CloudAiCaps::kMaxTexts)));
caps.set("max_text_chars", Json(static_cast<int>(ca::CloudAiCaps::kMaxTextChars)));
caps.set("max_images", Json(static_cast<int>(ca::CloudAiCaps::kMaxImages)));
caps.set("max_inline_bytes", Json(static_cast<int>(ca::CloudAiCaps::kMaxAudioBytes)));
o.set("caps", caps);
return json_out(o);
});
// The Discovery-checked call table, for humans.
router.get("/google/cloudai/calls", "google-cloudai-calls", [](fw::Request req) {
(void)req;
Json arr = Json::array();
for (std::size_t i = 0; i < ca::call_count(); ++i) {
const ca::CallSpec& s = ca::spec_at(i);
Json row = Json::object();
row.set("api", Json(std::string(s.api) + " " + s.version));
row.set("http", Json(s.http_method));
row.set("path", Json(s.path));
row.set("rpc", Json(s.rpc_id));
arr.push(row);
}
Json o = Json::object();
o.set("calls", arr);
return json_out(o);
});
// --- Translation -----------------------------------------------------------
router.post("/google/cloudai/translate", "google-cloudai-translate", [rig](fw::Request req) {
Json b = parse_body(req);
ca::TranslateRequest r;
r.texts = strings(b, "texts", false);
if (r.texts.empty()) r.texts.push_back(text(b, "text", true));
r.target = text(b, "target", true);
r.source = text(b, "source");
r.mime_type = text(b, "mimeType");
r.model = text(b, "model");
r.glossary = text(b, "glossary");
r.glossary_ignore_case = flag(b, "glossaryIgnoreCase");
bool confirm = flag(b, "confirm");
auto t = rig->make<ca::Translation>(b, "global");
return guarded([&] {
Json body = t.translate_body(r);
if (!confirm) return json_out(dry_run("translateText", body));
Json arr = Json::array();
for (const auto& x : t.translate(r)) {
Json o = Json::object();
o.set("text", Json(x.text));
o.set("detectedLanguage", Json(x.detected_language));
o.set("model", Json(x.model));
arr.push(o);
}
Json o = Json::object();
o.set("translations", arr);
return json_out(o);
});
});
router.post("/google/cloudai/translate/detect", "google-cloudai-detect", [rig](fw::Request req) {
Json b = parse_body(req);
std::string content = text(b, "text", true);
bool confirm = flag(b, "confirm");
auto t = rig->make<ca::Translation>(b, "global");
return guarded([&] {
if (!confirm) {
Json body = Json::object();
body.set("content", Json(content));
return json_out(dry_run("detectLanguage", body));
}
return json_out(t.detect_language(content));
});
});
router.post("/google/cloudai/translate/list", "google-cloudai-translate-list", [rig](fw::Request req) {
Json b = parse_body(req);
std::string what = pick(b, "what", {"languages", "glossaries", "adaptiveDatasets", "models", "datasets", "locations"});
std::string display = text(b, "displayLanguage");
auto t = rig->make<ca::Translation>(b, "global");
return guarded([&] {
if (what == "languages") return json_out(t.supported_languages(display));
Json o = Json::object();
if (what == "glossaries") o.set("glossaries", t.list_glossaries());
else if (what == "adaptiveDatasets") o.set("adaptiveMtDatasets", t.list_adaptive_datasets());
else if (what == "models") o.set("models", t.list_models());
else if (what == "datasets") o.set("datasets", t.list_datasets());
else o.set("locations", t.list_locations());
return json_out(o);
});
});
router.post("/google/cloudai/translate/glossary", "google-cloudai-glossary", [rig](fw::Request req) {
Json b = parse_body(req);
std::string action = pick(b, "action", {"get", "create", "delete"});
std::string id = text(b, "id", true);
ca::GlossaryDraft d;
d.id = id;
d.input_uri = text(b, "inputUri");
d.source_language = text(b, "source");
d.target_language = text(b, "target");
d.language_codes = strings(b, "languageCodes", false);
bool confirm = flag(b, "confirm");
auto t = rig->make<ca::Translation>(b, "global");
return guarded([&] {
if (action == "get") return json_out(t.get_glossary(id));
if (action == "create") {
Json body = t.glossary_body(d);
if (!confirm) return json_out(dry_run("glossaries.create", body));
return json_out(operation_json(t.create_glossary_start(d)), 202);
}
t.glossary_name(id);
if (!confirm) {
Json target = Json::object();
target.set("glossary", Json(t.glossary_name(id)));
return json_out(dry_run("glossaries.delete", target));
}
return json_out(operation_json(t.delete_glossary_start(id)), 202);
});
});
// --- Vision ----------------------------------------------------------------
router.post("/google/cloudai/vision/annotate", "google-cloudai-vision", [rig](fw::Request req) {
Json b = parse_body(req);
std::vector<ca::VisionImage> images;
const Json* arr = b.find("images");
if (!arr || !arr->is_array() || arr->as_array().empty()) bad("images is required (array of {content|uri})");
for (const Json& im : arr->as_array()) {
if (!im.is_object()) bad("each image is {content|uri}");
images.push_back({text(im, "content"), text(im, "uri")});
}
auto features = vision_features(b);
bool confirm = flag(b, "confirm");
auto v = rig->make<ca::Vision>(b, "us-west1");
return guarded([&] {
Json body = v.annotate_body(images, features);
if (!confirm) return json_out(dry_run("images.annotate", body));
return json_out(v.annotate(images, features));
});
});
router.post("/google/cloudai/vision/list", "google-cloudai-vision-list", [rig](fw::Request req) {
Json b = parse_body(req);
std::string what = pick(b, "what", {"productSets", "products", "referenceImages"});
std::string product = what == "referenceImages" ? text(b, "productId", true) : "";
auto v = rig->make<ca::Vision>(b, "us-west1");
return guarded([&] {
Json o = Json::object();
if (what == "productSets") o.set("productSets", v.list_product_sets());
else if (what == "products") o.set("products", v.list_products());
else o.set("referenceImages", v.list_reference_images(product));
return json_out(o);
});
});
// --- Natural Language ------------------------------------------------------
router.post("/google/cloudai/language/analyze", "google-cloudai-language", [rig](fw::Request req) {
Json b = parse_body(req);
std::string kind = pick(b, "kind", {"sentiment", "entities", "classify", "moderate", "annotate", "syntax", "entitySentiment"});
ca::NlDocument d = nl_document(b);
std::string encoding = text(b, "encoding");
ca::NlFeatures f;
if (kind == "annotate") {
f.entities = flag(b, "extractEntities");
f.sentiment = flag(b, "extractSentiment");
f.classify = flag(b, "classifyText");
f.moderate = flag(b, "moderateText");
}
bool confirm = flag(b, "confirm");
auto nl = rig->make<ca::NaturalLanguage>(b, "global");
return guarded([&] {
Json doc = nl.document_json(d); // validates before any dry run
if (kind == "annotate" && !f.entities && !f.sentiment && !f.classify && !f.moderate)
bad("annotate needs one of extractEntities, extractSentiment, classifyText, moderateText");
if (!confirm) {
Json body = Json::object();
body.set("document", doc);
return json_out(dry_run(kind.c_str(), body));
}
if (kind == "sentiment") return json_out(nl.analyze_sentiment(d, encoding));
if (kind == "entities") return json_out(nl.analyze_entities(d, encoding));
if (kind == "classify") return json_out(nl.classify_text(d));
if (kind == "moderate") return json_out(nl.moderate_text(d));
if (kind == "syntax") return json_out(nl.analyze_syntax(d, encoding));
if (kind == "entitySentiment") return json_out(nl.analyze_entity_sentiment(d, encoding));
return json_out(nl.annotate_text(d, f, encoding));
});
});
// --- Video Intelligence ----------------------------------------------------
router.post("/google/cloudai/video/annotate", "google-cloudai-video", [rig](fw::Request req) {
Json b = parse_body(req);
ca::VideoRequest r;
r.input_uri = text(b, "inputUri");
r.input_content_base64 = text(b, "content");
r.features = strings(b, "features", true);
r.output_uri = text(b, "outputUri");
r.location_id = text(b, "locationId");
bool confirm = flag(b, "confirm");
auto vi = rig->make<ca::VideoIntelligence>(b, "us-east1");
return guarded([&] {
Json body = vi.annotate_body(r);
if (!confirm) return json_out(dry_run("videos.annotate", body));
return json_out(operation_json(vi.annotate_start(r)), 202);
});
});
// --- Document AI -----------------------------------------------------------
router.post("/google/cloudai/documentai/process", "google-cloudai-docai-process", [rig](fw::Request req) {
Json b = parse_body(req);
ca::ProcessRequest r;
r.processor_id = text(b, "processorId", true);
r.version_id = text(b, "versionId");
r.content_base64 = text(b, "content");
r.gcs_uri = text(b, "gcsUri");
r.mime_type = text(b, "mimeType", true);
r.skip_human_review = flag(b, "skipHumanReview", true);
r.field_mask = text(b, "fieldMask");
bool confirm = flag(b, "confirm");
auto d = rig->make<ca::DocumentAi>(b, "us");
return guarded([&] {
Json body = d.process_body(r);
d.process_target(r);
if (!confirm) return json_out(dry_run("processors.process", body));
return json_out(d.process(r));
});
});
router.post("/google/cloudai/documentai/list", "google-cloudai-docai-list", [rig](fw::Request req) {
Json b = parse_body(req);
std::string what = pick(b, "what", {"processors", "processorTypes", "fetchProcessorTypes", "versions", "schemas", "locations"});
std::string processor = what == "versions" ? text(b, "processorId", true) : "";
auto d = rig->make<ca::DocumentAi>(b, "us");
return guarded([&] {
Json o = Json::object();
if (what == "processors") o.set("processors", d.list_processors());
else if (what == "processorTypes") o.set("processorTypes", d.list_processor_types());
else if (what == "fetchProcessorTypes") return json_out(d.fetch_processor_types());
else if (what == "versions") o.set("processorVersions", d.list_versions(processor));
else if (what == "schemas") o.set("schemas", d.list_schemas());
else o.set("locations", d.list_locations());
return json_out(o);
});
});
// --- Speech v2 -------------------------------------------------------------
router.post("/google/cloudai/speech/recognize", "google-cloudai-speech", [rig](fw::Request req) {
Json b = parse_body(req);
ca::SpeechRequest r;
std::string rec = text(b, "recognizer");
if (!rec.empty()) r.recognizer = rec;
r.content_base64 = text(b, "content");
r.uri = text(b, "uri");
std::string model = text(b, "model");
if (!model.empty()) r.model = model;
r.languages = strings(b, "languages", true);
bool confirm = flag(b, "confirm");
auto s = rig->make<ca::SpeechV2>(b, "us-central1");
return guarded([&] {
Json body = s.recognize_body(r);
s.recognizer_name(r.recognizer);
if (!confirm) return json_out(dry_run("recognizers.recognize", body));
return json_out(s.recognize(r));
});
});
router.post("/google/cloudai/speech/list", "google-cloudai-speech-list", [rig](fw::Request req) {
Json b = parse_body(req);
std::string what = pick(b, "what", {"recognizers", "phraseSets", "customClasses", "config", "locations"});
auto s = rig->make<ca::SpeechV2>(b, "us-central1");
return guarded([&] {
Json o = Json::object();
if (what == "recognizers") o.set("recognizers", s.list_recognizers());
else if (what == "phraseSets") o.set("phraseSets", s.list_phrase_sets());
else if (what == "customClasses") o.set("customClasses", s.list_custom_classes());
else if (what == "config") return json_out(s.get_config());
else o.set("locations", s.list_locations());
return json_out(o);
});
});
// --- Text-to-Speech long audio ---------------------------------------------
router.post("/google/cloudai/tts/long", "google-cloudai-tts-long", [rig](fw::Request req) {
Json b = parse_body(req);
ca::LongAudioRequest r;
r.text = text(b, "text", true);
r.ssml = flag(b, "ssml");
r.language = text(b, "language", true);
r.voice_name = text(b, "voice");
std::string enc = text(b, "encoding");
if (!enc.empty()) r.encoding = enc;
r.output_uri = text(b, "outputUri", true);
bool confirm = flag(b, "confirm");
auto la = rig->make<ca::LongAudio>(b, "global");
return guarded([&] {
Json body = la.request_body(r);
if (!confirm) return json_out(dry_run("synthesizeLongAudio", body));
return json_out(operation_json(la.synthesize_start(r)), 202);
});
});
router.post("/google/cloudai/tts/voices", "google-cloudai-tts-voices", [rig](fw::Request req) {
Json b = parse_body(req);
std::string language = text(b, "language");
auto la = rig->make<ca::LongAudio>(b, "global");
return guarded([&] { return json_out(la.voices(language)); });
});
// --- Operations (read) -----------------------------------------------------
router.post("/google/cloudai/operation", "google-cloudai-operation", [rig](fw::Request req) {
Json b = parse_body(req);
std::string api = pick(b, "api", {"translate", "vision", "video", "documentai", "speech", "tts"});
std::string name = text(b, "name", true);
auto s = rig->make<ca::CloudAiService>(b, "global");
ca::Call get = api == "translate" ? ca::Call::kTranslateLocationsOperationsGet
: api == "vision" ? ca::Call::kVisionProjectsLocationsOperationsGet
: api == "video" ? ca::Call::kVideoLocationsOperationsGet
: api == "documentai" ? ca::Call::kDocumentaiLocationsOperationsGet
: api == "speech" ? ca::Call::kSpeechLocationsOperationsGet
: ca::Call::kTtsLocationsOperationsGet;
return guarded([&] { return json_out(operation_json(s.operation(get, name))); });
});
// --- Any row of the table --------------------------------------------------
// Reads (GET) run at once; every other method is a dry run until confirm.
router.post("/google/cloudai/call", "google-cloudai-call", [rig](fw::Request req) {
Json b = parse_body(req);
std::string rpc = text(b, "rpc", true);
const ca::CallSpec* spec = ca::find_rpc(rpc);
if (!spec) bad("unknown rpc; see GET /google/cloudai/calls");
std::string name = text(b, "name");
const Json* body = b.find("body");
if (body && body->is_null()) body = nullptr;
ca::Query query;
if (const Json* q = b.find("query")) {
if (!q->is_object()) bad("query must be an object of strings");
for (const auto& [k, v] : q->as_object()) {
if (!v.is_string()) bad("query values must be strings");
query.emplace_back(k, v.as_string());
}
}
bool confirm = flag(b, "confirm");
ca::Call call = spec->call;
auto s = rig->make<ca::CloudAiService>(b, "global");
return guarded([&] {
std::string path = ca::render(call, name) + ca::query_of(call, query);
ca::check_body(call, body);
bool read = std::string(spec->http_method) == "GET";
if (!read && !confirm) {
Json o = dry_run(spec->rpc_id, body ? *body : Json::object());
o.set("http", Json(spec->http_method));
o.set("url", Json(ca::endpoint_of(call, name).base_url + path));
return json_out(o);
}
return json_out(s.invoke(call, name, body, query));
});
});
} }
} // namespace molo::example::app } // namespace molo::example::app
+66
View File
@@ -0,0 +1,66 @@
// Cloud AI page: thin client over /google/cloudai/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("ca-out"), err = $("ca-error"), sel = $("ca-route"), box = $("ca-body");
// One example body per route. Paid calls and writes stay dry runs until
// "confirm": true is added.
var routes = [
["translate", "Translate text (paid)", { texts: ["Hello world"], target: "nl" }],
["translate/detect", "Detect language (paid)", { text: "Bonjour tout le monde" }],
["translate/list", "Translation: languages, glossaries, models, datasets", { what: "languages", displayLanguage: "en" }],
["translate/glossary", "Translation: glossary get, create, delete", { action: "get", id: "GLOSSARY_ID" }],
["vision/annotate", "Vision: annotate images (paid)", { images: [{ uri: "gs://BUCKET/image.png" }], features: [{ type: "LABEL_DETECTION", maxResults: 5 }] }],
["vision/list", "Vision: product sets, products", { what: "productSets" }],
["language/analyze", "Natural Language: analyze text (paid)", { kind: "sentiment", content: "I love this product." }],
["video/annotate", "Video Intelligence: annotate (paid, starts an operation)", { inputUri: "gs://BUCKET/video.mp4", features: ["LABEL_DETECTION"] }],
["documentai/process", "Document AI: process (paid)", { processorId: "PROCESSOR_ID", gcsUri: "gs://BUCKET/doc.pdf", mimeType: "application/pdf" }],
["documentai/list", "Document AI: processors, types, versions", { what: "processors" }],
["speech/recognize", "Speech v2: recognize (paid)", { languages: ["en-US"], uri: "gs://BUCKET/audio.wav", model: "chirp_2" }],
["speech/list", "Speech v2: recognizers, phrase sets", { what: "recognizers" }],
["tts/long", "Text-to-Speech: long audio (paid, starts an operation)", { text: "Hello there.", language: "en-US", outputUri: "gs://BUCKET/out.wav" }],
["tts/voices", "Text-to-Speech: voices", { language: "en-US" }],
["operation", "Read an operation", { api: "video", name: "projects/P/locations/us-east1/operations/ID" }],
["call", "Any call of the table (see /google/cloudai/calls)", { rpc: "translate.projects.locations.glossaries.list", name: "projects/P/locations/global" }]
];
routes.forEach(function (r) {
var o = document.createElement("option");
o.value = r[0];
o.textContent = r[1];
sel.appendChild(o);
});
function fill() {
for (var i = 0; i < routes.length; i++) {
if (routes[i][0] === sel.value) { box.value = JSON.stringify(routes[i][2], null, 1); return; }
}
}
sel.onchange = fill;
fill();
fetch("/google/cloudai/status").then(function (r) { return r.json(); }).then(function (s) {
$("ca-config").textContent = s.configured
? "Google credential found" + (s.project_set ? "." : "; set GOOGLE_CLOUD_PROJECT.")
: "No Google credential: calls answer 503 until one is configured.";
});
$("ca-send").onclick = function () {
var body;
err.textContent = "";
try { body = JSON.parse(box.value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
out.textContent = "...";
fetch("/google/cloudai/" + sel.value, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (res) {
return res.text().then(function (t) {
var j;
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
else { out.textContent = JSON.stringify(j, null, 2); }
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
};
})();
+189
View File
@@ -0,0 +1,189 @@
# Cloud AI (Translation, Vision, Natural Language, Video Intelligence, Document AI, Speech v2, long-audio TTS) - Specification
Owned directory: `src/google/cloudai/`. Namespace `molo::google::cloudai`.
Depends on the `src/google` foundation (`GoogleClient`, `Service`,
`operation.h`, `resource.h`). Plan: `plans/18-breadth.md` section 1.
## 1. Scope
Seven Google APIs, every method of Discovery wrapped as a row of one call
table (176 rows), with typed classes for the common paths:
| API | Discovery | Rows | Typed class |
|---|---|---|---|
| Cloud Translation | `translate` v3 | 51 | `Translation` (translate, detect, supported languages, romanize, glossaries, adaptive MT, models, datasets) |
| Cloud Vision | `vision` v1 | 38 | `Vision` (images and files annotate, product search reads and writes) |
| Natural Language | `language` v2 + two v1-only methods | 7 | `NaturalLanguage` (sentiment, entities, classify, moderate, annotateText; v1 entity sentiment and syntax) |
| Video Intelligence | `videointelligence` v1 | 5 | `VideoIntelligence` (annotate start, operations) |
| Document AI | `documentai` v1 | 42 | `DocumentAi` (process, processors, types, versions, schemas, batch start) |
| Speech-to-Text v2 (Chirp) | `speech` v2 | 26 | `SpeechV2` (recognize, recognizers, phrase sets, custom classes, config) |
| Text-to-Speech | `texttospeech` v1 | 7 | `LongAudio` (synthesizeLongAudio, voices, operations) |
Every row not covered by a typed method is still reachable and validated
through `CloudAiService::invoke(Call, name, body, query)` and the `call()`
member of each typed class.
Media Translation: not wrapped. It is not in Google's Discovery directory
(`discovery.googleapis.com/discovery/v1/apis`, 2026-10-25) and its Discovery
document answers 502, so no call can be derived (rule: never invent an
endpoint). It is the only item of the plan row left out; listed in `## Gaps`.
Short text synthesis and the v1 speech recognizer stay in the foundation
(`text_to_speech.h`, `speech_to_text.h`).
## 2. Module map
| File | Concern |
|---|---|
| `caps.h` | Static ceilings (`CloudAiCaps`) |
| `call_enum.inc`, `calls_table.inc` | Generated from Discovery by `tools/check_discovery.py --generate`; do not edit |
| `calls.h/.cpp` | `Call`, `CallSpec`, `render`, `query_of`, `check_body`, `endpoint_of`, `valid_enum`, the SCHEMAS / ENUMS / ENDPOINTS tables |
| `cloudai_service.h/.cpp` | `CloudAiService`: `invoke`, `list`, `start`, `operation`, `wait`; shared input checks |
| `translation.h/.cpp` | `Translation` |
| `vision.h/.cpp` | `Vision` |
| `natural_language.h/.cpp` | `NaturalLanguage` |
| `video_intelligence.h/.cpp` | `VideoIntelligence` |
| `document_ai.h/.cpp` | `DocumentAi` |
| `speech_v2.h/.cpp` | `SpeechV2` |
| `long_audio.h/.cpp` | `LongAudio` |
| `tools/check_discovery.py` | Generator and checker against live Discovery |
## 3. Static caps (caps.h)
| Knob | Value |
|---|---|
| Resource name | 512 chars, each segment 1..128 of `[A-Za-z0-9._~-]`, never `.` or `..` |
| Resource id (glossary, processor, recognizer, product) | 63 chars |
| Request body (serialized) | 16 MiB |
| Inline media (image, document, audio, video) | 10 MiB decoded |
| Query parameters | 20, value 2048 chars |
| List pages followed | 20 pages of 50 |
| Translate texts / characters | 1024 texts, 30000 characters per request |
| Natural Language document | 1,000,000 bytes |
| Vision images / features / maxResults | 16 / 16 / 100; file pages 5 |
| Video features | 9 |
| Long audio text | 1,000,000 bytes |
| Operation wait | foundation `OperationCaps` (600 s, 400 polls); the demo never waits |
| Demo request body / shown response | 12 MiB / 1 MiB (larger shows a 64 KiB preview) |
## 4. Contracts
- Validation first. A resource name must match the Discovery `pattern` of the
row's path variable (literal segments, `[^/]+`, trailing `.*` only); body
top-level fields must be request fields of the row; query keys must be
query parameters of the row. All of it throws `GoogleError::invalid`
(HTTP 400 `INVALID_ARGUMENT`) before the credential check and before any
traffic. No credential then throws `NotConfiguredError` (503).
- Endpoints: `translation.googleapis.com/v3` (location only in the path),
`vision.googleapis.com/v1`, `language.googleapis.com/v2` (and `/v1`),
`videointelligence.googleapis.com/v1`, `texttospeech.googleapis.com/v1`.
Document AI and Speech use `https://<api>.<location>.rep.googleapis.com`
when Discovery lists the location in its `endpoints`; Speech `us` and `eu`
use `https://<loc>-speech.googleapis.com` (product docs, see Unverified).
- Long-running calls (`*_start`, `batch_process_start`,
`synthesize_start`, `annotate_start`) answer the `Operation` and never wait.
`wait()` polls with the foundation caps.
- Writes and deletes are explicit methods without defaults
(`create_glossary_start`, `delete_glossary_start`, `create_product_set`,
`delete_product`, `create_processor`, `delete_processor_start`,
`create_recognizer_start`, `delete_recognizer_start`, `cancel_operation`).
- Paid calls: translate, detect, adaptive translate, annotate (Vision),
NL analyze, video annotate, process, recognize, synthesize.
## 5. Example routes (`/google/cloudai`)
GET `/google/cloudai` (page), `/status`, `/calls` answer 2xx without a
credential. Everything else is POST: 422 on bad input (checked before the
credential), 503 `google_not_configured` without a credential. Paid calls and
every write or delete are dry runs (200, `dry_run:true`, request shown with
long strings redacted) unless the body has `"confirm":true`. Long-running
starts answer 202 with the operation name. Optional `location` in every body
(default per API: translate `global`, vision `us-west1`, document AI `us`,
speech `us-central1`, video `us-east1`, TTS `global`); project from
`GOOGLE_CLOUD_PROJECT`.
Routes: `translate`, `translate/detect`, `translate/list`,
`translate/glossary`, `vision/annotate`, `vision/list`, `language/analyze`,
`video/annotate`, `documentai/process`, `documentai/list`,
`speech/recognize`, `speech/list`, `tts/long`, `tts/voices`, `operation`,
`call` (any row by rpc id; GET rows run, others dry-run until confirm).
Excluded from locust.
## Required APIs
Enable these on the project (placeholder project `example-214512124`). A disabled API answers 403 `SERVICE_DISABLED`.
- `translate.googleapis.com`: https://console.developers.google.com/apis/api/translate.googleapis.com/overview?project=example-214512124
- `vision.googleapis.com`: https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=example-214512124
- `language.googleapis.com`: https://console.developers.google.com/apis/api/language.googleapis.com/overview?project=example-214512124
- `videointelligence.googleapis.com`: https://console.developers.google.com/apis/api/videointelligence.googleapis.com/overview?project=example-214512124
- `documentai.googleapis.com`: https://console.developers.google.com/apis/api/documentai.googleapis.com/overview?project=example-214512124
- `speech.googleapis.com`: https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=example-214512124
- `texttospeech.googleapis.com`: https://console.developers.google.com/apis/api/texttospeech.googleapis.com/overview?project=example-214512124
Scope: `cloud-platform` for all (service account token as in the other
modules). Translation, Vision, Natural Language, Speech and TTS also accept an
API key; Document AI and Video Intelligence need OAuth in practice (unverified).
## Sources
- Discovery documents, fetched 2026-10-25: `https://<service>.googleapis.com/$discovery/rest?version=<v>`
for `translate` v3 (rev 20260901), `vision` v1 (20260918), `language` v2 and
v1 (20261019), `videointelligence` v1 (20260428), `documentai` v1
(20261019), `speech` v2 (20261001), `texttospeech` v1 (20260827).
- Directory: `https://discovery.googleapis.com/discovery/v1/apis` (no
`mediatranslation`).
- Product docs for semantics: Cloud Translation v3, Vision API, Natural
Language, Video Intelligence, Document AI regional endpoints, Speech-to-Text
v2 / Chirp, Text-to-Speech long audio.
## Verified against discovery
`python3 -I src/google/cloudai/tools/check_discovery.py` (2026-10-25): 176
rows (HTTP method, path, request fields, query parameters, path variable and
pattern), full coverage of the Discovery methods of the seven APIs (a new
method fails the check until wrapped), all SCHEMAS field groups, the enum sets
(vision features, video features, NL document type and encoding, TTS encoding
and gender) and the regional locations of Document AI and Speech.
## Verified live
Service account of project `lisa-448004`, read-only, 2026-10-25
(`tests/src/google/cloudai/test_cloudai_live.cpp`, `MOLO_LIVE_NET=1`):
| Call | Result |
|---|---|
| Vision `locations.productSets.list` (us-west1) | 200, 0 items |
| Speech v2 `recognizers.list` (us-central1) | 200, 0 items |
| Text-to-Speech `voices.list` (nl-NL) | 200, 34 voices |
| Translation `getSupportedLanguages`, `glossaries.list`, `adaptiveMtDatasets.list`, `models.list` | 403 `PERMISSION_DENIED`: Cloud Translation API has not been used / is disabled (API NOT ENABLED) |
| Document AI `processors.list`, `processorTypes.list` (us) | 403: Cloud Document AI API not enabled |
| Video Intelligence `operations.list` | 403: Cloud Video Intelligence API not enabled |
The route, auth, regional host selection (Vision, Speech, TTS) and error
mapping are proven by the three 200s. The three disabled APIs are ready to run
once an owner enables them. No paid request was made (the single allowed one,
a 5 character `translateText`, needs `MOLO_LIVE_PAID=1` and the Translation
API enabled). Natural Language was not called live (paid per document).
## Unverified
- Response shapes are read tolerantly from Discovery schema names; only
`Translation::translate` and the list helpers parse fields.
- Speech multi-region hosts `us-speech.googleapis.com` / `eu-speech.googleapis.com`
come from the product documentation, not Discovery.
- Video `locationId` values (us-east1, us-west1, europe-west1, asia-east1) and
product categories of Vision product search come from product docs.
- Document AI `.rep.googleapis.com` hosts are Discovery-listed but not called
live with a processor in this project.
- Which APIs accept an API key versus OAuth only is not probed.
- Nothing is created, changed or deleted live; all writes are verified by
Discovery and by request-shape tests only.
## Gaps
- Media Translation: no Discovery document, not wrapped.
- Typed response classes: replies are returned as `Json` except translate.
- Streaming Speech recognition (gRPC) and Document AI `humanReviewConfig`
flows beyond the raw call are not typed.
- Upload of media is inline base64 or `gs://` only.
+177
View File
@@ -0,0 +1,177 @@
// Generated by tools/check_discovery.py --generate. Do not edit.
kTranslateDetectLanguage,
kTranslateGetSupportedLanguages,
kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesAdaptiveMtSentencesList,
kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesDelete,
kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesGet,
kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesList,
kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtSentencesList,
kTranslateLocationsAdaptiveMtDatasetsCreate,
kTranslateLocationsAdaptiveMtDatasetsDelete,
kTranslateLocationsAdaptiveMtDatasetsGet,
kTranslateLocationsAdaptiveMtDatasetsImportAdaptiveMtFile,
kTranslateLocationsAdaptiveMtDatasetsList,
kTranslateLocationsAdaptiveMtTranslate,
kTranslateLocationsBatchTranslateDocument,
kTranslateLocationsBatchTranslateText,
kTranslateLocationsDatasetsCreate,
kTranslateLocationsDatasetsDelete,
kTranslateLocationsDatasetsExamplesList,
kTranslateLocationsDatasetsExportData,
kTranslateLocationsDatasetsGet,
kTranslateLocationsDatasetsImportData,
kTranslateLocationsDatasetsList,
kTranslateLocationsDetectLanguage,
kTranslateLocationsGet,
kTranslateLocationsGetSupportedLanguages,
kTranslateLocationsGlossariesCreate,
kTranslateLocationsGlossariesDelete,
kTranslateLocationsGlossariesGet,
kTranslateLocationsGlossariesGlossaryEntriesCreate,
kTranslateLocationsGlossariesGlossaryEntriesDelete,
kTranslateLocationsGlossariesGlossaryEntriesGet,
kTranslateLocationsGlossariesGlossaryEntriesList,
kTranslateLocationsGlossariesGlossaryEntriesPatch,
kTranslateLocationsGlossariesList,
kTranslateLocationsGlossariesPatch,
kTranslateLocationsList,
kTranslateLocationsModelsCreate,
kTranslateLocationsModelsDelete,
kTranslateLocationsModelsGet,
kTranslateLocationsModelsList,
kTranslateLocationsOperationsCancel,
kTranslateLocationsOperationsDelete,
kTranslateLocationsOperationsGet,
kTranslateLocationsOperationsList,
kTranslateLocationsOperationsWait,
kTranslateLocationsRefineText,
kTranslateLocationsRomanizeText,
kTranslateLocationsTranslateDocument,
kTranslateLocationsTranslateText,
kTranslateRomanizeText,
kTranslateTranslateText,
kVisionFilesAnnotate,
kVisionFilesAsyncBatchAnnotate,
kVisionImagesAnnotate,
kVisionImagesAsyncBatchAnnotate,
kVisionLocationsOperationsGet,
kVisionOperationsCancel,
kVisionOperationsDelete,
kVisionOperationsGet,
kVisionOperationsList,
kVisionProjectsFilesAnnotate,
kVisionProjectsFilesAsyncBatchAnnotate,
kVisionProjectsImagesAnnotate,
kVisionProjectsImagesAsyncBatchAnnotate,
kVisionLocationsFilesAnnotate,
kVisionLocationsFilesAsyncBatchAnnotate,
kVisionLocationsImagesAnnotate,
kVisionLocationsImagesAsyncBatchAnnotate,
kVisionProjectsLocationsOperationsGet,
kVisionLocationsProductSetsAddProduct,
kVisionLocationsProductSetsCreate,
kVisionLocationsProductSetsDelete,
kVisionLocationsProductSetsGet,
kVisionLocationsProductSetsImport,
kVisionLocationsProductSetsList,
kVisionLocationsProductSetsPatch,
kVisionLocationsProductSetsProductsList,
kVisionLocationsProductSetsRemoveProduct,
kVisionLocationsProductsCreate,
kVisionLocationsProductsDelete,
kVisionLocationsProductsGet,
kVisionLocationsProductsList,
kVisionLocationsProductsPatch,
kVisionLocationsProductsPurge,
kVisionLocationsProductsReferenceImagesCreate,
kVisionLocationsProductsReferenceImagesDelete,
kVisionLocationsProductsReferenceImagesGet,
kVisionLocationsProductsReferenceImagesList,
kVisionProjectsOperationsGet,
kLanguageDocumentsAnalyzeEntities,
kLanguageDocumentsAnalyzeSentiment,
kLanguageDocumentsAnnotateText,
kLanguageDocumentsClassifyText,
kLanguageDocumentsModerateText,
kLanguageV1DocumentsAnalyzeEntitySentiment,
kLanguageV1DocumentsAnalyzeSyntax,
kVideoLocationsOperationsCancel,
kVideoLocationsOperationsDelete,
kVideoLocationsOperationsGet,
kVideoLocationsOperationsList,
kVideoVideosAnnotate,
kDocumentaiOperationsDelete,
kDocumentaiLocationsFetchProcessorTypes,
kDocumentaiLocationsGet,
kDocumentaiLocationsList,
kDocumentaiLocationsOperationsCancel,
kDocumentaiLocationsOperationsGet,
kDocumentaiLocationsOperationsList,
kDocumentaiLocationsProcessorTypesGet,
kDocumentaiLocationsProcessorTypesList,
kDocumentaiLocationsProcessorsBatchProcess,
kDocumentaiLocationsProcessorsCreate,
kDocumentaiLocationsProcessorsDelete,
kDocumentaiLocationsProcessorsDisable,
kDocumentaiLocationsProcessorsEnable,
kDocumentaiLocationsProcessorsGet,
kDocumentaiLocationsProcessorsHumanReviewConfigReviewDocument,
kDocumentaiLocationsProcessorsList,
kDocumentaiLocationsProcessorsProcess,
kDocumentaiLocationsProcessorsProcessorVersionsBatchProcess,
kDocumentaiLocationsProcessorsProcessorVersionsDelete,
kDocumentaiLocationsProcessorsProcessorVersionsDeploy,
kDocumentaiLocationsProcessorsProcessorVersionsEvaluateProcessorVersion,
kDocumentaiLocationsProcessorsProcessorVersionsEvaluationsGet,
kDocumentaiLocationsProcessorsProcessorVersionsEvaluationsList,
kDocumentaiLocationsProcessorsProcessorVersionsGet,
kDocumentaiLocationsProcessorsProcessorVersionsList,
kDocumentaiLocationsProcessorsProcessorVersionsProcess,
kDocumentaiLocationsProcessorsProcessorVersionsTrain,
kDocumentaiLocationsProcessorsProcessorVersionsUndeploy,
kDocumentaiLocationsProcessorsSetDefaultProcessorVersion,
kDocumentaiLocationsSchemasCreate,
kDocumentaiLocationsSchemasDelete,
kDocumentaiLocationsSchemasGet,
kDocumentaiLocationsSchemasList,
kDocumentaiLocationsSchemasPatch,
kDocumentaiLocationsSchemasSchemaVersionsCreate,
kDocumentaiLocationsSchemasSchemaVersionsDelete,
kDocumentaiLocationsSchemasSchemaVersionsGenerate,
kDocumentaiLocationsSchemasSchemaVersionsGet,
kDocumentaiLocationsSchemasSchemaVersionsList,
kDocumentaiLocationsSchemasSchemaVersionsPatch,
kDocumentaiOperationsGet,
kSpeechLocationsConfigGet,
kSpeechLocationsConfigUpdate,
kSpeechLocationsCustomClassesCreate,
kSpeechLocationsCustomClassesDelete,
kSpeechLocationsCustomClassesGet,
kSpeechLocationsCustomClassesList,
kSpeechLocationsCustomClassesPatch,
kSpeechLocationsCustomClassesUndelete,
kSpeechLocationsGet,
kSpeechLocationsList,
kSpeechLocationsOperationsGet,
kSpeechLocationsOperationsList,
kSpeechLocationsPhraseSetsCreate,
kSpeechLocationsPhraseSetsDelete,
kSpeechLocationsPhraseSetsGet,
kSpeechLocationsPhraseSetsList,
kSpeechLocationsPhraseSetsPatch,
kSpeechLocationsPhraseSetsUndelete,
kSpeechLocationsRecognizersBatchRecognize,
kSpeechLocationsRecognizersCreate,
kSpeechLocationsRecognizersDelete,
kSpeechLocationsRecognizersGet,
kSpeechLocationsRecognizersList,
kSpeechLocationsRecognizersPatch,
kSpeechLocationsRecognizersRecognize,
kSpeechLocationsRecognizersUndelete,
kTtsOperationsCancel,
kTtsOperationsDelete,
kTtsLocationsOperationsGet,
kTtsLocationsOperationsList,
kTtsLocationsSynthesizeLongAudio,
kTtsTextSynthesize,
kTtsVoicesList,
+281
View File
@@ -0,0 +1,281 @@
#include "google/cloudai/calls.h"
#include <cstring>
#include "clients/url.h"
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
static const CallSpec kCalls[] = {
#include "google/cloudai/calls_table.inc"
};
namespace {
// Regional hosts https://<api>.<location>.rep.googleapis.com (checked against
// the Discovery `endpoints` list by tools/check_discovery.py).
struct RegionalHosts {
const char* api;
const char* version;
const char* locations;
};
// ENDPOINTS-BEGIN
const RegionalHosts kRegional[] = {
{"documentai", "v1", "us,eu,asia-south1,asia-southeast1,northamerica-northeast1,australia-southeast1,europe-west2,europe-west3"},
{"speech", "v2", "us-central1,us-west1,me-west1,europe-west1,europe-west2,europe-west3,europe-west4"},
};
// ENDPOINTS-END
// Schema fields the typed classes put into request bodies; every one is
// checked against Discovery by tools/check_discovery.py.
struct SchemaFields {
const char* api;
const char* version;
const char* schema;
const char* fields;
};
// SCHEMAS-BEGIN
const SchemaFields kSchemas[] = {
{"translate", "v3", "TranslateTextRequest", "contents,mimeType,sourceLanguageCode,targetLanguageCode,model,glossaryConfig,labels"},
{"translate", "v3", "DetectLanguageRequest", "content,mimeType,model,labels"},
{"translate", "v3", "RomanizeTextRequest", "contents,sourceLanguageCode"},
{"translate", "v3", "Glossary", "name,languagePair,languageCodesSet,inputConfig,displayName"},
{"translate", "v3", "TranslateTextGlossaryConfig", "glossary,ignoreCase"},
{"vision", "v1", "AnnotateImageRequest", "image,features,imageContext"},
{"vision", "v1", "BatchAnnotateImagesRequest", "requests,parent"},
{"vision", "v1", "Image", "content,source"},
{"vision", "v1", "ImageSource", "imageUri,gcsImageUri"},
{"vision", "v1", "Feature", "type,maxResults,model"},
{"vision", "v1", "Product", "name,displayName,description,productCategory,productLabels"},
{"language", "v2", "Document", "type,content,gcsContentUri,languageCode"},
{"language", "v2", "AnalyzeSentimentRequest", "document,encodingType"},
{"language", "v2", "AnalyzeEntitiesRequest", "document,encodingType"},
{"language", "v2", "ClassifyTextRequest", "document"},
{"language", "v2", "ModerateTextRequest", "document"},
{"language", "v1", "AnalyzeSyntaxRequest", "document,encodingType"},
{"language", "v1", "AnalyzeEntitySentimentRequest", "document,encodingType"},
{"videointelligence", "v1", "GoogleCloudVideointelligenceV1_AnnotateVideoRequest", "inputUri,inputContent,features,locationId,outputUri"},
{"documentai", "v1", "GoogleCloudDocumentaiV1ProcessRequest", "rawDocument,inlineDocument,gcsDocument,skipHumanReview,fieldMask"},
{"documentai", "v1", "GoogleCloudDocumentaiV1RawDocument", "content,mimeType,displayName"},
{"documentai", "v1", "GoogleCloudDocumentaiV1Processor", "displayName,type"},
{"speech", "v2", "RecognizeRequest", "config,configMask,content,uri"},
{"speech", "v2", "RecognitionConfig", "autoDecodingConfig,explicitDecodingConfig,model,languageCodes,features"},
{"texttospeech", "v1", "SynthesizeLongAudioRequest", "input,audioConfig,outputGcsUri,voice"},
{"translate", "v3", "GcsSource", "inputUri"},
{"translate", "v3", "LanguageCodesSet", "languageCodes"},
{"translate", "v3", "LanguageCodePair", "sourceLanguageCode,targetLanguageCode"},
{"vision", "v1", "GcsSource", "uri"},
{"vision", "v1", "ProductSet", "displayName"},
{"documentai", "v1", "GoogleCloudDocumentaiV1GcsDocument", "gcsUri,mimeType"},
{"speech", "v2", "Recognizer", "model,languageCodes"},
{"texttospeech", "v1", "SynthesisInput", "text,ssml"},
{"texttospeech", "v1", "VoiceSelectionParams", "languageCode,name,ssmlGender"},
{"texttospeech", "v1", "AudioConfig", "audioEncoding,speakingRate,pitch,sampleRateHertz"},
{"translate", "v3", "TranslateTextResponse", "translations,glossaryTranslations"},
{"translate", "v3", "Translation", "translatedText,detectedLanguageCode,model"},
{"translate", "v3", "DetectLanguageResponse", "languages"},
{"translate", "v3", "SupportedLanguages", "languages"},
{"translate", "v3", "ListGlossariesResponse", "glossaries,nextPageToken"},
{"translate", "v3", "ListAdaptiveMtDatasetsResponse", "adaptiveMtDatasets,nextPageToken"},
{"translate", "v3", "ListModelsResponse", "models,nextPageToken"},
{"translate", "v3", "AdaptiveMtTranslateRequest", "dataset,content,mimeType"},
{"vision", "v1", "BatchAnnotateImagesResponse", "responses"},
{"vision", "v1", "ListProductSetsResponse", "productSets,nextPageToken"},
{"vision", "v1", "ListProductsResponse", "products,nextPageToken"},
{"vision", "v1", "AnnotateFileRequest", "inputConfig,features,pages"},
{"vision", "v1", "InputConfig", "gcsSource,mimeType,content"},
{"language", "v2", "AnnotateTextRequest", "document,features,encodingType"},
{"language", "v2", "AnnotateTextRequestFeatures", "extractEntities,extractDocumentSentiment,classifyText,moderateText"},
{"documentai", "v1", "GoogleCloudDocumentaiV1ListProcessorsResponse", "processors,nextPageToken"},
{"documentai", "v1", "GoogleCloudDocumentaiV1ListProcessorTypesResponse", "processorTypes,nextPageToken"},
{"documentai", "v1", "GoogleCloudDocumentaiV1ProcessResponse", "document,humanReviewStatus"},
{"documentai", "v1", "GoogleCloudDocumentaiV1ListProcessorVersionsResponse", "processorVersions,nextPageToken"},
{"speech", "v2", "ListRecognizersResponse", "recognizers,nextPageToken"},
{"speech", "v2", "ListPhraseSetsResponse", "phraseSets,nextPageToken"},
{"speech", "v2", "ListCustomClassesResponse", "customClasses,nextPageToken"},
{"speech", "v2", "RecognizeResponse", "results,metadata"},
{"speech", "v2", "ExplicitDecodingConfig", "encoding,sampleRateHertz,audioChannelCount"},
};
// SCHEMAS-END
// Enum values the typed classes accept (UNSPECIFIED values left out); every
// set must equal the Discovery enum, checked by tools/check_discovery.py.
struct EnumSet {
const char* api;
const char* version;
const char* schema_field;
const char* values;
};
// ENUMS-BEGIN
const EnumSet kEnums[] = {
{"vision", "v1", "Feature.type", "FACE_DETECTION,LANDMARK_DETECTION,LOGO_DETECTION,LABEL_DETECTION,TEXT_DETECTION,DOCUMENT_TEXT_DETECTION,SAFE_SEARCH_DETECTION,IMAGE_PROPERTIES,CROP_HINTS,WEB_DETECTION,PRODUCT_SEARCH,OBJECT_LOCALIZATION"},
{"videointelligence", "v1", "GoogleCloudVideointelligenceV1_AnnotateVideoRequest.features", "LABEL_DETECTION,SHOT_CHANGE_DETECTION,EXPLICIT_CONTENT_DETECTION,FACE_DETECTION,SPEECH_TRANSCRIPTION,TEXT_DETECTION,OBJECT_TRACKING,LOGO_RECOGNITION,PERSON_DETECTION"},
{"language", "v2", "Document.type", "PLAIN_TEXT,HTML"},
{"language", "v2", "AnnotateTextRequest.encodingType", "NONE,UTF8,UTF16,UTF32"},
{"texttospeech", "v1", "AudioConfig.audioEncoding", "LINEAR16,MP3,OGG_OPUS,MULAW,ALAW,PCM,M4A"},
{"texttospeech", "v1", "VoiceSelectionParams.ssmlGender", "MALE,FEMALE,NEUTRAL"},
};
// ENUMS-END
std::vector<std::string> split(const std::string& s, char sep) {
std::vector<std::string> out;
std::size_t start = 0;
for (;;) {
std::size_t at = s.find(sep, start);
if (at == std::string::npos) {
out.push_back(s.substr(start));
return out;
}
out.push_back(s.substr(start, at - start));
start = at + 1;
}
}
// Discovery patterns here are only literal segments, [^/]+ and a trailing .*.
bool matches(const std::string& pattern_raw, const std::string& name) {
std::string pattern = pattern_raw;
if (!pattern.empty() && pattern.front() == '^') pattern.erase(0, 1);
if (!pattern.empty() && pattern.back() == '$') pattern.pop_back();
// "[^/]+" holds a slash: swap it for a one-character token before splitting.
for (std::size_t at = pattern.find("[^/]+"); at != std::string::npos; at = pattern.find("[^/]+", at + 1))
pattern.replace(at, 5, "*");
std::vector<std::string> ps = split(pattern, '/');
std::vector<std::string> ns = split(name, '/');
for (std::size_t i = 0; i < ps.size(); ++i) {
if (ps[i] == ".*") return i < ns.size() && i + 1 == ps.size();
if (i >= ns.size()) return false;
if (ps[i] == "*") {
if (!valid_name_segment(ns[i])) return false;
} else if (ps[i] != ns[i]) {
return false;
}
}
return ps.size() == ns.size();
}
bool has_field(const char* list, const std::string& key) {
std::string l = std::string(",") + list + ",";
return l.find("," + key + ",") != std::string::npos;
}
std::string location_of(const std::string& name) {
std::vector<std::string> segs = split(name, '/');
for (std::size_t i = 0; i + 1 < segs.size(); ++i)
if (segs[i] == "locations") return segs[i + 1];
return "";
}
} // namespace
std::size_t call_count() { return sizeof(kCalls) / sizeof(kCalls[0]); }
bool valid_enum(const std::string& schema_field, const std::string& value) {
for (const EnumSet& e : kEnums)
if (schema_field == e.schema_field)
return (std::string(",") + e.values + ",").find("," + value + ",") != std::string::npos;
return false;
}
std::size_t schema_count() { return sizeof(kSchemas) / sizeof(kSchemas[0]); }
const CallSpec& spec_at(std::size_t index) {
if (index >= call_count()) throw GoogleError::invalid("no such call");
return kCalls[index];
}
const CallSpec& spec(Call call) { return spec_at(static_cast<std::size_t>(call)); }
const CallSpec* find_rpc(const std::string& rpc_id) {
for (const CallSpec& s : kCalls)
if (rpc_id == s.rpc_id) return &s;
return nullptr;
}
clients::Method method_of(Call call) {
const char* m = spec(call).http_method;
if (!std::strcmp(m, "GET")) return clients::Method::kGet;
if (!std::strcmp(m, "POST")) return clients::Method::kPost;
if (!std::strcmp(m, "PATCH")) return clients::Method::kPatch;
if (!std::strcmp(m, "DELETE")) return clients::Method::kDelete;
throw GoogleError::invalid("unsupported http method");
}
bool valid_name_segment(const std::string& s) {
if (s.empty() || s.size() > CloudAiCaps::kMaxSegmentChars) return false;
if (s == "." || s == "..") return false;
for (char c : s)
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~'))
return false;
return true;
}
std::string render(Call call, const std::string& name) {
const CallSpec& s = spec(call);
std::string path = s.path;
path.erase(0, path.find('/') + 1); // version prefix
if (!*s.path_var) {
if (!name.empty()) throw GoogleError::invalid("this call takes no resource name");
return "/" + path;
}
if (name.empty() || name.size() > CloudAiCaps::kMaxNameChars)
throw GoogleError::invalid("resource name is missing or too long");
if (!matches(s.pattern, name))
throw GoogleError::invalid(std::string("resource name does not match ") + s.pattern);
std::size_t open = path.find('{');
std::size_t close = path.find('}', open);
path.replace(open, close - open + 1, name);
return "/" + path;
}
std::string query_of(Call call, const Query& query) {
if (query.size() > CloudAiCaps::kMaxQueryParams) throw GoogleError::invalid("too many query parameters");
const CallSpec& s = spec(call);
std::string out;
for (const auto& [k, v] : query) {
if (!has_field(s.query_params, k)) throw GoogleError::invalid("unknown query parameter " + k);
if (v.size() > CloudAiCaps::kMaxQueryValueChars) throw GoogleError::invalid("query value too long");
out += (out.empty() ? "?" : "&") + clients::Url::encode_component(k) + "=" +
clients::Url::encode_component(v);
}
return out;
}
void check_body(Call call, const Json* body) {
if (!body || body->is_null()) return;
const CallSpec& s = spec(call);
if (!std::strcmp(s.http_method, "GET") || !std::strcmp(s.http_method, "DELETE"))
throw GoogleError::invalid("this call takes no request body");
if (!body->is_object()) throw GoogleError::invalid("request body must be a JSON object");
for (const auto& [k, v] : body->as_object()) {
(void)v;
if (!has_field(s.body_fields, k)) throw GoogleError::invalid("unknown request field " + k);
}
if (body->dump().size() > CloudAiCaps::kMaxBodyBytes) throw GoogleError::invalid("request body too large");
}
ServiceEndpoint endpoint_of(Call call, const std::string& name) {
const CallSpec& s = spec(call);
std::string api = s.api;
std::string root = "https://" + api + ".googleapis.com";
std::string service = api;
if (api == "translate") {
root = "https://translation.googleapis.com";
} else {
std::string loc = location_of(name);
for (const RegionalHosts& r : kRegional) {
if (api != r.api || loc.empty()) continue;
if ((std::string(",") + r.locations + ",").find("," + loc + ",") != std::string::npos)
root = "https://" + api + "." + loc + ".rep.googleapis.com";
else if (api == "speech" && (loc == "us" || loc == "eu"))
root = "https://" + loc + "-speech.googleapis.com"; // multi-region, product docs
}
}
return {service, root + "/" + s.version};
}
} // namespace molo::google::cloudai
+76
View File
@@ -0,0 +1,76 @@
#pragma once
// The ONE table of REST calls this package makes. The rows (calls_table.inc)
// and the enum (call_enum.inc) are generated from Google's Discovery documents
// by tools/check_discovery.py --generate and verified by the same script.
// Code builds every URL from this table, never from a literal. Package
// coverage: Translation v3, Vision v1, Natural Language v2 (+ two v1 only
// methods), Video Intelligence v1, Document AI v1, Speech v2, Text-to-Speech
// v1 (long audio and voices). Media Translation has no Discovery document.
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
#include "clients/body/json.h"
#include "clients/request.h"
#include "google/endpoints.h"
namespace molo::google::cloudai {
enum class Call {
#include "google/cloudai/call_enum.inc"
};
struct CallSpec {
Call call;
const char* http_method;
const char* path; // Discovery path, version prefix included
const char* api; // Discovery name: translate, vision, language, ...
const char* version; // v1, v2, v3
const char* rpc_id; // Discovery method id
const char* body_fields; // comma list: top-level request fields
const char* query_params; // comma list
const char* path_var; // the one path variable ("" if none)
const char* pattern; // Discovery pattern of that variable
};
std::size_t call_count();
const CallSpec& spec_at(std::size_t index);
const CallSpec& spec(Call call);
const CallSpec* find_rpc(const std::string& rpc_id);
// Number of schema field groups verified against Discovery (SCHEMAS table).
std::size_t schema_count();
clients::Method method_of(Call call);
// Query parameters in send order; each key must be in the row.
using Query = std::vector<std::pair<std::string, std::string>>;
// The path after the version prefix, with `name` validated against the
// Discovery pattern of the row's path variable. Rows without a variable take
// an empty name. Throws GoogleError::invalid.
std::string render(Call call, const std::string& name);
// "?k=v&..." (encoded), "" when empty. Keys must be query parameters of the
// row; count and value sizes are capped.
std::string query_of(Call call, const Query& query);
// Throws GoogleError::invalid when a top-level field is not a request field
// of the row, the body is not an object, it exceeds the size cap, or a GET /
// DELETE row is given a body. A null pointer is always fine.
void check_body(Call call, const clients::body::Json* body);
// Endpoint for a call. Document AI and Speech use the regional host named in
// `name` (".../locations/{l}/...") when Discovery lists one.
ServiceEndpoint endpoint_of(Call call, const std::string& name);
// True when `value` is a Discovery enum value of "Schema.field" for the
// enum sets this package accepts ("Feature.type", "AudioConfig.audioEncoding").
bool valid_enum(const std::string& schema_field, const std::string& value);
// True for [A-Za-z0-9._~-], 1..128, not "." or "..".
bool valid_name_segment(const std::string& s);
} // namespace molo::google::cloudai
+179
View File
@@ -0,0 +1,179 @@
// Generated by tools/check_discovery.py --generate. Do not edit.
// CALLS-BEGIN (parsed by tools/check_discovery.py; one row per line)
{Call::kTranslateDetectLanguage, "POST", "v3/{+parent}:detectLanguage", "translate", "v3", "translate.projects.detectLanguage", "content,documentInputConfig,labels,mimeType,model", "", "parent", "^projects/[^/]+$"},
{Call::kTranslateGetSupportedLanguages, "GET", "v3/{+parent}/supportedLanguages", "translate", "v3", "translate.projects.getSupportedLanguages", "", "displayLanguageCode,model", "parent", "^projects/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesAdaptiveMtSentencesList, "GET", "v3/{+parent}/adaptiveMtSentences", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.adaptiveMtFiles.adaptiveMtSentences.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+/adaptiveMtFiles/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.adaptiveMtFiles.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+/adaptiveMtFiles/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.adaptiveMtFiles.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+/adaptiveMtFiles/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtFilesList, "GET", "v3/{+parent}/adaptiveMtFiles", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.adaptiveMtFiles.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsAdaptiveMtSentencesList, "GET", "v3/{+parent}/adaptiveMtSentences", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.adaptiveMtSentences.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsCreate, "POST", "v3/{+parent}/adaptiveMtDatasets", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.create", "createTime,displayName,exampleCount,name,sourceLanguageCode,targetLanguageCode,updateTime", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsImportAdaptiveMtFile, "POST", "v3/{+parent}:importAdaptiveMtFile", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.importAdaptiveMtFile", "fileInputSource,gcsInputSource", "", "parent", "^projects/[^/]+/locations/[^/]+/adaptiveMtDatasets/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtDatasetsList, "GET", "v3/{+parent}/adaptiveMtDatasets", "translate", "v3", "translate.projects.locations.adaptiveMtDatasets.list", "", "filter,pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsAdaptiveMtTranslate, "POST", "v3/{+parent}:adaptiveMtTranslate", "translate", "v3", "translate.projects.locations.adaptiveMtTranslate", "content,dataset,glossaryConfig,mimeType,referenceSentenceConfig", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsBatchTranslateDocument, "POST", "v3/{+parent}:batchTranslateDocument", "translate", "v3", "translate.projects.locations.batchTranslateDocument", "customizedAttribution,enableRotationCorrection,enableShadowRemovalNativePdf,formatConversions,glossaries,inputConfigs,models,outputConfig,pdfNativeOnly,sourceLanguageCode,targetLanguageCodes", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsBatchTranslateText, "POST", "v3/{+parent}:batchTranslateText", "translate", "v3", "translate.projects.locations.batchTranslateText", "glossaries,inputConfigs,labels,models,outputConfig,sourceLanguageCode,targetLanguageCodes", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsDatasetsCreate, "POST", "v3/{+parent}/datasets", "translate", "v3", "translate.projects.locations.datasets.create", "createTime,displayName,exampleCount,name,sourceLanguageCode,targetLanguageCode,testExampleCount,trainExampleCount,updateTime,validateExampleCount", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsDatasetsDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.datasets.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/datasets/[^/]+$"},
{Call::kTranslateLocationsDatasetsExamplesList, "GET", "v3/{+parent}/examples", "translate", "v3", "translate.projects.locations.datasets.examples.list", "", "filter,pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/datasets/[^/]+$"},
{Call::kTranslateLocationsDatasetsExportData, "POST", "v3/{+dataset}:exportData", "translate", "v3", "translate.projects.locations.datasets.exportData", "outputConfig", "", "dataset", "^projects/[^/]+/locations/[^/]+/datasets/[^/]+$"},
{Call::kTranslateLocationsDatasetsGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.datasets.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/datasets/[^/]+$"},
{Call::kTranslateLocationsDatasetsImportData, "POST", "v3/{+dataset}:importData", "translate", "v3", "translate.projects.locations.datasets.importData", "inputConfig", "", "dataset", "^projects/[^/]+/locations/[^/]+/datasets/[^/]+$"},
{Call::kTranslateLocationsDatasetsList, "GET", "v3/{+parent}/datasets", "translate", "v3", "translate.projects.locations.datasets.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsDetectLanguage, "POST", "v3/{+parent}:detectLanguage", "translate", "v3", "translate.projects.locations.detectLanguage", "content,documentInputConfig,labels,mimeType,model", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsGetSupportedLanguages, "GET", "v3/{+parent}/supportedLanguages", "translate", "v3", "translate.projects.locations.getSupportedLanguages", "", "displayLanguageCode,model", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsGlossariesCreate, "POST", "v3/{+parent}/glossaries", "translate", "v3", "translate.projects.locations.glossaries.create", "displayName,endTime,entryCount,inputConfig,languageCodesSet,languagePair,name,submitTime", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsGlossariesDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.glossaries.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+$"},
{Call::kTranslateLocationsGlossariesGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.glossaries.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+$"},
{Call::kTranslateLocationsGlossariesGlossaryEntriesCreate, "POST", "v3/{+parent}/glossaryEntries", "translate", "v3", "translate.projects.locations.glossaries.glossaryEntries.create", "description,name,termsPair,termsSet", "", "parent", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+$"},
{Call::kTranslateLocationsGlossariesGlossaryEntriesDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.glossaries.glossaryEntries.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+/glossaryEntries/[^/]+$"},
{Call::kTranslateLocationsGlossariesGlossaryEntriesGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.glossaries.glossaryEntries.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+/glossaryEntries/[^/]+$"},
{Call::kTranslateLocationsGlossariesGlossaryEntriesList, "GET", "v3/{+parent}/glossaryEntries", "translate", "v3", "translate.projects.locations.glossaries.glossaryEntries.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+$"},
{Call::kTranslateLocationsGlossariesGlossaryEntriesPatch, "PATCH", "v3/{+name}", "translate", "v3", "translate.projects.locations.glossaries.glossaryEntries.patch", "description,name,termsPair,termsSet", "", "name", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+/glossaryEntries/[^/]+$"},
{Call::kTranslateLocationsGlossariesList, "GET", "v3/{+parent}/glossaries", "translate", "v3", "translate.projects.locations.glossaries.list", "", "filter,pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsGlossariesPatch, "PATCH", "v3/{+name}", "translate", "v3", "translate.projects.locations.glossaries.patch", "displayName,endTime,entryCount,inputConfig,languageCodesSet,languagePair,name,submitTime", "updateMask", "name", "^projects/[^/]+/locations/[^/]+/glossaries/[^/]+$"},
{Call::kTranslateLocationsList, "GET", "v3/{+name}/locations", "translate", "v3", "translate.projects.locations.list", "", "extraLocationTypes,filter,pageSize,pageToken", "name", "^projects/[^/]+$"},
{Call::kTranslateLocationsModelsCreate, "POST", "v3/{+parent}/models", "translate", "v3", "translate.projects.locations.models.create", "createTime,dataset,displayName,name,sourceLanguageCode,targetLanguageCode,testExampleCount,trainExampleCount,updateTime,validateExampleCount", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsModelsDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.models.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/models/[^/]+$"},
{Call::kTranslateLocationsModelsGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.models.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/models/[^/]+$"},
{Call::kTranslateLocationsModelsList, "GET", "v3/{+parent}/models", "translate", "v3", "translate.projects.locations.models.list", "", "filter,pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsOperationsCancel, "POST", "v3/{+name}:cancel", "translate", "v3", "translate.projects.locations.operations.cancel", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kTranslateLocationsOperationsDelete, "DELETE", "v3/{+name}", "translate", "v3", "translate.projects.locations.operations.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kTranslateLocationsOperationsGet, "GET", "v3/{+name}", "translate", "v3", "translate.projects.locations.operations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kTranslateLocationsOperationsList, "GET", "v3/{+name}/operations", "translate", "v3", "translate.projects.locations.operations.list", "", "filter,pageSize,pageToken,returnPartialSuccess", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsOperationsWait, "POST", "v3/{+name}:wait", "translate", "v3", "translate.projects.locations.operations.wait", "timeout", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kTranslateLocationsRefineText, "POST", "v3/{+parent}:refineText", "translate", "v3", "translate.projects.locations.refineText", "refinementEntries,sourceLanguageCode,targetLanguageCode", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsRomanizeText, "POST", "v3/{+parent}:romanizeText", "translate", "v3", "translate.projects.locations.romanizeText", "contents,sourceLanguageCode", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsTranslateDocument, "POST", "v3/{+parent}:translateDocument", "translate", "v3", "translate.projects.locations.translateDocument", "customizedAttribution,documentInputConfig,documentOutputConfig,enableRotationCorrection,enableShadowRemovalNativePdf,glossaryConfig,isTranslateNativePdfOnly,labels,model,sourceLanguageCode,targetLanguageCode", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateLocationsTranslateText, "POST", "v3/{+parent}:translateText", "translate", "v3", "translate.projects.locations.translateText", "contents,glossaryConfig,labels,mimeType,model,sourceLanguageCode,targetLanguageCode,transliterationConfig", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTranslateRomanizeText, "POST", "v3/{+parent}:romanizeText", "translate", "v3", "translate.projects.romanizeText", "contents,sourceLanguageCode", "", "parent", "^projects/[^/]+$"},
{Call::kTranslateTranslateText, "POST", "v3/{+parent}:translateText", "translate", "v3", "translate.projects.translateText", "contents,glossaryConfig,labels,mimeType,model,sourceLanguageCode,targetLanguageCode,transliterationConfig", "", "parent", "^projects/[^/]+$"},
{Call::kVisionFilesAnnotate, "POST", "v1/files:annotate", "vision", "v1", "vision.files.annotate", "labels,parent,requests", "", "", ""},
{Call::kVisionFilesAsyncBatchAnnotate, "POST", "v1/files:asyncBatchAnnotate", "vision", "v1", "vision.files.asyncBatchAnnotate", "labels,parent,requests", "", "", ""},
{Call::kVisionImagesAnnotate, "POST", "v1/images:annotate", "vision", "v1", "vision.images.annotate", "labels,parent,requests", "", "", ""},
{Call::kVisionImagesAsyncBatchAnnotate, "POST", "v1/images:asyncBatchAnnotate", "vision", "v1", "vision.images.asyncBatchAnnotate", "labels,outputConfig,parent,requests", "", "", ""},
{Call::kVisionLocationsOperationsGet, "GET", "v1/{+name}", "vision", "v1", "vision.locations.operations.get", "", "", "name", "^locations/[^/]+/operations/[^/]+$"},
{Call::kVisionOperationsCancel, "POST", "v1/{+name}:cancel", "vision", "v1", "vision.operations.cancel", "", "", "name", "^operations/.*$"},
{Call::kVisionOperationsDelete, "DELETE", "v1/{+name}", "vision", "v1", "vision.operations.delete", "", "", "name", "^operations/.*$"},
{Call::kVisionOperationsGet, "GET", "v1/{+name}", "vision", "v1", "vision.operations.get", "", "", "name", "^operations/[^/]+$"},
{Call::kVisionOperationsList, "GET", "v1/{+name}", "vision", "v1", "vision.operations.list", "", "filter,pageSize,pageToken,returnPartialSuccess", "name", "^operations$"},
{Call::kVisionProjectsFilesAnnotate, "POST", "v1/{+parent}/files:annotate", "vision", "v1", "vision.projects.files.annotate", "labels,parent,requests", "", "parent", "^projects/[^/]+$"},
{Call::kVisionProjectsFilesAsyncBatchAnnotate, "POST", "v1/{+parent}/files:asyncBatchAnnotate", "vision", "v1", "vision.projects.files.asyncBatchAnnotate", "labels,parent,requests", "", "parent", "^projects/[^/]+$"},
{Call::kVisionProjectsImagesAnnotate, "POST", "v1/{+parent}/images:annotate", "vision", "v1", "vision.projects.images.annotate", "labels,parent,requests", "", "parent", "^projects/[^/]+$"},
{Call::kVisionProjectsImagesAsyncBatchAnnotate, "POST", "v1/{+parent}/images:asyncBatchAnnotate", "vision", "v1", "vision.projects.images.asyncBatchAnnotate", "labels,outputConfig,parent,requests", "", "parent", "^projects/[^/]+$"},
{Call::kVisionLocationsFilesAnnotate, "POST", "v1/{+parent}/files:annotate", "vision", "v1", "vision.projects.locations.files.annotate", "labels,parent,requests", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsFilesAsyncBatchAnnotate, "POST", "v1/{+parent}/files:asyncBatchAnnotate", "vision", "v1", "vision.projects.locations.files.asyncBatchAnnotate", "labels,parent,requests", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsImagesAnnotate, "POST", "v1/{+parent}/images:annotate", "vision", "v1", "vision.projects.locations.images.annotate", "labels,parent,requests", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsImagesAsyncBatchAnnotate, "POST", "v1/{+parent}/images:asyncBatchAnnotate", "vision", "v1", "vision.projects.locations.images.asyncBatchAnnotate", "labels,outputConfig,parent,requests", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionProjectsLocationsOperationsGet, "GET", "v1/{+name}", "vision", "v1", "vision.projects.locations.operations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kVisionLocationsProductSetsAddProduct, "POST", "v1/{+name}:addProduct", "vision", "v1", "vision.projects.locations.productSets.addProduct", "product", "", "name", "^projects/[^/]+/locations/[^/]+/productSets/[^/]+$"},
{Call::kVisionLocationsProductSetsCreate, "POST", "v1/{+parent}/productSets", "vision", "v1", "vision.projects.locations.productSets.create", "displayName,indexError,indexTime,name", "productSetId", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsProductSetsDelete, "DELETE", "v1/{+name}", "vision", "v1", "vision.projects.locations.productSets.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/productSets/[^/]+$"},
{Call::kVisionLocationsProductSetsGet, "GET", "v1/{+name}", "vision", "v1", "vision.projects.locations.productSets.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/productSets/[^/]+$"},
{Call::kVisionLocationsProductSetsImport, "POST", "v1/{+parent}/productSets:import", "vision", "v1", "vision.projects.locations.productSets.import", "inputConfig", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsProductSetsList, "GET", "v1/{+parent}/productSets", "vision", "v1", "vision.projects.locations.productSets.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsProductSetsPatch, "PATCH", "v1/{+name}", "vision", "v1", "vision.projects.locations.productSets.patch", "displayName,indexError,indexTime,name", "updateMask", "name", "^projects/[^/]+/locations/[^/]+/productSets/[^/]+$"},
{Call::kVisionLocationsProductSetsProductsList, "GET", "v1/{+name}/products", "vision", "v1", "vision.projects.locations.productSets.products.list", "", "pageSize,pageToken", "name", "^projects/[^/]+/locations/[^/]+/productSets/[^/]+$"},
{Call::kVisionLocationsProductSetsRemoveProduct, "POST", "v1/{+name}:removeProduct", "vision", "v1", "vision.projects.locations.productSets.removeProduct", "product", "", "name", "^projects/[^/]+/locations/[^/]+/productSets/[^/]+$"},
{Call::kVisionLocationsProductsCreate, "POST", "v1/{+parent}/products", "vision", "v1", "vision.projects.locations.products.create", "description,displayName,name,productCategory,productLabels", "productId", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsProductsDelete, "DELETE", "v1/{+name}", "vision", "v1", "vision.projects.locations.products.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/products/[^/]+$"},
{Call::kVisionLocationsProductsGet, "GET", "v1/{+name}", "vision", "v1", "vision.projects.locations.products.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/products/[^/]+$"},
{Call::kVisionLocationsProductsList, "GET", "v1/{+parent}/products", "vision", "v1", "vision.projects.locations.products.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsProductsPatch, "PATCH", "v1/{+name}", "vision", "v1", "vision.projects.locations.products.patch", "description,displayName,name,productCategory,productLabels", "updateMask", "name", "^projects/[^/]+/locations/[^/]+/products/[^/]+$"},
{Call::kVisionLocationsProductsPurge, "POST", "v1/{+parent}/products:purge", "vision", "v1", "vision.projects.locations.products.purge", "deleteOrphanProducts,force,productSetPurgeConfig", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVisionLocationsProductsReferenceImagesCreate, "POST", "v1/{+parent}/referenceImages", "vision", "v1", "vision.projects.locations.products.referenceImages.create", "boundingPolys,name,uri", "referenceImageId", "parent", "^projects/[^/]+/locations/[^/]+/products/[^/]+$"},
{Call::kVisionLocationsProductsReferenceImagesDelete, "DELETE", "v1/{+name}", "vision", "v1", "vision.projects.locations.products.referenceImages.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/products/[^/]+/referenceImages/[^/]+$"},
{Call::kVisionLocationsProductsReferenceImagesGet, "GET", "v1/{+name}", "vision", "v1", "vision.projects.locations.products.referenceImages.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/products/[^/]+/referenceImages/[^/]+$"},
{Call::kVisionLocationsProductsReferenceImagesList, "GET", "v1/{+parent}/referenceImages", "vision", "v1", "vision.projects.locations.products.referenceImages.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/products/[^/]+$"},
{Call::kVisionProjectsOperationsGet, "GET", "v1/{+name}", "vision", "v1", "vision.projects.operations.get", "", "", "name", "^projects/[^/]+/operations/[^/]+$"},
{Call::kLanguageDocumentsAnalyzeEntities, "POST", "v2/documents:analyzeEntities", "language", "v2", "language.documents.analyzeEntities", "document,encodingType", "", "", ""},
{Call::kLanguageDocumentsAnalyzeSentiment, "POST", "v2/documents:analyzeSentiment", "language", "v2", "language.documents.analyzeSentiment", "document,encodingType", "", "", ""},
{Call::kLanguageDocumentsAnnotateText, "POST", "v2/documents:annotateText", "language", "v2", "language.documents.annotateText", "document,encodingType,features", "", "", ""},
{Call::kLanguageDocumentsClassifyText, "POST", "v2/documents:classifyText", "language", "v2", "language.documents.classifyText", "document", "", "", ""},
{Call::kLanguageDocumentsModerateText, "POST", "v2/documents:moderateText", "language", "v2", "language.documents.moderateText", "document,modelVersion", "", "", ""},
{Call::kLanguageV1DocumentsAnalyzeEntitySentiment, "POST", "v1/documents:analyzeEntitySentiment", "language", "v1", "language.documents.analyzeEntitySentiment", "document,encodingType", "", "", ""},
{Call::kLanguageV1DocumentsAnalyzeSyntax, "POST", "v1/documents:analyzeSyntax", "language", "v1", "language.documents.analyzeSyntax", "document,encodingType", "", "", ""},
{Call::kVideoLocationsOperationsCancel, "POST", "v1/{+name}:cancel", "videointelligence", "v1", "videointelligence.projects.locations.operations.cancel", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kVideoLocationsOperationsDelete, "DELETE", "v1/{+name}", "videointelligence", "v1", "videointelligence.projects.locations.operations.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kVideoLocationsOperationsGet, "GET", "v1/{+name}", "videointelligence", "v1", "videointelligence.projects.locations.operations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kVideoLocationsOperationsList, "GET", "v1/{+name}/operations", "videointelligence", "v1", "videointelligence.projects.locations.operations.list", "", "filter,pageSize,pageToken,returnPartialSuccess", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kVideoVideosAnnotate, "POST", "v1/videos:annotate", "videointelligence", "v1", "videointelligence.videos.annotate", "features,inputContent,inputUri,locationId,outputUri,videoContext", "", "", ""},
{Call::kDocumentaiOperationsDelete, "DELETE", "v1/{+name}", "documentai", "v1", "documentai.operations.delete", "", "", "name", "^operations/.*$"},
{Call::kDocumentaiLocationsFetchProcessorTypes, "GET", "v1/{+parent}:fetchProcessorTypes", "documentai", "v1", "documentai.projects.locations.fetchProcessorTypes", "", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsList, "GET", "v1/{+name}/locations", "documentai", "v1", "documentai.projects.locations.list", "", "extraLocationTypes,filter,pageSize,pageToken", "name", "^projects/[^/]+$"},
{Call::kDocumentaiLocationsOperationsCancel, "POST", "v1/{+name}:cancel", "documentai", "v1", "documentai.projects.locations.operations.cancel", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kDocumentaiLocationsOperationsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.operations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kDocumentaiLocationsOperationsList, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.operations.list", "", "filter,pageSize,pageToken,returnPartialSuccess", "name", "^projects/[^/]+/locations/[^/]+/operations$"},
{Call::kDocumentaiLocationsProcessorTypesGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.processorTypes.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/processorTypes/[^/]+$"},
{Call::kDocumentaiLocationsProcessorTypesList, "GET", "v1/{+parent}/processorTypes", "documentai", "v1", "documentai.projects.locations.processorTypes.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsBatchProcess, "POST", "v1/{+name}:batchProcess", "documentai", "v1", "documentai.projects.locations.processors.batchProcess", "documentOutputConfig,inputDocuments,labels,processOptions,skipHumanReview", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsCreate, "POST", "v1/{+parent}/processors", "documentai", "v1", "documentai.projects.locations.processors.create", "activeSchemaVersion,createTime,defaultProcessorVersion,displayName,kmsKeyName,name,processEndpoint,processorVersionAliases,satisfiesPzi,satisfiesPzs,state,type", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsDelete, "DELETE", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.processors.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsDisable, "POST", "v1/{+name}:disable", "documentai", "v1", "documentai.projects.locations.processors.disable", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsEnable, "POST", "v1/{+name}:enable", "documentai", "v1", "documentai.projects.locations.processors.enable", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.processors.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsHumanReviewConfigReviewDocument, "POST", "v1/{+humanReviewConfig}:reviewDocument", "documentai", "v1", "documentai.projects.locations.processors.humanReviewConfig.reviewDocument", "documentSchema,enableSchemaValidation,inlineDocument,priority", "", "humanReviewConfig", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/humanReviewConfig$"},
{Call::kDocumentaiLocationsProcessorsList, "GET", "v1/{+parent}/processors", "documentai", "v1", "documentai.projects.locations.processors.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcess, "POST", "v1/{+name}:process", "documentai", "v1", "documentai.projects.locations.processors.process", "fieldMask,gcsDocument,imagelessMode,inlineDocument,labels,processOptions,rawDocument,skipHumanReview", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsBatchProcess, "POST", "v1/{+name}:batchProcess", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.batchProcess", "documentOutputConfig,inputDocuments,labels,processOptions,skipHumanReview", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsDelete, "DELETE", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsDeploy, "POST", "v1/{+name}:deploy", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.deploy", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsEvaluateProcessorVersion, "POST", "v1/{+processorVersion}:evaluateProcessorVersion", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.evaluateProcessorVersion", "evaluationDocuments", "", "processorVersion", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsEvaluationsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.evaluations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+/evaluations/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsEvaluationsList, "GET", "v1/{+parent}/evaluations", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.evaluations.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsList, "GET", "v1/{+parent}/processorVersions", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsProcess, "POST", "v1/{+name}:process", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.process", "fieldMask,gcsDocument,imagelessMode,inlineDocument,labels,processOptions,rawDocument,skipHumanReview", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsTrain, "POST", "v1/{+parent}/processorVersions:train", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.train", "baseProcessorVersion,customDocumentExtractionOptions,documentSchema,foundationModelTuningOptions,inputData,processorVersion", "", "parent", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsProcessorVersionsUndeploy, "POST", "v1/{+name}:undeploy", "documentai", "v1", "documentai.projects.locations.processors.processorVersions.undeploy", "", "", "name", "^projects/[^/]+/locations/[^/]+/processors/[^/]+/processorVersions/[^/]+$"},
{Call::kDocumentaiLocationsProcessorsSetDefaultProcessorVersion, "POST", "v1/{+processor}:setDefaultProcessorVersion", "documentai", "v1", "documentai.projects.locations.processors.setDefaultProcessorVersion", "defaultProcessorVersion", "", "processor", "^projects/[^/]+/locations/[^/]+/processors/[^/]+$"},
{Call::kDocumentaiLocationsSchemasCreate, "POST", "v1/{+parent}/schemas", "documentai", "v1", "documentai.projects.locations.schemas.create", "createTime,displayName,labels,name,updateTime", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsSchemasDelete, "DELETE", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.schemas.delete", "", "force", "name", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+$"},
{Call::kDocumentaiLocationsSchemasGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.schemas.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+$"},
{Call::kDocumentaiLocationsSchemasList, "GET", "v1/{+parent}/schemas", "documentai", "v1", "documentai.projects.locations.schemas.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kDocumentaiLocationsSchemasPatch, "PATCH", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.schemas.patch", "createTime,displayName,labels,name,updateTime", "updateMask", "name", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+$"},
{Call::kDocumentaiLocationsSchemasSchemaVersionsCreate, "POST", "v1/{+parent}/schemaVersions", "documentai", "v1", "documentai.projects.locations.schemas.schemaVersions.create", "createTime,displayName,labels,name,schema", "", "parent", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+$"},
{Call::kDocumentaiLocationsSchemasSchemaVersionsDelete, "DELETE", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.schemas.schemaVersions.delete", "", "", "name", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+/schemaVersions/[^/]+$"},
{Call::kDocumentaiLocationsSchemasSchemaVersionsGenerate, "POST", "v1/{+parent}/schemaVersions:generate", "documentai", "v1", "documentai.projects.locations.schemas.schemaVersions.generate", "baseSchemaVersion,gcsDocuments,gcsPrefix,generateSchemaVersionParams,inlineDocuments,rawDocuments", "", "parent", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+$"},
{Call::kDocumentaiLocationsSchemasSchemaVersionsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.schemas.schemaVersions.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+/schemaVersions/[^/]+$"},
{Call::kDocumentaiLocationsSchemasSchemaVersionsList, "GET", "v1/{+parent}/schemaVersions", "documentai", "v1", "documentai.projects.locations.schemas.schemaVersions.list", "", "pageSize,pageToken", "parent", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+$"},
{Call::kDocumentaiLocationsSchemasSchemaVersionsPatch, "PATCH", "v1/{+name}", "documentai", "v1", "documentai.projects.locations.schemas.schemaVersions.patch", "createTime,displayName,labels,name,schema", "updateMask", "name", "^projects/[^/]+/locations/[^/]+/schemas/[^/]+/schemaVersions/[^/]+$"},
{Call::kDocumentaiOperationsGet, "GET", "v1/{+name}", "documentai", "v1", "documentai.projects.operations.get", "", "", "name", "^projects/[^/]+/operations/[^/]+$"},
{Call::kSpeechLocationsConfigGet, "GET", "v2/{+name}", "speech", "v2", "speech.projects.locations.config.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/config$"},
{Call::kSpeechLocationsConfigUpdate, "PATCH", "v2/{+name}", "speech", "v2", "speech.projects.locations.config.update", "kmsKeyName,name,updateTime", "updateMask", "name", "^projects/[^/]+/locations/[^/]+/config$"},
{Call::kSpeechLocationsCustomClassesCreate, "POST", "v2/{+parent}/customClasses", "speech", "v2", "speech.projects.locations.customClasses.create", "annotations,createTime,deleteTime,displayName,etag,expireTime,items,kmsKeyName,kmsKeyVersionName,name,reconciling,state,uid,updateTime", "customClassId,validateOnly", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsCustomClassesDelete, "DELETE", "v2/{+name}", "speech", "v2", "speech.projects.locations.customClasses.delete", "", "allowMissing,etag,validateOnly", "name", "^projects/[^/]+/locations/[^/]+/customClasses/[^/]+$"},
{Call::kSpeechLocationsCustomClassesGet, "GET", "v2/{+name}", "speech", "v2", "speech.projects.locations.customClasses.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/customClasses/[^/]+$"},
{Call::kSpeechLocationsCustomClassesList, "GET", "v2/{+parent}/customClasses", "speech", "v2", "speech.projects.locations.customClasses.list", "", "pageSize,pageToken,showDeleted", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsCustomClassesPatch, "PATCH", "v2/{+name}", "speech", "v2", "speech.projects.locations.customClasses.patch", "annotations,createTime,deleteTime,displayName,etag,expireTime,items,kmsKeyName,kmsKeyVersionName,name,reconciling,state,uid,updateTime", "updateMask,validateOnly", "name", "^projects/[^/]+/locations/[^/]+/customClasses/[^/]+$"},
{Call::kSpeechLocationsCustomClassesUndelete, "POST", "v2/{+name}:undelete", "speech", "v2", "speech.projects.locations.customClasses.undelete", "etag,name,validateOnly", "", "name", "^projects/[^/]+/locations/[^/]+/customClasses/[^/]+$"},
{Call::kSpeechLocationsGet, "GET", "v2/{+name}", "speech", "v2", "speech.projects.locations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsList, "GET", "v2/{+name}/locations", "speech", "v2", "speech.projects.locations.list", "", "extraLocationTypes,filter,pageSize,pageToken", "name", "^projects/[^/]+$"},
{Call::kSpeechLocationsOperationsGet, "GET", "v2/{+name}", "speech", "v2", "speech.projects.locations.operations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kSpeechLocationsOperationsList, "GET", "v2/{+name}/operations", "speech", "v2", "speech.projects.locations.operations.list", "", "filter,pageSize,pageToken,returnPartialSuccess", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsPhraseSetsCreate, "POST", "v2/{+parent}/phraseSets", "speech", "v2", "speech.projects.locations.phraseSets.create", "annotations,boost,createTime,deleteTime,displayName,etag,expireTime,kmsKeyName,kmsKeyVersionName,name,phrases,reconciling,state,uid,updateTime", "phraseSetId,validateOnly", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsPhraseSetsDelete, "DELETE", "v2/{+name}", "speech", "v2", "speech.projects.locations.phraseSets.delete", "", "allowMissing,etag,validateOnly", "name", "^projects/[^/]+/locations/[^/]+/phraseSets/[^/]+$"},
{Call::kSpeechLocationsPhraseSetsGet, "GET", "v2/{+name}", "speech", "v2", "speech.projects.locations.phraseSets.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/phraseSets/[^/]+$"},
{Call::kSpeechLocationsPhraseSetsList, "GET", "v2/{+parent}/phraseSets", "speech", "v2", "speech.projects.locations.phraseSets.list", "", "pageSize,pageToken,showDeleted", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsPhraseSetsPatch, "PATCH", "v2/{+name}", "speech", "v2", "speech.projects.locations.phraseSets.patch", "annotations,boost,createTime,deleteTime,displayName,etag,expireTime,kmsKeyName,kmsKeyVersionName,name,phrases,reconciling,state,uid,updateTime", "updateMask,validateOnly", "name", "^projects/[^/]+/locations/[^/]+/phraseSets/[^/]+$"},
{Call::kSpeechLocationsPhraseSetsUndelete, "POST", "v2/{+name}:undelete", "speech", "v2", "speech.projects.locations.phraseSets.undelete", "etag,name,validateOnly", "", "name", "^projects/[^/]+/locations/[^/]+/phraseSets/[^/]+$"},
{Call::kSpeechLocationsRecognizersBatchRecognize, "POST", "v2/{+recognizer}:batchRecognize", "speech", "v2", "speech.projects.locations.recognizers.batchRecognize", "config,configMask,files,processingStrategy,recognitionOutputConfig,recognizer", "", "recognizer", "^projects/[^/]+/locations/[^/]+/recognizers/[^/]+$"},
{Call::kSpeechLocationsRecognizersCreate, "POST", "v2/{+parent}/recognizers", "speech", "v2", "speech.projects.locations.recognizers.create", "annotations,createTime,defaultRecognitionConfig,deleteTime,displayName,etag,expireTime,kmsKeyName,kmsKeyVersionName,languageCodes,model,name,reconciling,state,uid,updateTime", "recognizerId,validateOnly", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsRecognizersDelete, "DELETE", "v2/{+name}", "speech", "v2", "speech.projects.locations.recognizers.delete", "", "allowMissing,etag,validateOnly", "name", "^projects/[^/]+/locations/[^/]+/recognizers/[^/]+$"},
{Call::kSpeechLocationsRecognizersGet, "GET", "v2/{+name}", "speech", "v2", "speech.projects.locations.recognizers.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/recognizers/[^/]+$"},
{Call::kSpeechLocationsRecognizersList, "GET", "v2/{+parent}/recognizers", "speech", "v2", "speech.projects.locations.recognizers.list", "", "pageSize,pageToken,showDeleted", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kSpeechLocationsRecognizersPatch, "PATCH", "v2/{+name}", "speech", "v2", "speech.projects.locations.recognizers.patch", "annotations,createTime,defaultRecognitionConfig,deleteTime,displayName,etag,expireTime,kmsKeyName,kmsKeyVersionName,languageCodes,model,name,reconciling,state,uid,updateTime", "updateMask,validateOnly", "name", "^projects/[^/]+/locations/[^/]+/recognizers/[^/]+$"},
{Call::kSpeechLocationsRecognizersRecognize, "POST", "v2/{+recognizer}:recognize", "speech", "v2", "speech.projects.locations.recognizers.recognize", "config,configMask,content,uri", "", "recognizer", "^projects/[^/]+/locations/[^/]+/recognizers/[^/]+$"},
{Call::kSpeechLocationsRecognizersUndelete, "POST", "v2/{+name}:undelete", "speech", "v2", "speech.projects.locations.recognizers.undelete", "etag,name,validateOnly", "", "name", "^projects/[^/]+/locations/[^/]+/recognizers/[^/]+$"},
{Call::kTtsOperationsCancel, "POST", "v1/{+name}:cancel", "texttospeech", "v1", "texttospeech.operations.cancel", "", "", "name", "^operations/.*$"},
{Call::kTtsOperationsDelete, "DELETE", "v1/{+name}", "texttospeech", "v1", "texttospeech.operations.delete", "", "", "name", "^operations/.*$"},
{Call::kTtsLocationsOperationsGet, "GET", "v1/{+name}", "texttospeech", "v1", "texttospeech.projects.locations.operations.get", "", "", "name", "^projects/[^/]+/locations/[^/]+/operations/[^/]+$"},
{Call::kTtsLocationsOperationsList, "GET", "v1/{+name}/operations", "texttospeech", "v1", "texttospeech.projects.locations.operations.list", "", "filter,pageSize,pageToken,returnPartialSuccess", "name", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTtsLocationsSynthesizeLongAudio, "POST", "v1/{+parent}:synthesizeLongAudio", "texttospeech", "v1", "texttospeech.projects.locations.synthesizeLongAudio", "audioConfig,input,outputGcsUri,voice", "", "parent", "^projects/[^/]+/locations/[^/]+$"},
{Call::kTtsTextSynthesize, "POST", "v1/text:synthesize", "texttospeech", "v1", "texttospeech.text.synthesize", "advancedVoiceOptions,audioConfig,input,voice", "", "", ""},
{Call::kTtsVoicesList, "GET", "v1/voices", "texttospeech", "v1", "texttospeech.voices.list", "", "languageCode", "", ""},
// CALLS-END
+31
View File
@@ -0,0 +1,31 @@
#pragma once
// Static ceilings for the cloudai package. Never autotuned.
#include <cstddef>
namespace molo::google::cloudai {
struct CloudAiCaps {
static constexpr std::size_t kMaxNameChars = 512; // a resource name
static constexpr std::size_t kMaxSegmentChars = 128; // one path segment
static constexpr std::size_t kMaxBodyBytes = 16 * 1024 * 1024; // serialized request
static constexpr std::size_t kMaxQueryParams = 20;
static constexpr std::size_t kMaxQueryValueChars = 2048;
static constexpr std::size_t kMaxListPages = 20;
static constexpr std::size_t kPageSize = 50;
static constexpr std::size_t kMaxTexts = 1024; // contents per translate call
static constexpr std::size_t kMaxTextChars = 30000; // Translation v3: 30k code points per request
static constexpr std::size_t kMaxNlTextChars = 1000000; // Natural Language document content
static constexpr std::size_t kMaxImages = 16; // images per annotate batch (Vision limit)
static constexpr std::size_t kMaxFeatures = 16;
static constexpr int kMaxResults = 100; // maxResults per feature
static constexpr std::size_t kMaxUriChars = 1024;
static constexpr std::size_t kMaxIdChars = 63; // resource ids (glossary, processor, recognizer)
static constexpr std::size_t kMaxDisplayChars = 128;
static constexpr std::size_t kMaxLanguageCodeChars = 35;
static constexpr std::size_t kMaxLongAudioChars = 1000000; // long audio input text, bytes
static constexpr std::size_t kMaxAudioBytes = 10 * 1024 * 1024; // inline audio / document bytes
};
} // namespace molo::google::cloudai
+87
View File
@@ -0,0 +1,87 @@
#include "google/cloudai/cloudai_service.h"
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
CloudAiService::CloudAiService(std::shared_ptr<GoogleClient> client, CloudContext context,
Sleeper sleeper)
: Service(std::move(client), ServiceEndpoint{"cloudai", ""}),
context_(std::move(context)),
sleeper_(std::move(sleeper)) {}
Json CloudAiService::invoke(Call call, const std::string& name, const Json* body,
const Query& query) const {
std::string path = render(call, name) + query_of(call, query);
check_body(call, body);
return client_->request(method_of(call), endpoint_of(call, name), path, body);
}
Json CloudAiService::list(Call call, const std::string& name, const std::string& items_key,
Query query) const {
query.emplace_back("pageSize", std::to_string(CloudAiCaps::kPageSize));
std::string path = render(call, name) + query_of(call, query);
return list_all(*client_, endpoint_of(call, name), path, items_key, CloudAiCaps::kMaxListPages);
}
Operation CloudAiService::start(Call call, const std::string& name, const Json* body,
const Query& query) const {
Json reply = invoke(call, name, body, query);
Operation op = Operation::from_json(reply);
if (op.name.empty() && !op.done) throw GoogleError(502, "BAD_RESPONSE", "no operation in reply", "cloudai");
return op;
}
Operation CloudAiService::operation(Call get_call, const std::string& operation_name) const {
return Operation::from_json(invoke(get_call, operation_name));
}
Operation CloudAiService::wait(Call get_call, const std::string& operation_name) const {
std::string path = render(get_call, operation_name); // validates the name
(void)path;
return wait_operation(*client_, endpoint_of(get_call, operation_name), operation_name, sleeper_);
}
std::string CloudAiService::project() const {
std::string p = context_.parent(); // validates the project
return p.substr(0, p.find("/locations/"));
}
std::string checked_language_code(const std::string& code, const char* what) {
if (code.empty() || code.size() > CloudAiCaps::kMaxLanguageCodeChars)
throw GoogleError::invalid(std::string(what) + " must be a language code (1..35 chars)");
for (char c : code)
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-'))
throw GoogleError::invalid(std::string(what) + " has characters outside A-Z a-z 0-9 -");
return code;
}
std::string checked_id(const std::string& id, const char* what) {
if (id.empty() || id.size() > CloudAiCaps::kMaxIdChars || !valid_name_segment(id))
throw GoogleError::invalid(std::string(what) + " must be 1..63 chars of A-Z a-z 0-9 . _ - ~");
return id;
}
std::string checked_gcs_uri(const std::string& uri, const char* what) {
if (uri.rfind("gs://", 0) != 0 || uri.size() <= 5 || uri.size() > CloudAiCaps::kMaxUriChars)
throw GoogleError::invalid(std::string(what) + " must be a gs:// URI");
for (char c : uri)
if (static_cast<unsigned char>(c) <= 0x20 || c == '"' || c == '\\')
throw GoogleError::invalid(std::string(what) + " has illegal characters");
return uri;
}
std::string checked_https_or_gcs_uri(const std::string& uri, const char* what) {
if (uri.rfind("gs://", 0) == 0) return checked_gcs_uri(uri, what);
if (uri.rfind("https://", 0) != 0 || uri.size() <= 8 || uri.size() > CloudAiCaps::kMaxUriChars)
throw GoogleError::invalid(std::string(what) + " must be a gs:// or https:// URI");
for (char c : uri)
if (static_cast<unsigned char>(c) <= 0x20 || c == '"' || c == '\\')
throw GoogleError::invalid(std::string(what) + " has illegal characters");
return uri;
}
} // namespace molo::google::cloudai
+62
View File
@@ -0,0 +1,62 @@
#pragma once
// Base of every cloudai API class: runs one row of the call table through the
// shared GoogleClient. Validation (resource name against the Discovery
// pattern, request fields, query parameters, sizes) happens before any
// network traffic and before the credential check.
#include <memory>
#include <string>
#include "google/cloudai/calls.h"
#include "google/operation.h"
#include "google/resource.h"
#include "google/service.h"
namespace molo::google::cloudai {
class CloudAiService : public Service {
public:
CloudAiService(std::shared_ptr<GoogleClient> client, CloudContext context,
Sleeper sleeper = nullptr);
const CloudContext& context() const { return context_; }
// One call of the table. `name` fills the row's path variable. Throws
// GoogleError::invalid for bad input, NotConfiguredError without a credential.
clients::body::Json invoke(Call call, const std::string& name = "",
const clients::body::Json* body = nullptr,
const Query& query = {}) const;
// Follows nextPageToken (capped) on a list row and collects `items_key`.
clients::body::Json list(Call call, const std::string& name, const std::string& items_key,
Query query = {}) const;
// A row that starts a long-running operation: answers it, never waits.
Operation start(Call call, const std::string& name, const clients::body::Json* body = nullptr,
const Query& query = {}) const;
// GET the operation by its full name through `get_call` (the family's
// operations.get row).
Operation operation(Call get_call, const std::string& operation_name) const;
// Polls until done (hard caps in OperationCaps). Throws on failure.
Operation wait(Call get_call, const std::string& operation_name) const;
// "projects/{project}/locations/{location}" of the context.
std::string parent() const { return context_.parent(); }
// "projects/{project}" only.
std::string project() const;
protected:
CloudContext context_;
Sleeper sleeper_;
};
// Shared input checks for the typed classes. All throw GoogleError::invalid.
std::string checked_language_code(const std::string& code, const char* what);
std::string checked_id(const std::string& id, const char* what); // resource id, 1..63 [A-Za-z0-9._-]
std::string checked_gcs_uri(const std::string& uri, const char* what);
std::string checked_https_or_gcs_uri(const std::string& uri, const char* what);
} // namespace molo::google::cloudai
+125
View File
@@ -0,0 +1,125 @@
#include "google/cloudai/document_ai.h"
#include <cstring>
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
namespace {
bool mime_ok(const std::string& m) {
for (const char* ok : {"application/pdf", "image/png", "image/jpeg", "image/tiff", "image/gif",
"image/bmp", "image/webp", "text/html"})
if (m == ok) return true;
return false;
}
} // namespace
std::string DocumentAi::processor_name(const std::string& id) const {
return context_.child("processors", checked_id(id, "processor id"));
}
std::string DocumentAi::process_target(const ProcessRequest& r) const {
std::string name = processor_name(r.processor_id);
if (!r.version_id.empty()) name += "/processorVersions/" + checked_id(r.version_id, "version id");
return name;
}
Json DocumentAi::process_body(const ProcessRequest& r) const {
if (!mime_ok(r.mime_type))
throw GoogleError::invalid("mimeType must be application/pdf, image/png, image/jpeg, image/tiff, image/gif, image/bmp, image/webp or text/html");
Json b = Json::object();
if (!r.gcs_uri.empty()) {
if (!r.content_base64.empty()) throw GoogleError::invalid("give content or gcsUri, not both");
Json g = Json::object();
g.set("gcsUri", Json(checked_gcs_uri(r.gcs_uri, "gcsUri")));
g.set("mimeType", Json(r.mime_type));
b.set("gcsDocument", g);
} else {
if (r.content_base64.empty()) throw GoogleError::invalid("content or gcsUri is required");
if (r.content_base64.size() / 4 * 3 > CloudAiCaps::kMaxAudioBytes)
throw GoogleError::invalid("document over 10 MiB");
for (char c : r.content_base64)
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '+' ||
c == '/' || c == '=' || c == '-' || c == '_'))
throw GoogleError::invalid("content is not base64");
Json raw = Json::object();
raw.set("content", Json(r.content_base64));
raw.set("mimeType", Json(r.mime_type));
b.set("rawDocument", raw);
}
b.set("skipHumanReview", Json(r.skip_human_review));
if (!r.field_mask.empty()) {
if (r.field_mask.size() > 512) throw GoogleError::invalid("fieldMask too long");
b.set("fieldMask", Json(r.field_mask));
}
return b;
}
Json DocumentAi::process(const ProcessRequest& r) const {
Json b = process_body(r);
return invoke(r.version_id.empty() ? Call::kDocumentaiLocationsProcessorsProcess
: Call::kDocumentaiLocationsProcessorsProcessorVersionsProcess,
process_target(r), &b);
}
Json DocumentAi::list_processor_types() const {
return list(Call::kDocumentaiLocationsProcessorTypesList, parent(), "processorTypes");
}
Json DocumentAi::fetch_processor_types() const {
return invoke(Call::kDocumentaiLocationsFetchProcessorTypes, parent());
}
Json DocumentAi::list_processors() const {
return list(Call::kDocumentaiLocationsProcessorsList, parent(), "processors");
}
Json DocumentAi::get_processor(const std::string& id) const {
return invoke(Call::kDocumentaiLocationsProcessorsGet, processor_name(id));
}
Json DocumentAi::list_versions(const std::string& processor_id) const {
return list(Call::kDocumentaiLocationsProcessorsProcessorVersionsList, processor_name(processor_id),
"processorVersions");
}
Json DocumentAi::list_schemas() const {
return list(Call::kDocumentaiLocationsSchemasList, parent(), "schemas");
}
Json DocumentAi::list_locations() const {
return list(Call::kDocumentaiLocationsList, project(), "locations");
}
Operation DocumentAi::get_operation(const std::string& name) const {
return operation(Call::kDocumentaiLocationsOperationsGet, name);
}
Json DocumentAi::create_processor(const std::string& display_name, const std::string& type) const {
if (display_name.empty() || display_name.size() > CloudAiCaps::kMaxDisplayChars)
throw GoogleError::invalid("displayName: 1..128 chars");
if (type.empty() || type.size() > 64) throw GoogleError::invalid("type: 1..64 chars");
for (char c : type)
if (!((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'))
throw GoogleError::invalid("type looks like FORM_PARSER_PROCESSOR");
Json b = Json::object();
b.set("displayName", Json(display_name));
b.set("type", Json(type));
return invoke(Call::kDocumentaiLocationsProcessorsCreate, parent(), &b);
}
Operation DocumentAi::delete_processor_start(const std::string& id) const {
return start(Call::kDocumentaiLocationsProcessorsDelete, processor_name(id));
}
Operation DocumentAi::enable_processor_start(const std::string& id) const {
return start(Call::kDocumentaiLocationsProcessorsEnable, processor_name(id));
}
Operation DocumentAi::disable_processor_start(const std::string& id) const {
return start(Call::kDocumentaiLocationsProcessorsDisable, processor_name(id));
}
Operation DocumentAi::batch_process_start(const std::string& processor_id, const Json& body) const {
return start(Call::kDocumentaiLocationsProcessorsBatchProcess, processor_name(processor_id), &body);
}
Json DocumentAi::call(Call c, const std::string& name, const Json* body, const Query& query) const {
if (std::strcmp(spec(c).api, "documentai") != 0) throw GoogleError::invalid("not a Document AI call");
return invoke(c, name, body, query);
}
} // namespace molo::google::cloudai
+55
View File
@@ -0,0 +1,55 @@
#pragma once
// Document AI v1: processor types, processors, versions, schemas, online
// process, batch process start. Locations "us" and "eu" use their regional
// hosts. Process is paid per page.
#include <string>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct ProcessRequest {
std::string processor_id; // processor id (hex) under the context location
std::string version_id; // optional processor version id
std::string content_base64; // raw document bytes, base64 (at most 10 MiB decoded)
std::string gcs_uri; // alternative: gs:// document
std::string mime_type; // application/pdf, image/png, ...
bool skip_human_review = true;
std::string field_mask; // optional, e.g. "text,entities"
};
class DocumentAi : public CloudAiService {
public:
using CloudAiService::CloudAiService;
std::string processor_name(const std::string& id) const;
std::string process_target(const ProcessRequest& r) const;
clients::body::Json process_body(const ProcessRequest& r) const;
// Paid. Online (sync) process.
clients::body::Json process(const ProcessRequest& r) const;
// Reads.
clients::body::Json list_processor_types() const;
clients::body::Json fetch_processor_types() const;
clients::body::Json list_processors() const;
clients::body::Json get_processor(const std::string& id) const;
clients::body::Json list_versions(const std::string& processor_id) const;
clients::body::Json list_schemas() const;
clients::body::Json list_locations() const;
Operation get_operation(const std::string& name) const;
// Writes: explicit, no defaults.
clients::body::Json create_processor(const std::string& display_name, const std::string& type) const;
Operation delete_processor_start(const std::string& id) const;
Operation enable_processor_start(const std::string& id) const;
Operation disable_processor_start(const std::string& id) const;
// Batch process from a prepared body (inputDocuments, documentOutputConfig); paid.
Operation batch_process_start(const std::string& processor_id, const clients::body::Json& body) const;
clients::body::Json call(Call c, const std::string& name, const clients::body::Json* body = nullptr,
const Query& query = {}) const;
};
} // namespace molo::google::cloudai
+56
View File
@@ -0,0 +1,56 @@
#include "google/cloudai/long_audio.h"
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
Json LongAudio::request_body(const LongAudioRequest& r) const {
if (r.text.empty()) throw GoogleError::invalid("text is empty");
if (r.text.size() > CloudAiCaps::kMaxLongAudioChars) throw GoogleError::invalid("text over 1,000,000 bytes");
if (!valid_enum("AudioConfig.audioEncoding", r.encoding)) throw GoogleError::invalid("unknown audioEncoding");
Json in = Json::object();
in.set(r.ssml ? "ssml" : "text", Json(r.text));
Json voice = Json::object();
voice.set("languageCode", Json(checked_language_code(r.language, "language")));
if (!r.voice_name.empty()) {
if (r.voice_name.size() > 128 || !valid_name_segment(r.voice_name))
throw GoogleError::invalid("voice name has illegal characters");
voice.set("name", Json(r.voice_name));
}
Json audio = Json::object();
audio.set("audioEncoding", Json(r.encoding));
Json b = Json::object();
b.set("input", in);
b.set("voice", voice);
b.set("audioConfig", audio);
b.set("outputGcsUri", Json(checked_gcs_uri(r.output_uri, "outputUri")));
return b;
}
Operation LongAudio::synthesize_start(const LongAudioRequest& r) const {
Json b = request_body(r);
return start(Call::kTtsLocationsSynthesizeLongAudio, parent(), &b);
}
Operation LongAudio::get_operation(const std::string& name) const {
return operation(Call::kTtsLocationsOperationsGet, name);
}
Json LongAudio::list_operations() const {
return list(Call::kTtsLocationsOperationsList, parent(), "operations");
}
Json LongAudio::voices(const std::string& language) const {
Query q;
if (!language.empty()) q.emplace_back("languageCode", checked_language_code(language, "language"));
return invoke(Call::kTtsVoicesList, "", nullptr, q);
}
void LongAudio::cancel_operation(const std::string& name) const {
invoke(Call::kTtsOperationsCancel, name);
}
} // namespace molo::google::cloudai
+37
View File
@@ -0,0 +1,37 @@
#pragma once
// Text-to-Speech long audio synthesis (v1 projects.locations.synthesizeLongAudio):
// a long-running operation that writes LINEAR16 audio to a gs:// object, plus
// the voice list. Paid per character. Short synthesis stays in
// google/text_to_speech.h.
#include <string>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct LongAudioRequest {
std::string text; // plain text (or SSML when ssml is true), at most 1,000,000 bytes
bool ssml = false;
std::string language; // voice languageCode, e.g. en-US
std::string voice_name; // optional, e.g. en-US-Standard-A
std::string encoding = "LINEAR16"; // AudioConfig.audioEncoding (long audio supports LINEAR16)
std::string output_uri; // gs://bucket/object.wav
};
class LongAudio : public CloudAiService {
public:
using CloudAiService::CloudAiService;
clients::body::Json request_body(const LongAudioRequest& r) const;
Operation synthesize_start(const LongAudioRequest& r) const; // never waits
Operation get_operation(const std::string& name) const;
clients::body::Json list_operations() const;
// Free of charge.
clients::body::Json voices(const std::string& language = "") const;
// Explicit.
void cancel_operation(const std::string& name) const;
};
} // namespace molo::google::cloudai
+77
View File
@@ -0,0 +1,77 @@
#include "google/cloudai/natural_language.h"
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
Json NaturalLanguage::document_json(const NlDocument& d) const {
if (!valid_enum("Document.type", d.type)) throw GoogleError::invalid("document type must be PLAIN_TEXT or HTML");
Json o = Json::object();
o.set("type", Json(d.type));
if (!d.gcs_uri.empty()) {
if (!d.content.empty()) throw GoogleError::invalid("give content or gcsUri, not both");
o.set("gcsContentUri", Json(checked_gcs_uri(d.gcs_uri, "gcsUri")));
} else {
if (d.content.empty()) throw GoogleError::invalid("document content is empty");
if (d.content.size() > CloudAiCaps::kMaxNlTextChars)
throw GoogleError::invalid("document content over 1,000,000 bytes");
o.set("content", Json(d.content));
}
if (!d.language.empty()) o.set("languageCode", Json(checked_language_code(d.language, "language")));
return o;
}
Json NaturalLanguage::with_document(Call c, const NlDocument& d, const std::string& encoding,
bool has_encoding) const {
Json b = Json::object();
b.set("document", document_json(d));
if (!encoding.empty()) {
if (!has_encoding || !valid_enum("AnnotateTextRequest.encodingType", encoding))
throw GoogleError::invalid("encodingType must be NONE, UTF8, UTF16 or UTF32");
b.set("encodingType", Json(encoding));
}
return invoke(c, "", &b);
}
Json NaturalLanguage::analyze_sentiment(const NlDocument& d, const std::string& e) const {
return with_document(Call::kLanguageDocumentsAnalyzeSentiment, d, e, true);
}
Json NaturalLanguage::analyze_entities(const NlDocument& d, const std::string& e) const {
return with_document(Call::kLanguageDocumentsAnalyzeEntities, d, e, true);
}
Json NaturalLanguage::classify_text(const NlDocument& d) const {
return with_document(Call::kLanguageDocumentsClassifyText, d, "", false);
}
Json NaturalLanguage::moderate_text(const NlDocument& d) const {
return with_document(Call::kLanguageDocumentsModerateText, d, "", false);
}
Json NaturalLanguage::analyze_entity_sentiment(const NlDocument& d, const std::string& e) const {
return with_document(Call::kLanguageV1DocumentsAnalyzeEntitySentiment, d, e, true);
}
Json NaturalLanguage::analyze_syntax(const NlDocument& d, const std::string& e) const {
return with_document(Call::kLanguageV1DocumentsAnalyzeSyntax, d, e, true);
}
Json NaturalLanguage::annotate_text(const NlDocument& d, const NlFeatures& f, const std::string& encoding) const {
if (!f.entities && !f.sentiment && !f.classify && !f.moderate)
throw GoogleError::invalid("annotateText needs at least one feature");
Json feats = Json::object();
if (f.entities) feats.set("extractEntities", Json(true));
if (f.sentiment) feats.set("extractDocumentSentiment", Json(true));
if (f.classify) feats.set("classifyText", Json(true));
if (f.moderate) feats.set("moderateText", Json(true));
Json b = Json::object();
b.set("document", document_json(d));
b.set("features", feats);
if (!encoding.empty()) {
if (!valid_enum("AnnotateTextRequest.encodingType", encoding))
throw GoogleError::invalid("encodingType must be NONE, UTF8, UTF16 or UTF32");
b.set("encodingType", Json(encoding));
}
return invoke(Call::kLanguageDocumentsAnnotateText, "", &b);
}
} // namespace molo::google::cloudai
+48
View File
@@ -0,0 +1,48 @@
#pragma once
// Cloud Natural Language: v2 (entities, sentiment, classify, moderate,
// annotateText) and the two v1-only methods (entity sentiment, syntax).
// Document content is paid per 1000 characters (first 5000 a month free).
#include <string>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct NlDocument {
std::string content; // text or HTML, at most 1,000,000 bytes
std::string gcs_uri; // alternative to content
std::string type = "PLAIN_TEXT"; // PLAIN_TEXT or HTML
std::string language; // optional BCP-47, empty: detect
};
struct NlFeatures {
bool entities = false;
bool sentiment = false;
bool classify = false;
bool moderate = false;
};
class NaturalLanguage : public CloudAiService {
public:
using CloudAiService::CloudAiService;
clients::body::Json document_json(const NlDocument& d) const;
clients::body::Json analyze_sentiment(const NlDocument& d, const std::string& encoding = "") const;
clients::body::Json analyze_entities(const NlDocument& d, const std::string& encoding = "") const;
clients::body::Json classify_text(const NlDocument& d) const;
clients::body::Json moderate_text(const NlDocument& d) const;
clients::body::Json annotate_text(const NlDocument& d, const NlFeatures& f,
const std::string& encoding = "") const;
// v1 only.
clients::body::Json analyze_entity_sentiment(const NlDocument& d, const std::string& encoding = "") const;
clients::body::Json analyze_syntax(const NlDocument& d, const std::string& encoding = "") const;
private:
clients::body::Json with_document(Call c, const NlDocument& d, const std::string& encoding,
bool has_encoding) const;
};
} // namespace molo::google::cloudai
+102
View File
@@ -0,0 +1,102 @@
#include "google/cloudai/speech_v2.h"
#include <cstring>
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
namespace {
std::string checked_model(const std::string& m) {
if (m.empty() || m.size() > 64) throw GoogleError::invalid("model: 1..64 chars");
for (char c : m)
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_'))
throw GoogleError::invalid("model has characters outside a-z 0-9 _");
return m;
}
Json languages_json(const std::vector<std::string>& langs) {
if (langs.empty() || langs.size() > 3) throw GoogleError::invalid("languages: 1..3 codes");
Json a = Json::array();
for (const auto& l : langs) a.push(Json(l == "auto" ? l : checked_language_code(l, "language")));
return a;
}
} // namespace
std::string SpeechV2::recognizer_name(const std::string& id) const {
return context_.child("recognizers", id == "_" ? id : checked_id(id, "recognizer id"));
}
Json SpeechV2::recognize_body(const SpeechRequest& r) const {
Json cfg = Json::object();
cfg.set("model", Json(checked_model(r.model)));
cfg.set("languageCodes", languages_json(r.languages));
cfg.set("autoDecodingConfig", Json::object());
Json b = Json::object();
b.set("config", cfg);
if (!r.uri.empty()) {
if (!r.content_base64.empty()) throw GoogleError::invalid("give content or uri, not both");
b.set("uri", Json(checked_gcs_uri(r.uri, "uri")));
} else {
if (r.content_base64.empty()) throw GoogleError::invalid("audio content or uri is required");
if (r.content_base64.size() / 4 * 3 > CloudAiCaps::kMaxAudioBytes)
throw GoogleError::invalid("audio over 10 MiB");
for (char c : r.content_base64)
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '+' ||
c == '/' || c == '=' || c == '-' || c == '_'))
throw GoogleError::invalid("content is not base64");
b.set("content", Json(r.content_base64));
}
return b;
}
Json SpeechV2::recognize(const SpeechRequest& r) const {
Json b = recognize_body(r);
return invoke(Call::kSpeechLocationsRecognizersRecognize, recognizer_name(r.recognizer), &b);
}
Json SpeechV2::list_recognizers() const {
return list(Call::kSpeechLocationsRecognizersList, parent(), "recognizers");
}
Json SpeechV2::get_recognizer(const std::string& id) const {
return invoke(Call::kSpeechLocationsRecognizersGet, recognizer_name(id));
}
Json SpeechV2::list_phrase_sets() const {
return list(Call::kSpeechLocationsPhraseSetsList, parent(), "phraseSets");
}
Json SpeechV2::list_custom_classes() const {
return list(Call::kSpeechLocationsCustomClassesList, parent(), "customClasses");
}
Json SpeechV2::get_config() const {
return invoke(Call::kSpeechLocationsConfigGet, parent() + "/config");
}
Json SpeechV2::list_locations() const {
return list(Call::kSpeechLocationsList, project(), "locations");
}
Operation SpeechV2::get_operation(const std::string& name) const {
return operation(Call::kSpeechLocationsOperationsGet, name);
}
Operation SpeechV2::create_recognizer_start(const std::string& id, const std::string& model,
const std::vector<std::string>& languages) const {
Json rec = Json::object();
rec.set("model", Json(checked_model(model)));
rec.set("languageCodes", languages_json(languages));
return start(Call::kSpeechLocationsRecognizersCreate, parent(), &rec,
{{"recognizerId", checked_id(id, "recognizer id")}});
}
Operation SpeechV2::delete_recognizer_start(const std::string& id) const {
return start(Call::kSpeechLocationsRecognizersDelete, recognizer_name(id));
}
Json SpeechV2::call(Call c, const std::string& name, const Json* body, const Query& query) const {
if (std::strcmp(spec(c).api, "speech") != 0) throw GoogleError::invalid("not a Speech call");
return invoke(c, name, body, query);
}
} // namespace molo::google::cloudai
+49
View File
@@ -0,0 +1,49 @@
#pragma once
// Speech-to-Text v2 (Chirp models): recognize against a recognizer (the
// recognizer id "_" is the implicit default recognizer), recognizers, phrase
// sets, custom classes, config. Models such as chirp_2 / chirp_3 need their
// supported regions (us-central1, europe-west4, us, eu ...).
#include <string>
#include <vector>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct SpeechRequest {
std::string recognizer = "_"; // id under the context location
std::string content_base64; // audio bytes, base64 (at most 10 MiB decoded)
std::string uri; // alternative: gs:// audio
std::string model = "chirp_2"; // 1..64 chars [a-z0-9_]
std::vector<std::string> languages; // 1..3 BCP-47 codes, or "auto"
};
class SpeechV2 : public CloudAiService {
public:
using CloudAiService::CloudAiService;
std::string recognizer_name(const std::string& id) const;
clients::body::Json recognize_body(const SpeechRequest& r) const;
// Paid (per second of audio).
clients::body::Json recognize(const SpeechRequest& r) const;
clients::body::Json list_recognizers() const;
clients::body::Json get_recognizer(const std::string& id) const;
clients::body::Json list_phrase_sets() const;
clients::body::Json list_custom_classes() const;
clients::body::Json get_config() const;
clients::body::Json list_locations() const;
Operation get_operation(const std::string& name) const;
// Writes: explicit, no defaults.
Operation create_recognizer_start(const std::string& id, const std::string& model,
const std::vector<std::string>& languages) const;
Operation delete_recognizer_start(const std::string& id) const;
clients::body::Json call(Call c, const std::string& name, const clients::body::Json* body = nullptr,
const Query& query = {}) const;
};
} // namespace molo::google::cloudai
+220
View File
@@ -0,0 +1,220 @@
#!/usr/bin/env python3
"""Check (and generate) the cloudai call table against Google's Discovery documents.
python3 -I src/google/cloudai/tools/check_discovery.py # check
python3 src/google/cloudai/tools/check_discovery.py --generate # rewrite the .inc files
The call table (calls_table.inc) and the enum (call_enum.inc) are generated
from Discovery by --generate, so every path, HTTP method, request field and
query parameter is read, never typed. The check fails (exit 1) when:
row a row is absent from Discovery, or its HTTP method, path, request
fields, query parameters, path variable or pattern differ;
coverage a Discovery method of a covered API is neither a row nor listed in
EXCLUDED with a reason (a new Discovery method fails until wrapped);
schema a schema field the typed classes use (SCHEMAS-BEGIN block in
calls.cpp) is absent;
enum an accepted enum set differs from Discovery's;
endpoint a regional host used by calls.cpp (Document AI, Speech) is not in
the Discovery `endpoints` list.
Python 3 stdlib only, no credentials needed.
"""
import json
import os
import re
import sys
import time
import urllib.request
HERE = os.path.dirname(os.path.abspath(__file__))
PKG = os.path.join(HERE, "..")
URL = "https://%s.googleapis.com/$discovery/rest?version=%s"
# (discovery name, version, enum prefix, only these rpc ids or None for all)
APIS = [
("translate", "v3", "Translate", None),
("vision", "v1", "Vision", None),
("language", "v2", "Language", None),
("language", "v1", "LanguageV1",
{"language.documents.analyzeEntitySentiment", "language.documents.analyzeSyntax"}),
("videointelligence", "v1", "Video", None),
("documentai", "v1", "Documentai", None),
("speech", "v2", "Speech", None),
("texttospeech", "v1", "Tts", None),
]
# Discovery methods deliberately not wrapped (id -> reason).
EXCLUDED = {
"videointelligence.operations.projects.locations.operations.get": "alias of projects.locations.operations.get",
"videointelligence.operations.projects.locations.operations.cancel": "alias of projects.locations.operations.cancel",
"videointelligence.operations.projects.locations.operations.delete": "alias of projects.locations.operations.delete",
}
# Not in the Discovery directory: no public REST surface to derive calls from.
NOT_IN_DISCOVERY = ["mediatranslation"]
def fetch(name, version):
last = None
for _ in range(4):
try:
with urllib.request.urlopen(URL % (name, version), timeout=60) as r:
return json.load(r)
except Exception as e: # transient 502s happen
last = e
time.sleep(2)
raise last
def methods(node, out):
for m in node.get("methods", {}).values():
out[m["id"]] = m
for child in node.get("resources", {}).values():
methods(child, out)
def camel(parts):
return "".join(p[:1].upper() + p[1:] for p in parts)
def row_fields(doc, m):
ref = m.get("request", {}).get("$ref")
body = sorted(doc["schemas"][ref]["properties"]) if ref else []
query = sorted(k for k, v in m.get("parameters", {}).items() if v.get("location") == "query")
names = re.findall(r"\{\+?(\w+)\}", m["path"])
var = names[0] if names else ""
pat = m.get("parameters", {}).get(var, {}).get("pattern", "") if var else ""
return body, query, var, pat
def build():
docs, rows, used = {}, [], set()
for name, version, prefix, only in APIS:
doc = fetch(name, version)
docs[(name, version)] = doc
idx = {}
methods(doc, idx)
for rpc in sorted(idx):
m = idx[rpc]
if only is not None and rpc not in only:
continue
if rpc in EXCLUDED:
continue
parts = rpc.split(".")[1:]
short = parts[1:] if parts[0] == "projects" else parts
ident = "k" + prefix + camel(short)
if ident in used:
ident = "k" + prefix + camel(parts)
assert ident not in used, ident
used.add(ident)
body, query, var, pat = row_fields(doc, m)
rows.append((ident, m["httpMethod"], m["path"], name, version, rpc,
",".join(body), ",".join(query), var, pat))
return docs, rows
def generate():
_, rows = build()
with open(os.path.join(PKG, "call_enum.inc"), "w") as f:
f.write("// Generated by tools/check_discovery.py --generate. Do not edit.\n")
for r in rows:
f.write(" %s,\n" % r[0])
with open(os.path.join(PKG, "calls_table.inc"), "w") as f:
f.write("// Generated by tools/check_discovery.py --generate. Do not edit.\n")
f.write("// CALLS-BEGIN (parsed by tools/check_discovery.py; one row per line)\n")
for r in rows:
f.write(" {Call::%s, %s},\n" % (r[0], ", ".join('"%s"' % x for x in r[1:])))
f.write("// CALLS-END\n")
print("generated %d rows" % len(rows))
ROW = re.compile(r'^\s*\{Call::(\w+),\s*(.*)\},?\s*$')
STR = re.compile(r'"([^"]*)"')
def table_rows():
rows = []
text = open(os.path.join(PKG, "calls_table.inc")).read()
a, b = text.index("// CALLS-BEGIN"), text.index("// CALLS-END")
for line in text[a:b].splitlines()[1:]:
m = ROW.match(line)
if m:
rows.append(STR.findall(m.group(2)))
return rows
def block(text, begin, end):
a, b = text.index(begin), text.index(end)
out = []
for line in text[a:b].splitlines()[1:]:
s = STR.findall(line)
if s:
out.append(s)
return out
def main():
if "--generate" in sys.argv:
generate()
return 0
rows = table_rows()
docs, expected = build()
bad = []
# (rpc, api, version) triples
have = {(r[4], r[2], r[3]) for r in rows}
want = {(e[5], e[3], e[4]) for e in expected}
for t in sorted(want - have):
bad.append("coverage: discovery method %s (%s %s) is not a row" % t)
for t in sorted(have - want):
bad.append("row %s (%s %s) is not in discovery" % t)
by = {}
for r in rows:
by[(r[4], r[2], r[3])] = r
for e in expected:
r = by.get((e[5], e[3], e[4]))
if not r:
continue
if r[0] != e[1]:
bad.append("%s: http %s != %s" % (e[5], r[0], e[1]))
if r[1] != e[2]:
bad.append("%s: path %s != %s" % (e[5], r[1], e[2]))
for i, label in ((5, "body fields"), (6, "query"), (7, "path variable"), (8, "pattern")):
if r[i] != e[i + 1]:
bad.append("%s: %s %r != %r" % (e[5], label, r[i], e[i + 1]))
calls_cpp = open(os.path.join(PKG, "calls.cpp")).read()
for api, version, schema, fields in block(calls_cpp, "// SCHEMAS-BEGIN", "// SCHEMAS-END"):
doc = docs.get((api, version))
props = doc["schemas"].get(schema, {}).get("properties") if doc else None
if props is None:
bad.append("missing schema %s %s %s" % (api, version, schema))
continue
for f in fields.split(","):
if f not in props:
bad.append("schema %s.%s absent" % (schema, f))
for api, version, sf, values in block(calls_cpp, "// ENUMS-BEGIN", "// ENUMS-END"):
doc = docs.get((api, version))
schema, field = sf.split(".")
prop = doc["schemas"].get(schema, {}).get("properties", {}).get(field) if doc else None
enum = (prop.get("enum") or prop.get("items", {}).get("enum")) if prop else None
if not enum:
bad.append("enum %s %s has no discovery enum" % (api, sf))
continue
real = {v for v in enum if not v.endswith("UNSPECIFIED")}
if real != set(values.split(",")):
bad.append("enum %s differs: missing %s extra %s" % (
sf, sorted(real - set(values.split(","))), sorted(set(values.split(",")) - real)))
for api, version, hosts in block(calls_cpp, "// ENDPOINTS-BEGIN", "// ENDPOINTS-END"):
doc = docs.get((api, version))
listed = {e["location"] for e in doc.get("endpoints", [])} if doc else set()
for loc in hosts.split(","):
if loc not in listed:
bad.append("endpoint %s %s not in discovery endpoints" % (api, loc))
revs = {"%s-%s" % k: d.get("revision") for k, d in docs.items()}
if bad:
print("DISCOVERY CHECK FAILED (revisions %s)" % revs)
for b in bad:
print(" " + b)
return 1
print("discovery ok: %d rows, %d apis, revisions %s" % (len(rows), len(docs), revs))
return 0
if __name__ == "__main__":
sys.exit(main())
+191
View File
@@ -0,0 +1,191 @@
#include "google/cloudai/translation.h"
#include <cstring>
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
namespace {
std::size_t code_points(const std::string& s) {
std::size_t n = 0;
for (unsigned char c : s)
if ((c & 0xC0) != 0x80) ++n;
return n;
}
void check_texts(const std::vector<std::string>& texts) {
if (texts.empty()) throw GoogleError::invalid("texts must not be empty");
if (texts.size() > CloudAiCaps::kMaxTexts) throw GoogleError::invalid("too many texts (max 1024)");
std::size_t total = 0;
for (const auto& t : texts) {
if (t.empty()) throw GoogleError::invalid("a text is empty");
total += code_points(t);
}
if (total > CloudAiCaps::kMaxTextChars)
throw GoogleError::invalid("texts exceed 30000 characters in total");
}
Json strings(const std::vector<std::string>& v) {
Json a = Json::array();
for (const auto& s : v) a.push(Json(s));
return a;
}
std::string s_at(const Json& j, const char* k) {
const Json* v = j.is_object() ? j.find(k) : nullptr;
return v && v->is_string() ? v->as_string() : std::string();
}
} // namespace
std::string Translation::glossary_name(const std::string& id) const {
return context_.child("glossaries", checked_id(id, "glossary id"));
}
void Translation::validate(const TranslateRequest& r) const {
check_texts(r.texts);
checked_language_code(r.target, "target");
if (!r.source.empty()) checked_language_code(r.source, "source");
if (!r.mime_type.empty() && r.mime_type != "text/plain" && r.mime_type != "text/html")
throw GoogleError::invalid("mimeType must be text/plain or text/html");
if (r.model.size() > CloudAiCaps::kMaxNameChars) throw GoogleError::invalid("model too long");
if (!r.model.empty() && r.model.rfind("projects/", 0) != 0)
throw GoogleError::invalid("model must be a full projects/... name");
if (!r.glossary.empty() && (r.glossary.rfind("projects/", 0) != 0 ||
r.glossary.size() > CloudAiCaps::kMaxNameChars ||
r.glossary.find("..") != std::string::npos))
throw GoogleError::invalid("glossary must be a full projects/... name");
}
Json Translation::translate_body(const TranslateRequest& r) const {
validate(r);
Json b = Json::object();
b.set("contents", strings(r.texts));
b.set("targetLanguageCode", Json(r.target));
if (!r.source.empty()) b.set("sourceLanguageCode", Json(r.source));
if (!r.mime_type.empty()) b.set("mimeType", Json(r.mime_type));
if (!r.model.empty()) b.set("model", Json(r.model));
if (!r.glossary.empty()) {
Json g = Json::object();
g.set("glossary", Json(r.glossary));
if (r.glossary_ignore_case) g.set("ignoreCase", Json(true));
b.set("glossaryConfig", g);
}
return b;
}
std::vector<Translated> Translation::translate(const TranslateRequest& r) const {
Json b = translate_body(r);
Json reply = invoke(Call::kTranslateLocationsTranslateText, parent(), &b);
std::vector<Translated> out;
const Json* arr = reply.find("translations");
if (arr && arr->is_array())
for (const Json& t : arr->as_array())
out.push_back({s_at(t, "translatedText"), s_at(t, "detectedLanguageCode"), s_at(t, "model")});
return out;
}
Json Translation::detect_language(const std::string& text) const {
check_texts({text});
Json b = Json::object();
b.set("content", Json(text));
b.set("mimeType", Json("text/plain"));
return invoke(Call::kTranslateLocationsDetectLanguage, parent(), &b);
}
Json Translation::supported_languages(const std::string& display_language, const std::string& model) const {
Query q;
if (!display_language.empty()) q.emplace_back("displayLanguageCode", checked_language_code(display_language, "displayLanguageCode"));
if (!model.empty()) q.emplace_back("model", model);
return invoke(Call::kTranslateLocationsGetSupportedLanguages, parent(), nullptr, q);
}
Json Translation::romanize(const std::vector<std::string>& texts, const std::string& source) const {
check_texts(texts);
Json b = Json::object();
b.set("contents", strings(texts));
if (!source.empty()) b.set("sourceLanguageCode", Json(checked_language_code(source, "source")));
return invoke(Call::kTranslateLocationsRomanizeText, parent(), &b);
}
Json Translation::list_glossaries() const {
return list(Call::kTranslateLocationsGlossariesList, parent(), "glossaries");
}
Json Translation::get_glossary(const std::string& id) const {
return invoke(Call::kTranslateLocationsGlossariesGet, glossary_name(id));
}
Json Translation::list_adaptive_datasets() const {
return list(Call::kTranslateLocationsAdaptiveMtDatasetsList, parent(), "adaptiveMtDatasets");
}
Json Translation::get_adaptive_dataset(const std::string& id) const {
return invoke(Call::kTranslateLocationsAdaptiveMtDatasetsGet,
context_.child("adaptiveMtDatasets", checked_id(id, "dataset id")));
}
Json Translation::list_models() const {
return list(Call::kTranslateLocationsModelsList, parent(), "models");
}
Json Translation::list_datasets() const {
return list(Call::kTranslateLocationsDatasetsList, parent(), "datasets");
}
Json Translation::list_locations() const {
return list(Call::kTranslateLocationsList, project(), "locations");
}
Operation Translation::get_operation(const std::string& name) const {
return operation(Call::kTranslateLocationsOperationsGet, name);
}
Json Translation::adaptive_translate(const std::string& dataset_id, const std::vector<std::string>& texts) const {
check_texts(texts);
Json b = Json::object();
b.set("dataset", Json(context_.child("adaptiveMtDatasets", checked_id(dataset_id, "dataset id"))));
b.set("content", strings(texts));
return invoke(Call::kTranslateLocationsAdaptiveMtTranslate, parent(), &b);
}
Json Translation::glossary_body(const GlossaryDraft& d) const {
Json b = Json::object();
b.set("name", Json(glossary_name(d.id)));
Json in = Json::object();
Json gcs = Json::object();
gcs.set("inputUri", Json(checked_gcs_uri(d.input_uri, "inputUri")));
in.set("gcsSource", gcs);
b.set("inputConfig", in);
if (!d.language_codes.empty()) {
if (!d.source_language.empty() || !d.target_language.empty())
throw GoogleError::invalid("use a language pair or a language set, not both");
if (d.language_codes.size() < 2 || d.language_codes.size() > 64)
throw GoogleError::invalid("a language set needs 2..64 codes");
Json set = Json::object();
Json codes = Json::array();
for (const auto& c : d.language_codes) codes.push(Json(checked_language_code(c, "language code")));
set.set("languageCodes", codes);
b.set("languageCodesSet", set);
} else {
Json pair = Json::object();
pair.set("sourceLanguageCode", Json(checked_language_code(d.source_language, "source")));
pair.set("targetLanguageCode", Json(checked_language_code(d.target_language, "target")));
b.set("languagePair", pair);
}
return b;
}
Operation Translation::create_glossary_start(const GlossaryDraft& d) const {
Json b = glossary_body(d);
return start(Call::kTranslateLocationsGlossariesCreate, parent(), &b);
}
Operation Translation::delete_glossary_start(const std::string& id) const {
return start(Call::kTranslateLocationsGlossariesDelete, glossary_name(id));
}
Json Translation::call(Call c, const std::string& name, const Json* body, const Query& query) const {
if (std::strcmp(spec(c).api, "translate") != 0) throw GoogleError::invalid("not a Translation call");
return invoke(c, name, body, query);
}
} // namespace molo::google::cloudai
+81
View File
@@ -0,0 +1,81 @@
#pragma once
// Cloud Translation v3: text, language detection, supported languages,
// romanization, glossaries, adaptive MT, batch and document translation,
// models and datasets. Location defaults to "global" in the demo; glossaries
// and models are regional resources.
#include <string>
#include <vector>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct TranslateRequest {
std::vector<std::string> texts; // 1..1024, total at most 30000 characters
std::string target; // required language code
std::string source; // optional, empty: detect
std::string mime_type; // text/plain (default) or text/html
std::string model; // optional full model name
std::string glossary; // optional full glossary name
bool glossary_ignore_case = false;
};
struct Translated {
std::string text;
std::string detected_language;
std::string model;
};
struct GlossaryDraft {
std::string id; // glossary id
std::string input_uri; // gs:// csv / tsv / tmx
std::string source_language; // with target_language: unidirectional pair
std::string target_language;
std::vector<std::string> language_codes; // set: equivalent term sets (alternative to the pair)
};
class Translation : public CloudAiService {
public:
using CloudAiService::CloudAiService;
void validate(const TranslateRequest& r) const;
clients::body::Json translate_body(const TranslateRequest& r) const;
// Paid (per character). Uses projects/{p}/locations/{l}.
std::vector<Translated> translate(const TranslateRequest& r) const;
clients::body::Json detect_language(const std::string& text) const;
clients::body::Json supported_languages(const std::string& display_language = "",
const std::string& model = "") const;
clients::body::Json romanize(const std::vector<std::string>& texts,
const std::string& source = "") const;
// Reads.
clients::body::Json list_glossaries() const;
clients::body::Json get_glossary(const std::string& id) const;
clients::body::Json list_adaptive_datasets() const;
clients::body::Json get_adaptive_dataset(const std::string& id) const;
clients::body::Json list_models() const;
clients::body::Json list_datasets() const;
clients::body::Json list_locations() const;
Operation get_operation(const std::string& name) const;
// Adaptive MT translate: texts against a dataset (paid).
clients::body::Json adaptive_translate(const std::string& dataset_id,
const std::vector<std::string>& texts) const;
// Long-running starts. They answer the operation and never wait.
clients::body::Json glossary_body(const GlossaryDraft& d) const;
Operation create_glossary_start(const GlossaryDraft& d) const;
// Destructive: explicit, no default.
Operation delete_glossary_start(const std::string& id) const;
// Any other row of the Translation family by call (validated against it).
clients::body::Json call(Call c, const std::string& name, const clients::body::Json* body = nullptr,
const Query& query = {}) const;
std::string glossary_name(const std::string& id) const;
};
} // namespace molo::google::cloudai
+66
View File
@@ -0,0 +1,66 @@
#include "google/cloudai/video_intelligence.h"
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
Json VideoIntelligence::annotate_body(const VideoRequest& r) const {
if (r.features.empty() || r.features.size() > 9) throw GoogleError::invalid("features: 1..9 required");
Json feats = Json::array();
for (const auto& f : r.features) {
if (!valid_enum("GoogleCloudVideointelligenceV1_AnnotateVideoRequest.features", f))
throw GoogleError::invalid("unknown video feature " + f);
feats.push(Json(f));
}
Json b = Json::object();
if (!r.input_uri.empty()) {
if (!r.input_content_base64.empty()) throw GoogleError::invalid("give inputUri or content, not both");
b.set("inputUri", Json(checked_gcs_uri(r.input_uri, "inputUri")));
} else {
if (r.input_content_base64.empty()) throw GoogleError::invalid("inputUri or content is required");
if (r.input_content_base64.size() / 4 * 3 > CloudAiCaps::kMaxAudioBytes)
throw GoogleError::invalid("inline video over 10 MiB");
b.set("inputContent", Json(r.input_content_base64));
}
b.set("features", feats);
if (!r.output_uri.empty()) b.set("outputUri", Json(checked_gcs_uri(r.output_uri, "outputUri")));
if (!r.location_id.empty()) {
for (const char* ok : {"us-east1", "us-west1", "europe-west1", "asia-east1"})
if (r.location_id == ok) {
b.set("locationId", Json(r.location_id));
break;
}
if (!b.find("locationId")) throw GoogleError::invalid("locationId must be us-east1, us-west1, europe-west1 or asia-east1");
}
return b;
}
Operation VideoIntelligence::annotate_start(const VideoRequest& r) const {
Json b = annotate_body(r);
return start(Call::kVideoVideosAnnotate, "", &b);
}
Operation VideoIntelligence::get_operation(const std::string& name) const {
return operation(Call::kVideoLocationsOperationsGet, name);
}
Json VideoIntelligence::list_operations(const std::string& filter) const {
Query q;
if (!filter.empty()) q.emplace_back("filter", filter);
return list(Call::kVideoLocationsOperationsList, parent(), "operations", q);
}
void VideoIntelligence::cancel_operation(const std::string& name) const {
invoke(Call::kVideoLocationsOperationsCancel, name);
}
void VideoIntelligence::delete_operation(const std::string& name) const {
invoke(Call::kVideoLocationsOperationsDelete, name);
}
Operation VideoIntelligence::wait_operation_done(const std::string& name) const {
return wait(Call::kVideoLocationsOperationsGet, name);
}
} // namespace molo::google::cloudai
+38
View File
@@ -0,0 +1,38 @@
#pragma once
// Cloud Video Intelligence v1: videos.annotate starts a long-running
// operation (paid per minute of video and feature); operations are read,
// listed, cancelled or deleted through the project location.
#include <string>
#include <vector>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct VideoRequest {
std::string input_uri; // gs:// video
std::string input_content_base64; // alternative, at most 10 MiB
std::vector<std::string> features; // 1..9, AnnotateVideoRequest.features enum
std::string output_uri; // optional gs:// for the JSON result
std::string location_id; // optional: us-east1, us-west1, europe-west1, asia-east1
};
class VideoIntelligence : public CloudAiService {
public:
using CloudAiService::CloudAiService;
clients::body::Json annotate_body(const VideoRequest& r) const;
// Starts the operation and answers it; never waits (videos take minutes).
Operation annotate_start(const VideoRequest& r) const;
Operation get_operation(const std::string& name) const;
clients::body::Json list_operations(const std::string& filter = "") const;
// Cancel and delete are explicit.
void cancel_operation(const std::string& name) const;
void delete_operation(const std::string& name) const;
Operation wait_operation_done(const std::string& name) const;
};
} // namespace molo::google::cloudai
+188
View File
@@ -0,0 +1,188 @@
#include "google/cloudai/vision.h"
#include <cstring>
#include "google/cloudai/caps.h"
#include "google/errors.h"
namespace molo::google::cloudai {
using clients::body::Json;
namespace {
bool base64_ok(const std::string& s) {
if (s.empty()) return false;
for (char c : s)
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '+' ||
c == '/' || c == '=' || c == '-' || c == '_'))
return false;
return true;
}
void check_inline(const std::string& b64, const char* what) {
if (!base64_ok(b64)) throw GoogleError::invalid(std::string(what) + " is not base64");
if (b64.size() / 4 * 3 > CloudAiCaps::kMaxAudioBytes)
throw GoogleError::invalid(std::string(what) + " exceeds 10 MiB");
}
Json features_json(const std::vector<VisionFeature>& features) {
if (features.empty() || features.size() > CloudAiCaps::kMaxFeatures)
throw GoogleError::invalid("features: 1..16 required");
Json arr = Json::array();
for (const auto& f : features) {
if (!Vision::valid_feature(f.type)) throw GoogleError::invalid("unknown feature type " + f.type);
if (f.max_results < 0 || f.max_results > CloudAiCaps::kMaxResults)
throw GoogleError::invalid("maxResults must be 0..100");
Json o = Json::object();
o.set("type", Json(f.type));
if (f.max_results > 0) o.set("maxResults", Json(f.max_results));
arr.push(o);
}
return arr;
}
Json image_json(const VisionImage& im) {
Json o = Json::object();
if (!im.content_base64.empty()) {
if (!im.uri.empty()) throw GoogleError::invalid("image: give content or uri, not both");
check_inline(im.content_base64, "image content");
o.set("content", Json(im.content_base64));
} else if (!im.uri.empty()) {
Json src = Json::object();
if (im.uri.rfind("gs://", 0) == 0)
src.set("gcsImageUri", Json(checked_gcs_uri(im.uri, "image uri")));
else
src.set("imageUri", Json(checked_https_or_gcs_uri(im.uri, "image uri")));
o.set("source", src);
} else {
throw GoogleError::invalid("image needs content or uri");
}
return o;
}
} // namespace
bool Vision::valid_feature(const std::string& type) { return valid_enum("Feature.type", type); }
std::string Vision::product_set_name(const std::string& id) const {
return context_.child("productSets", checked_id(id, "product set id"));
}
std::string Vision::product_name(const std::string& id) const {
return context_.child("products", checked_id(id, "product id"));
}
Json Vision::annotate_body(const std::vector<VisionImage>& images,
const std::vector<VisionFeature>& features) const {
if (images.empty() || images.size() > CloudAiCaps::kMaxImages)
throw GoogleError::invalid("images: 1..16 required");
Json feats = features_json(features);
Json reqs = Json::array();
for (const auto& im : images) {
Json r = Json::object();
r.set("image", image_json(im));
r.set("features", feats);
reqs.push(r);
}
Json b = Json::object();
b.set("requests", reqs);
return b;
}
Json Vision::annotate(const std::vector<VisionImage>& images, const std::vector<VisionFeature>& features) const {
Json b = annotate_body(images, features);
return invoke(Call::kVisionImagesAnnotate, "", &b);
}
Json Vision::annotate_file_body(const FileAnnotation& file, const std::vector<VisionFeature>& features) const {
Json input = Json::object();
if (!file.gcs_uri.empty()) {
if (!file.content_base64.empty()) throw GoogleError::invalid("file: give gcsUri or content, not both");
Json gcs = Json::object();
gcs.set("uri", Json(checked_gcs_uri(file.gcs_uri, "file uri")));
input.set("gcsSource", gcs);
} else {
check_inline(file.content_base64, "file content");
input.set("content", Json(file.content_base64));
}
if (file.mime_type != "application/pdf" && file.mime_type != "image/tiff" && file.mime_type != "image/gif")
throw GoogleError::invalid("mimeType must be application/pdf, image/tiff or image/gif");
input.set("mimeType", Json(file.mime_type));
Json req = Json::object();
req.set("inputConfig", input);
req.set("features", features_json(features));
if (!file.pages.empty()) {
if (file.pages.size() > 5) throw GoogleError::invalid("at most 5 pages");
Json pages = Json::array();
for (int p : file.pages) {
if (p < 1 || p > 10000) throw GoogleError::invalid("page numbers are 1..10000");
pages.push(Json(p));
}
req.set("pages", pages);
}
Json reqs = Json::array();
reqs.push(req);
Json b = Json::object();
b.set("requests", reqs);
return b;
}
Json Vision::annotate_file(const FileAnnotation& file, const std::vector<VisionFeature>& features) const {
Json b = annotate_file_body(file, features);
return invoke(Call::kVisionFilesAnnotate, "", &b);
}
Json Vision::list_product_sets() const {
return list(Call::kVisionLocationsProductSetsList, parent(), "productSets");
}
Json Vision::get_product_set(const std::string& id) const {
return invoke(Call::kVisionLocationsProductSetsGet, product_set_name(id));
}
Json Vision::list_products() const {
return list(Call::kVisionLocationsProductsList, parent(), "products");
}
Json Vision::get_product(const std::string& id) const {
return invoke(Call::kVisionLocationsProductsGet, product_name(id));
}
Json Vision::list_reference_images(const std::string& product_id) const {
return list(Call::kVisionLocationsProductsReferenceImagesList, product_name(product_id), "referenceImages");
}
Json Vision::create_product_set(const std::string& id, const std::string& display_name) const {
if (display_name.empty() || display_name.size() > CloudAiCaps::kMaxDisplayChars)
throw GoogleError::invalid("displayName: 1..128 chars");
Json b = Json::object();
b.set("displayName", Json(display_name));
return invoke(Call::kVisionLocationsProductSetsCreate, parent(), &b,
{{"productSetId", checked_id(id, "product set id")}});
}
Json Vision::delete_product_set(const std::string& id) const {
return invoke(Call::kVisionLocationsProductSetsDelete, product_set_name(id));
}
Json Vision::create_product(const std::string& id, const std::string& display_name,
const std::string& category) const {
if (display_name.empty() || display_name.size() > CloudAiCaps::kMaxDisplayChars)
throw GoogleError::invalid("displayName: 1..128 chars");
if (category != "homegoods-v2" && category != "apparel-v2" && category != "toys-v2" &&
category != "packagedgoods-v1" && category != "general-v1")
throw GoogleError::invalid("category must be homegoods-v2, apparel-v2, toys-v2, packagedgoods-v1 or general-v1");
Json b = Json::object();
b.set("displayName", Json(display_name));
b.set("productCategory", Json(category));
return invoke(Call::kVisionLocationsProductsCreate, parent(), &b,
{{"productId", checked_id(id, "product id")}});
}
Json Vision::delete_product(const std::string& id) const {
return invoke(Call::kVisionLocationsProductsDelete, product_name(id));
}
Operation Vision::get_operation(const std::string& name) const {
return operation(Call::kVisionProjectsLocationsOperationsGet, name);
}
Json Vision::call(Call c, const std::string& name, const Json* body, const Query& query) const {
if (std::strcmp(spec(c).api, "vision") != 0) throw GoogleError::invalid("not a Vision call");
return invoke(c, name, body, query);
}
} // namespace molo::google::cloudai
+71
View File
@@ -0,0 +1,71 @@
#pragma once
// Cloud Vision v1: image and file annotation (sync and async start),
// product search resources (product sets, products, reference images).
// Annotation is paid per image and feature.
#include <string>
#include <vector>
#include "google/cloudai/cloudai_service.h"
namespace molo::google::cloudai {
struct VisionImage {
std::string content_base64; // inline bytes, base64 (at most 10 MiB decoded)
std::string uri; // gs:// or https:// instead of content
};
struct VisionFeature {
std::string type; // Feature.type enum, e.g. LABEL_DETECTION
int max_results = 0; // 0: Google default; at most 100
};
struct FileAnnotation {
std::string gcs_uri; // gs:// PDF / TIFF / GIF
std::string content_base64; // alternative to gcs_uri
std::string mime_type; // application/pdf, image/tiff, image/gif
std::vector<int> pages; // at most 5, 1-based
};
class Vision : public CloudAiService {
public:
using CloudAiService::CloudAiService;
static bool valid_feature(const std::string& type);
clients::body::Json annotate_body(const std::vector<VisionImage>& images,
const std::vector<VisionFeature>& features) const;
// images:annotate (global endpoint). Returns the raw "responses" reply.
clients::body::Json annotate(const std::vector<VisionImage>& images,
const std::vector<VisionFeature>& features) const;
clients::body::Json annotate_file_body(const FileAnnotation& file,
const std::vector<VisionFeature>& features) const;
clients::body::Json annotate_file(const FileAnnotation& file,
const std::vector<VisionFeature>& features) const;
// Product search reads.
clients::body::Json list_product_sets() const;
clients::body::Json get_product_set(const std::string& id) const;
clients::body::Json list_products() const;
clients::body::Json get_product(const std::string& id) const;
clients::body::Json list_reference_images(const std::string& product_id) const;
// Product search writes: explicit, no defaults.
clients::body::Json create_product_set(const std::string& id, const std::string& display_name) const;
clients::body::Json delete_product_set(const std::string& id) const;
clients::body::Json create_product(const std::string& id, const std::string& display_name,
const std::string& category) const;
clients::body::Json delete_product(const std::string& id) const;
Operation get_operation(const std::string& name) const;
clients::body::Json call(Call c, const std::string& name, const clients::body::Json* body = nullptr,
const Query& query = {}) const;
std::string product_set_name(const std::string& id) const;
std::string product_name(const std::string& id) const;
};
} // namespace molo::google::cloudai
@@ -0,0 +1,60 @@
// Routes: POST /google/cloudai/{video/annotate,documentai/*,speech/*,tts/*,operation,call}
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
const char* j = "application/json";
// video
std::string v = "{\"inputUri\":\"gs://b/v.mp4\",\"features\":[\"LABEL_DETECTION\"]";
check(f.client->post("/google/cloudai/video/annotate", v + "}", j).status == 200, "video dry run");
check(f.client->post("/google/cloudai/video/annotate", v + ",\"confirm\":true}", j).status == 503, "video confirmed is 503");
check(f.client->post("/google/cloudai/video/annotate", "{\"inputUri\":\"gs://b/v\",\"features\":[\"X\"]}", j).status == 422, "bad feature");
check(f.client->post("/google/cloudai/video/annotate", "{}", j).status == 422, "features required");
// document ai
std::string d = "{\"processorId\":\"abc\",\"gcsUri\":\"gs://b/d.pdf\",\"mimeType\":\"application/pdf\"";
auto dd = f.client->post("/google/cloudai/documentai/process", d + "}", j);
check(dd.status == 200 && contains(dd.body, "gcsDocument"), "process dry run");
check(f.client->post("/google/cloudai/documentai/process", d + ",\"confirm\":true}", j).status == 503, "process confirmed is 503");
auto inline_doc = f.client->post("/google/cloudai/documentai/process",
"{\"processorId\":\"abc\",\"content\":\"" + std::string(1000, 'A') + "\",\"mimeType\":\"application/pdf\"}", j);
check(inline_doc.status == 200 && contains(inline_doc.body, "1000 characters") && !contains(inline_doc.body, "AAAAAAAAAA"), "long content is not echoed");
check(f.client->post("/google/cloudai/documentai/process", "{\"processorId\":\"a/b\",\"gcsUri\":\"gs://b/d\",\"mimeType\":\"application/pdf\"}", j).status == 422, "bad processor id");
check(f.client->post("/google/cloudai/documentai/process", "{\"processorId\":\"a\",\"gcsUri\":\"gs://b/d\",\"mimeType\":\"x/y\"}", j).status == 422, "bad mime");
check(f.client->post("/google/cloudai/documentai/list", "{\"what\":\"processors\",\"location\":\"eu\"}", j).status == 503, "list is 503");
check(f.client->post("/google/cloudai/documentai/list", "{\"what\":\"versions\"}", j).status == 422, "versions need processorId");
// speech
std::string s = "{\"languages\":[\"nl-NL\"],\"uri\":\"gs://b/a.wav\"";
check(f.client->post("/google/cloudai/speech/recognize", s + "}", j).status == 200, "recognize dry run");
check(f.client->post("/google/cloudai/speech/recognize", s + ",\"confirm\":true}", j).status == 503, "recognize confirmed is 503");
check(f.client->post("/google/cloudai/speech/recognize", "{\"uri\":\"gs://b/a\"}", j).status == 422, "languages required");
check(f.client->post("/google/cloudai/speech/recognize", "{\"languages\":[\"nl\"],\"uri\":\"gs://b/a\",\"model\":\"Bad Model\"}", j).status == 422, "bad model");
check(f.client->post("/google/cloudai/speech/list", "{\"what\":\"recognizers\"}", j).status == 503, "speech list is 503");
// long audio
std::string t = "{\"text\":\"Hello\",\"language\":\"en-US\",\"outputUri\":\"gs://b/o.wav\"";
auto td = f.client->post("/google/cloudai/tts/long", t + "}", j);
check(td.status == 200 && contains(td.body, "synthesizeLongAudio"), "long audio dry run");
check(f.client->post("/google/cloudai/tts/long", t + ",\"confirm\":true}", j).status == 503, "long audio confirmed is 503");
check(f.client->post("/google/cloudai/tts/long", "{\"text\":\"x\",\"language\":\"en-US\",\"outputUri\":\"o.wav\"}", j).status == 422, "bad outputUri");
check(f.client->post("/google/cloudai/tts/voices", "{}", j).status == 503, "voices is 503");
check(f.client->post("/google/cloudai/tts/voices", "{\"language\":\"n l\"}", j).status == 422, "bad language");
// operation
check(f.client->post("/google/cloudai/operation", "{\"api\":\"video\",\"name\":\"projects/1/locations/us-east1/operations/2\"}", j).status == 503, "operation get is 503");
check(f.client->post("/google/cloudai/operation", "{\"api\":\"video\",\"name\":\"operations/2\"}", j).status == 422, "bad operation name");
check(f.client->post("/google/cloudai/operation", "{\"api\":\"other\",\"name\":\"x\"}", j).status == 422, "bad api");
// generic call
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.list\",\"name\":\"projects/p/locations/global\"}", j).status == 503, "GET row runs, 503");
auto cd = f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.delete\",\"name\":\"projects/p/locations/global/glossaries/g\"}", j);
check(cd.status == 200 && contains(cd.body, "\"dry_run\":true") && contains(cd.body, "\"http\":\"DELETE\""), "DELETE row is a dry run");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.delete\",\"name\":\"projects/p/locations/global/glossaries/g\",\"confirm\":true}", j).status == 503, "confirmed DELETE is 503");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"nope\"}", j).status == 422, "unknown rpc");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.translateText\",\"name\":\"projects/p/locations/global\",\"body\":{\"bogus\":1}}", j).status == 422, "unknown request field");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.list\",\"name\":\"projects/p\"}", j).status == 422, "name not matching the pattern");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.list\",\"name\":\"projects/p/locations/global\",\"query\":{\"bogus\":\"1\"}}", j).status == 422, "unknown query parameter");
check(f.client->get("/google/cloudai/call").status == 405, "get is 405");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/cloudai/media");
}
@@ -0,0 +1,25 @@
// Routes: GET /google/cloudai, /google/cloudai/status, /google/cloudai/calls
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
auto page = f.client->get("/google/cloudai");
check(page.status == 200, "page 200 without a credential");
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
check(contains(page.body, "Cloud AI") && contains(page.body, "viewport"), "page and mobile viewport");
check(contains(page.body, "/embedded/google_cloudai.js"), "script wired");
auto status = f.client->get("/google/cloudai/status");
check(status.status == 200 && contains(status.body, "\"configured\":false"), "status without credential");
check(contains(status.body, "documentai v1") && contains(status.body, "mediatranslation"), "lists apis and the gap");
auto calls = f.client->get("/google/cloudai/calls");
check(calls.status == 200 && contains(calls.body, "translate.projects.locations.translateText"), "call table");
check(contains(calls.body, "documentai.projects.locations.processors.process"), "document ai rows");
check(f.client->post("/google/cloudai", "{}", "application/json").status == 405, "post page is 405");
check(f.client->post("/google/cloudai/status", "{}", "application/json").status == 405, "post status is 405");
check(rig.mock->send_count() == 0, "no network");
return finish("google/cloudai/page");
}
@@ -0,0 +1,41 @@
// Routes: POST /google/cloudai/translate{,/detect,/list,/glossary}
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
const char* j = "application/json";
auto dry = f.client->post("/google/cloudai/translate", "{\"texts\":[\"hi\"],\"target\":\"nl\"}", j);
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "targetLanguageCode"), "default is a dry run");
auto real = f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"nl\",\"confirm\":true}", j);
check(real.status == 503 && contains(real.body, "google_not_configured"), "confirmed without credential is 503");
check(f.client->post("/google/cloudai/translate", "{}", j).status == 422, "missing target is 422");
check(f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"n l\"}", j).status == 422, "bad language is 422");
check(f.client->post("/google/cloudai/translate", "{\"texts\":[],\"target\":\"nl\"}", j).status == 422, "no text is 422");
check(f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"nl\",\"confirm\":\"yes\"}", j).status == 422, "non-bool confirm is 422");
check(f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"nl\",\"location\":\"../x\"}", j).status == 422, "bad location is 422");
check(f.client->post("/google/cloudai/translate", "not json", j).status == 422, "bad json is 422");
check(f.client->get("/google/cloudai/translate").status == 405, "get is 405");
check(f.client->post("/google/cloudai/translate/detect", "{\"text\":\"bonjour\"}", j).status == 200, "detect dry run");
auto det = f.client->post("/google/cloudai/translate/detect", "{\"text\":\"bonjour\",\"confirm\":true}", j);
check(det.status == 503, "detect confirmed is 503");
check(f.client->post("/google/cloudai/translate/detect", "{}", j).status == 422, "detect needs text");
auto lang = f.client->post("/google/cloudai/translate/list", "{\"what\":\"languages\"}", j);
check(lang.status == 503 && contains(lang.body, "google_not_configured"), "read without credential is 503");
check(f.client->post("/google/cloudai/translate/list", "{\"what\":\"nope\"}", j).status == 422, "bad what is 422");
check(f.client->post("/google/cloudai/translate/list", "{\"what\":\"glossaries\",\"location\":\"bad loc\"}", j).status == 422, "bad location");
auto gdry = f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"delete\",\"id\":\"g1\"}", j);
check(gdry.status == 200 && contains(gdry.body, "\"dry_run\":true"), "glossary delete is a dry run");
auto gmake = f.client->post("/google/cloudai/translate/glossary",
"{\"action\":\"create\",\"id\":\"g1\",\"inputUri\":\"gs://b/g.csv\",\"source\":\"en\",\"target\":\"nl\"}", j);
check(gmake.status == 200 && contains(gmake.body, "languagePair"), "glossary create is a dry run");
auto greal = f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"delete\",\"id\":\"g1\",\"confirm\":true}", j);
check(greal.status == 503, "confirmed delete without credential is 503");
check(f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"create\",\"id\":\"g1\",\"inputUri\":\"http://x\",\"source\":\"en\",\"target\":\"nl\"}", j).status == 422, "bad inputUri is 422");
check(f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"get\",\"id\":\"a/b\"}", j).status == 422, "bad id is 422");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/cloudai/translate");
}
@@ -0,0 +1,37 @@
// Routes: POST /google/cloudai/vision/{annotate,list}, /google/cloudai/language/analyze
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
const char* j = "application/json";
std::string ok = "{\"images\":[{\"uri\":\"gs://b/i.png\"}],\"features\":[{\"type\":\"LABEL_DETECTION\",\"maxResults\":3}]";
auto dry = f.client->post("/google/cloudai/vision/annotate", ok + "}", j);
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "gcsImageUri"), "annotate dry run");
check(f.client->post("/google/cloudai/vision/annotate", ok + ",\"confirm\":true}", j).status == 503, "confirmed is 503");
check(f.client->post("/google/cloudai/vision/annotate", "{}", j).status == 422, "images required");
check(f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{\"uri\":\"gs://b/i\"}],\"features\":[\"NOPE\"]}", j).status == 422, "unknown feature is 422");
check(f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{}],\"features\":[\"LABEL_DETECTION\"]}", j).status == 422, "empty image is 422");
check(f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{\"uri\":\"gs://b/i\"}],\"features\":[{\"type\":\"LABEL_DETECTION\",\"maxResults\":500}]}", j).status == 422, "maxResults over cap is 422");
auto small = f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{\"content\":\"" + std::string(100, 'A') + "\"}],\"features\":[\"LABEL_DETECTION\"]}", j);
check(small.status == 200, "small inline image dry run");
auto sets = f.client->post("/google/cloudai/vision/list", "{\"what\":\"productSets\"}", j);
check(sets.status == 503, "list without credential is 503");
check(f.client->post("/google/cloudai/vision/list", "{\"what\":\"referenceImages\"}", j).status == 422, "productId required");
check(f.client->get("/google/cloudai/vision/list").status == 405, "get is 405");
auto nl = f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\",\"content\":\"great\"}", j);
check(nl.status == 200 && contains(nl.body, "\"dry_run\":true"), "analyze dry run");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\",\"content\":\"great\",\"confirm\":true}", j).status == 503, "confirmed is 503");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"syntax\",\"content\":\"great\",\"confirm\":true}", j).status == 503, "v1 syntax is 503");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\"}", j).status == 422, "content required");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"other\",\"content\":\"x\"}", j).status == 422, "unknown kind");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"annotate\",\"content\":\"x\"}", j).status == 422, "annotate needs a feature");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"annotate\",\"content\":\"x\",\"classifyText\":true}", j).status == 200, "annotate with a feature");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\",\"content\":\"x\",\"type\":\"MD\"}", j).status == 422, "bad type");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/cloudai/vision_language");
}
+496
View File
@@ -0,0 +1,496 @@
// cloudai: deterministic local tests. The call table is generated from and
// checked against Google's Discovery documents (tools/check_discovery.py);
// here: table integrity, path rendering and validation, endpoint selection,
// request bodies as the client really sends them (a configured client over a
// MockTransport with no scripted reply: the request is recorded, the missing
// reply surfaces as UNAVAILABLE), caps, and the no-credential behavior.
// No Google reply is ever scripted.
#include <memory>
#include <set>
#include "framework.h"
#include "clients/transports/mock_transport.h"
#include "google/cloudai/calls.h"
#include "google/cloudai/caps.h"
#include "google/cloudai/document_ai.h"
#include "google/cloudai/long_audio.h"
#include "google/cloudai/natural_language.h"
#include "google/cloudai/speech_v2.h"
#include "google/cloudai/translation.h"
#include "google/cloudai/video_intelligence.h"
#include "google/cloudai/vision.h"
#include "google/credentials.h"
using namespace molo::google;
using namespace molo::google::cloudai;
using molo::clients::body::Json;
using molo::clients::transports::MockTransport;
namespace {
struct Rig {
MockTransport* mock;
std::shared_ptr<GoogleClient> client;
CloudContext ctx;
explicit Rig(bool with_key = true, const std::string& location = "global") {
auto t = std::make_unique<MockTransport>();
mock = t.get();
client = std::make_shared<GoogleClient>(
std::move(t), with_key ? Credentials::api_key("AIzaTESTKEY") : nullptr);
ctx.project = "p1";
ctx.location = location;
}
MockTransport::SeenRequest last() const { return mock->seen().back(); }
};
// Runs f; returns the GoogleError it threw (status kept) or null.
template <typename F>
std::unique_ptr<GoogleError> thrown(F&& f) {
try {
f();
} catch (const GoogleError& e) {
return std::make_unique<GoogleError>(e);
}
return nullptr;
}
template <typename F>
bool invalid(F&& f) {
auto e = thrown(f);
return e && e->http_status() == 400 && e->status() == "INVALID_ARGUMENT";
}
// Passed validation, died at the missing reply (the request was recorded).
template <typename F>
bool sent(Rig& rig, F&& f) {
std::size_t before = rig.mock->send_count();
auto e = thrown(f);
return e && e->status() == "UNAVAILABLE" && rig.mock->send_count() == before + 1;
}
} // namespace
TEST(cloudai, table_is_indexed_by_enum) {
ASSERT_TRUE(call_count() > 150);
for (std::size_t i = 0; i < call_count(); ++i) ASSERT_EQ(static_cast<std::size_t>(spec_at(i).call), i);
std::set<std::string> ids;
for (std::size_t i = 0; i < call_count(); ++i) ids.insert(spec_at(i).rpc_id);
ASSERT_EQ(ids.size(), call_count());
ASSERT_TRUE(schema_count() > 40);
}
TEST(cloudai, every_row_renders_with_a_matching_name) {
// Build a name that satisfies the row's own pattern and render it.
for (std::size_t i = 0; i < call_count(); ++i) {
const CallSpec& s = spec_at(i);
std::string name;
if (*s.path_var) {
std::string p = s.pattern;
p = p.substr(1, p.size() - 2); // strip ^ $
for (std::size_t at = p.find("[^/]+"); at != std::string::npos; at = p.find("[^/]+"))
p.replace(at, 5, "x1");
for (std::size_t at = p.find(".*"); at != std::string::npos; at = p.find(".*")) p.replace(at, 2, "x1");
std::string out = p;
name = out;
}
std::string path = render(s.call, name);
ASSERT_TRUE(path.size() > 1 && path[0] == '/');
ASSERT_TRUE(path.find('{') == std::string::npos);
ASSERT_TRUE(path.find("v1/") != 1 && path.find("v2/") != 1 && path.find("v3/") != 1);
}
}
TEST(cloudai, render_rejects_bad_names) {
Call c = Call::kTranslateLocationsGlossariesGet;
ASSERT_EQ(render(c, "projects/p/locations/global/glossaries/g"),
"/projects/p/locations/global/glossaries/g");
for (const char* bad : {"", "projects/p/locations/global", "projects/p/locations/global/glossaries/g/x",
"projects/../locations/global/glossaries/g", "projects/p/locations/global/glossaries/g?x=1",
"projects/p/locations/global/glossaries/g#f", "projects/p/locations/global/glossaries/a b",
"projects//locations/global/glossaries/g", "locations/global/glossaries/g"})
ASSERT_TRUE(invalid([&] { render(c, bad); }));
ASSERT_TRUE(invalid([&] { render(c, std::string(600, 'a')); }));
ASSERT_TRUE(invalid([&] { render(Call::kVisionImagesAnnotate, "projects/p"); })); // takes no name
}
TEST(cloudai, query_and_body_are_checked_against_the_row) {
ASSERT_EQ(query_of(Call::kTranslateLocationsGlossariesList, {{"pageSize", "5"}, {"filter", "a b"}}),
"?pageSize=5&filter=a%20b");
ASSERT_TRUE(invalid([&] { query_of(Call::kTranslateLocationsGlossariesList, {{"bogus", "1"}}); }));
ASSERT_TRUE(invalid([&] { query_of(Call::kTranslateLocationsGlossariesList, {{"pageSize", std::string(3000, '1')}}); }));
Json ok = Json::object();
ok.set("contents", Json::array());
check_body(Call::kTranslateLocationsTranslateText, &ok);
Json bad = Json::object();
bad.set("nope", Json(1));
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsTranslateText, &bad); }));
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsGlossariesGet, &ok); })); // GET takes no body
Json arr = Json::array();
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsTranslateText, &arr); }));
Json big = Json::object();
big.set("contents", Json(std::string(CloudAiCaps::kMaxBodyBytes + 10, 'a')));
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsTranslateText, &big); }));
}
TEST(cloudai, endpoints_are_regional_where_discovery_lists_them) {
ASSERT_EQ(endpoint_of(Call::kTranslateLocationsTranslateText, "").base_url,
"https://translation.googleapis.com/v3");
ASSERT_EQ(endpoint_of(Call::kVisionImagesAnnotate, "").base_url, "https://vision.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kLanguageDocumentsAnalyzeSentiment, "").base_url,
"https://language.googleapis.com/v2");
ASSERT_EQ(endpoint_of(Call::kLanguageV1DocumentsAnalyzeSyntax, "").base_url,
"https://language.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kVideoVideosAnnotate, "").base_url, "https://videointelligence.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kTtsVoicesList, "").base_url, "https://texttospeech.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kDocumentaiLocationsProcessorsGet, "projects/p/locations/eu/processors/x").base_url,
"https://documentai.eu.rep.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kDocumentaiLocationsProcessorsGet, "projects/p/locations/mars/processors/x").base_url,
"https://documentai.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kSpeechLocationsRecognizersGet, "projects/p/locations/europe-west4/recognizers/_").base_url,
"https://speech.europe-west4.rep.googleapis.com/v2");
ASSERT_EQ(endpoint_of(Call::kSpeechLocationsRecognizersGet, "projects/p/locations/global/recognizers/_").base_url,
"https://speech.googleapis.com/v2");
ASSERT_EQ(endpoint_of(Call::kSpeechLocationsRecognizersGet, "projects/p/locations/eu/recognizers/_").base_url,
"https://eu-speech.googleapis.com/v2");
}
TEST(cloudai, no_credential_is_503_after_validation) {
Rig rig(false);
Translation t(rig.client, rig.ctx);
TranslateRequest r;
r.texts = {"hello"};
r.target = "nl";
auto e = thrown([&] { t.translate(r); });
ASSERT_TRUE(e != nullptr);
ASSERT_EQ(e->http_status(), 503);
ASSERT_EQ(e->status(), "NOT_CONFIGURED");
r.target = "";
ASSERT_TRUE(invalid([&] { t.translate(r); })); // validation wins over the credential check
ASSERT_EQ(rig.mock->send_count(), 0u);
}
TEST(cloudai, translate_request_as_sent) {
Rig rig;
Translation t(rig.client, rig.ctx);
TranslateRequest r;
r.texts = {"hello", "world"};
r.target = "nl";
r.source = "en";
r.glossary = "projects/p1/locations/global/glossaries/g1";
r.glossary_ignore_case = true;
ASSERT_TRUE(sent(rig, [&] { t.translate(r); }));
auto seen = rig.last();
ASSERT_EQ(seen.method, "POST");
ASSERT_TRUE(seen.target.find("/v3/projects/p1/locations/global:translateText") != std::string::npos);
Json body = Json::parse(seen.body);
ASSERT_EQ(body.find("targetLanguageCode")->as_string(), "nl");
ASSERT_EQ(body.find("sourceLanguageCode")->as_string(), "en");
ASSERT_EQ(body.find("contents")->as_array().size(), 2u);
ASSERT_TRUE(body.find("glossaryConfig")->find("ignoreCase")->as_bool());
ASSERT_TRUE(seen.headers.get("x-goog-api-key") == "AIzaTESTKEY");
ASSERT_TRUE(seen.target.find("AIzaTESTKEY") == std::string::npos);
}
TEST(cloudai, translate_validation) {
Rig rig;
Translation t(rig.client, rig.ctx);
TranslateRequest r;
r.target = "nl";
ASSERT_TRUE(invalid([&] { t.translate(r); })); // no texts
r.texts = {""};
ASSERT_TRUE(invalid([&] { t.translate(r); })); // empty text
r.texts = {std::string(CloudAiCaps::kMaxTextChars + 1, 'a')};
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.texts = std::vector<std::string>(CloudAiCaps::kMaxTexts + 1, "a");
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.texts = {"a"};
r.target = "n l";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.target = "nl";
r.mime_type = "application/pdf";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.mime_type = "";
r.glossary = "../x";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.glossary = "";
r.model = "base";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
ASSERT_EQ(rig.mock->send_count(), 0u);
// exactly the cap passes
r.model = "";
r.texts = {std::string(30000, 'a')};
ASSERT_TRUE(sent(rig, [&] { t.translate(r); }));
}
TEST(cloudai, translation_reads_and_glossary_writes) {
Rig rig;
Translation t(rig.client, rig.ctx);
ASSERT_TRUE(sent(rig, [&] { t.supported_languages("nl"); }));
ASSERT_TRUE(rig.last().target.find("/v3/projects/p1/locations/global/supportedLanguages?displayLanguageCode=nl") !=
std::string::npos);
ASSERT_TRUE(sent(rig, [&] { t.list_glossaries(); }));
ASSERT_EQ(rig.last().method, "GET");
ASSERT_TRUE(rig.last().target.find("/glossaries?pageSize=50") != std::string::npos);
ASSERT_TRUE(invalid([&] { t.get_glossary("a/b"); }));
ASSERT_TRUE(invalid([&] { t.supported_languages("n l"); }));
GlossaryDraft d;
d.id = "g1";
d.input_uri = "gs://bucket/g.csv";
d.source_language = "en";
d.target_language = "nl";
Json b = t.glossary_body(d);
ASSERT_EQ(b.find("name")->as_string(), "projects/p1/locations/global/glossaries/g1");
ASSERT_EQ(b.find("languagePair")->find("targetLanguageCode")->as_string(), "nl");
ASSERT_TRUE(sent(rig, [&] { t.create_glossary_start(d); }));
ASSERT_EQ(rig.last().method, "POST");
d.input_uri = "http://x/g.csv";
ASSERT_TRUE(invalid([&] { t.create_glossary_start(d); }));
d.input_uri = "gs://bucket/g.csv";
d.language_codes = {"en", "nl"};
ASSERT_TRUE(invalid([&] { t.glossary_body(d); })); // pair and set together
d.source_language = d.target_language = "";
ASSERT_TRUE(t.glossary_body(d).find("languageCodesSet") != nullptr);
ASSERT_TRUE(sent(rig, [&] { t.delete_glossary_start("g1"); }));
ASSERT_EQ(rig.last().method, "DELETE");
ASSERT_TRUE(invalid([&] { t.call(Call::kVisionImagesAnnotate, ""); })); // wrong family
}
TEST(cloudai, vision_bodies_and_validation) {
Rig rig(true, "us");
Vision v(rig.client, rig.ctx);
VisionImage im;
im.uri = "gs://b/o.png";
VisionFeature f;
f.type = "LABEL_DETECTION";
f.max_results = 5;
Json b = v.annotate_body({im}, {f});
const Json& req = b.find("requests")->as_array()[0];
ASSERT_EQ(req.find("image")->find("source")->find("gcsImageUri")->as_string(), "gs://b/o.png");
ASSERT_EQ(req.find("features")->as_array()[0].find("maxResults")->as_number(), 5.0);
im.uri = "https://example.com/a.png";
ASSERT_TRUE(v.annotate_body({im}, {f}).find("requests")->as_array()[0].find("image")->find("source")->find("imageUri") !=
nullptr);
ASSERT_TRUE(sent(rig, [&] { v.annotate({im}, {f}); }));
ASSERT_TRUE(rig.last().target.find("/v1/images:annotate") != std::string::npos);
f.type = "NOT_A_FEATURE";
ASSERT_TRUE(invalid([&] { v.annotate({im}, {f}); }));
f.type = "LABEL_DETECTION";
f.max_results = 101;
ASSERT_TRUE(invalid([&] { v.annotate({im}, {f}); }));
f.max_results = 1;
ASSERT_TRUE(invalid([&] { v.annotate({}, {f}); }));
ASSERT_TRUE(invalid([&] { v.annotate({im}, {}); }));
ASSERT_TRUE(invalid([&] { v.annotate(std::vector<VisionImage>(CloudAiCaps::kMaxImages + 1, im), {f}); }));
VisionImage both;
both.uri = "gs://b/o";
both.content_base64 = "QUJD";
ASSERT_TRUE(invalid([&] { v.annotate({both}, {f}); }));
VisionImage junk;
junk.content_base64 = "not base64!";
ASSERT_TRUE(invalid([&] { v.annotate({junk}, {f}); }));
VisionImage huge;
huge.content_base64 = std::string(CloudAiCaps::kMaxAudioBytes * 2, 'A');
ASSERT_TRUE(invalid([&] { v.annotate({huge}, {f}); }));
VisionImage badu;
badu.uri = "ftp://x";
ASSERT_TRUE(invalid([&] { v.annotate({badu}, {f}); }));
// files
FileAnnotation file;
file.gcs_uri = "gs://b/doc.pdf";
file.mime_type = "application/pdf";
file.pages = {1, 2};
ASSERT_TRUE(sent(rig, [&] { v.annotate_file(file, {f}); }));
ASSERT_TRUE(rig.last().target.find("/v1/files:annotate") != std::string::npos);
file.mime_type = "text/plain";
ASSERT_TRUE(invalid([&] { v.annotate_file(file, {f}); }));
file.mime_type = "application/pdf";
file.pages = {1, 2, 3, 4, 5, 6};
ASSERT_TRUE(invalid([&] { v.annotate_file(file, {f}); }));
// product search
ASSERT_TRUE(sent(rig, [&] { v.list_product_sets(); }));
ASSERT_TRUE(rig.last().target.find("/v1/projects/p1/locations/us/productSets") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { v.create_product_set("ps1", "My set"); }));
ASSERT_TRUE(rig.last().target.find("productSetId=ps1") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { v.create_product("p9", "Shoe", "apparel-v2"); }));
ASSERT_TRUE(rig.last().target.find("productId=p9") != std::string::npos);
ASSERT_TRUE(invalid([&] { v.create_product("p9", "Shoe", "cars"); }));
ASSERT_TRUE(sent(rig, [&] { v.delete_product("p9"); }));
ASSERT_EQ(rig.last().method, "DELETE");
ASSERT_TRUE(invalid([&] { v.get_product("../x"); }));
}
TEST(cloudai, natural_language_bodies) {
Rig rig;
NaturalLanguage nl(rig.client, rig.ctx);
NlDocument d;
d.content = "I love this.";
d.language = "en";
ASSERT_TRUE(sent(rig, [&] { nl.analyze_sentiment(d, "UTF8"); }));
ASSERT_TRUE(rig.last().target.find("/v2/documents:analyzeSentiment") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("document")->find("type")->as_string(), "PLAIN_TEXT");
ASSERT_EQ(body.find("encodingType")->as_string(), "UTF8");
ASSERT_TRUE(sent(rig, [&] { nl.analyze_syntax(d); }));
ASSERT_TRUE(rig.last().target.find("/v1/documents:analyzeSyntax") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { nl.classify_text(d); }));
ASSERT_TRUE(sent(rig, [&] { nl.moderate_text(d); }));
NlFeatures f;
f.sentiment = f.entities = true;
ASSERT_TRUE(sent(rig, [&] { nl.annotate_text(d, f); }));
ASSERT_TRUE(Json::parse(rig.last().body).find("features")->find("extractEntities") != nullptr);
ASSERT_TRUE(invalid([&] { nl.annotate_text(d, NlFeatures{}); }));
ASSERT_TRUE(invalid([&] { nl.analyze_sentiment(d, "UTF99"); }));
ASSERT_TRUE(invalid([&] { nl.classify_text(NlDocument{}); })); // empty
NlDocument html = d;
html.type = "MARKDOWN";
ASSERT_TRUE(invalid([&] { nl.analyze_entities(html); }));
NlDocument big = d;
big.content = std::string(CloudAiCaps::kMaxNlTextChars + 1, 'a');
ASSERT_TRUE(invalid([&] { nl.analyze_entities(big); }));
NlDocument both = d;
both.gcs_uri = "gs://b/o.txt";
ASSERT_TRUE(invalid([&] { nl.analyze_entities(both); }));
// classify and moderate have no encodingType
ASSERT_TRUE(Json::parse(rig.mock->seen()[2].body).find("encodingType") == nullptr);
}
TEST(cloudai, video_intelligence) {
Rig rig(true, "us-east1");
VideoIntelligence vi(rig.client, rig.ctx);
VideoRequest r;
r.input_uri = "gs://b/v.mp4";
r.features = {"LABEL_DETECTION", "SHOT_CHANGE_DETECTION"};
r.location_id = "us-east1";
ASSERT_TRUE(sent(rig, [&] { vi.annotate_start(r); }));
ASSERT_TRUE(rig.last().target.find("/v1/videos:annotate") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("features")->as_array().size(), 2u);
ASSERT_EQ(body.find("locationId")->as_string(), "us-east1");
r.features = {"FLY"};
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
r.features = {};
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
r.features = {"LABEL_DETECTION"};
r.input_uri = "http://x";
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
r.input_uri = "gs://b/v.mp4";
r.location_id = "mars-1";
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
ASSERT_TRUE(sent(rig, [&] { vi.get_operation("projects/1/locations/us-east1/operations/77"); }));
ASSERT_TRUE(invalid([&] { vi.get_operation("operations/77"); }));
ASSERT_TRUE(sent(rig, [&] { vi.list_operations(); }));
ASSERT_TRUE(sent(rig, [&] { vi.cancel_operation("projects/1/locations/us-east1/operations/77"); }));
ASSERT_TRUE(rig.last().target.find(":cancel") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { vi.delete_operation("projects/1/locations/us-east1/operations/77"); }));
ASSERT_EQ(rig.last().method, "DELETE");
}
TEST(cloudai, document_ai) {
Rig rig(true, "eu");
DocumentAi dai(rig.client, rig.ctx);
ProcessRequest r;
r.processor_id = "abc123";
r.content_base64 = "JVBERi0=";
r.mime_type = "application/pdf";
ASSERT_TRUE(sent(rig, [&] { dai.process(r); }));
ASSERT_TRUE(rig.last().target.find("/v1/projects/p1/locations/eu/processors/abc123:process") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("rawDocument")->find("mimeType")->as_string(), "application/pdf");
ASSERT_TRUE(body.find("skipHumanReview")->as_bool());
r.version_id = "pretrained-ocr-v2.0";
ASSERT_TRUE(sent(rig, [&] { dai.process(r); }));
ASSERT_TRUE(rig.last().target.find("/processorVersions/pretrained-ocr-v2.0:process") != std::string::npos);
r.version_id = "";
r.gcs_uri = "gs://b/o.pdf";
ASSERT_TRUE(invalid([&] { dai.process(r); })); // content and uri
r.content_base64 = "";
ASSERT_TRUE(dai.process_body(r).find("gcsDocument") != nullptr);
r.mime_type = "application/zip";
ASSERT_TRUE(invalid([&] { dai.process(r); }));
r.mime_type = "application/pdf";
r.processor_id = "a/b";
ASSERT_TRUE(invalid([&] { dai.process(r); }));
ASSERT_TRUE(sent(rig, [&] { dai.list_processors(); }));
ASSERT_TRUE(sent(rig, [&] { dai.fetch_processor_types(); }));
ASSERT_TRUE(rig.last().target.find(":fetchProcessorTypes") != std::string::npos);
ASSERT_TRUE(invalid([&] { dai.create_processor("x", "form parser"); }));
ASSERT_TRUE(sent(rig, [&] { dai.create_processor("x", "FORM_PARSER_PROCESSOR"); }));
ASSERT_TRUE(sent(rig, [&] { dai.delete_processor_start("abc123"); }));
ASSERT_EQ(rig.last().method, "DELETE");
ASSERT_TRUE(invalid([&] { dai.batch_process_start("abc123", Json::parse("{\"bogus\":1}")); }));
}
TEST(cloudai, speech_v2) {
Rig rig(true, "europe-west4");
SpeechV2 sp(rig.client, rig.ctx);
SpeechRequest r;
r.content_base64 = "UklGRg==";
r.languages = {"nl-NL"};
ASSERT_TRUE(sent(rig, [&] { sp.recognize(r); }));
ASSERT_TRUE(rig.last().target.find("/v2/projects/p1/locations/europe-west4/recognizers/_:recognize") !=
std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("config")->find("model")->as_string(), "chirp_2");
ASSERT_TRUE(body.find("config")->find("autoDecodingConfig") != nullptr);
r.languages = {};
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
r.languages = {"a", "b", "c", "d"};
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
r.languages = {"nl-NL"};
r.model = "Chirp 2";
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
r.model = "chirp_2";
r.uri = "gs://b/a.wav";
ASSERT_TRUE(invalid([&] { sp.recognize(r); })); // content and uri
r.content_base64 = "";
ASSERT_TRUE(sent(rig, [&] { sp.recognize(r); }));
r.uri = "";
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
ASSERT_TRUE(sent(rig, [&] { sp.list_recognizers(); }));
ASSERT_TRUE(sent(rig, [&] { sp.get_config(); }));
ASSERT_TRUE(rig.last().target.find("/locations/europe-west4/config") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { sp.create_recognizer_start("r1", "chirp_2", {"en-US"}); }));
ASSERT_TRUE(rig.last().target.find("recognizerId=r1") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { sp.delete_recognizer_start("r1"); }));
ASSERT_EQ(rig.last().method, "DELETE");
}
TEST(cloudai, long_audio) {
Rig rig(true, "global");
LongAudio la(rig.client, rig.ctx);
LongAudioRequest r;
r.text = "Hello there.";
r.language = "en-US";
r.output_uri = "gs://b/out.wav";
ASSERT_TRUE(sent(rig, [&] { la.synthesize_start(r); }));
ASSERT_TRUE(rig.last().target.find("/v1/projects/p1/locations/global:synthesizeLongAudio") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("audioConfig")->find("audioEncoding")->as_string(), "LINEAR16");
ASSERT_EQ(body.find("outputGcsUri")->as_string(), "gs://b/out.wav");
r.output_uri = "out.wav";
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.output_uri = "gs://b/out.wav";
r.text = "";
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.text = std::string(CloudAiCaps::kMaxLongAudioChars + 1, 'a');
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.text = "x";
r.encoding = "WAVE";
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.encoding = "MP3";
r.ssml = true;
ASSERT_TRUE(la.request_body(r).find("input")->find("ssml") != nullptr);
ASSERT_TRUE(sent(rig, [&] { la.voices("nl-NL"); }));
ASSERT_TRUE(rig.last().target.find("/v1/voices?languageCode=nl-NL") != std::string::npos);
ASSERT_TRUE(invalid([&] { la.voices("n l"); }));
}
TEST(cloudai, invoke_is_generic_over_the_table) {
Rig rig;
Translation t(rig.client, rig.ctx);
Json body = Json::parse("{\"bogus\":\"x\"}");
ASSERT_TRUE(invalid([&] { t.invoke(Call::kTranslateLocationsAdaptiveMtTranslate, "projects/p1/locations/global", &body); }));
// a field of the request passes validation and the call goes out
Json ok = Json::parse("{\"dataset\":\"projects/p1/locations/global/adaptiveMtDatasets/d\",\"content\":[\"hi\"]}");
ASSERT_TRUE(sent(rig, [&] { t.invoke(Call::kTranslateLocationsAdaptiveMtTranslate, "projects/p1/locations/global", &ok); }));
}
@@ -0,0 +1,116 @@
// Opt-in live checks (MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN, GOOGLE_CLOUD_PROJECT).
// Read-only: lists and gets only. A disabled API is reported loudly as
// "API NOT ENABLED" and is not a failure. The single paid request (one short
// translation) additionally needs MOLO_LIVE_PAID=1. Nothing is created,
// changed or deleted.
#include <cstdlib>
#include <iostream>
#include "framework.h"
#include "google/cloudai/document_ai.h"
#include "google/cloudai/long_audio.h"
#include "google/cloudai/speech_v2.h"
#include "google/cloudai/translation.h"
#include "google/cloudai/video_intelligence.h"
#include "google/cloudai/vision.h"
#include "google/credentials.h"
using namespace molo::google;
using namespace molo::google::cloudai;
namespace {
bool ready() {
return std::getenv("MOLO_LIVE_NET") && !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() &&
!Credentials::env("GOOGLE_CLOUD_PROJECT").empty();
}
CloudContext context(const char* location) {
CloudContext c = CloudContext::from_env();
c.location = location;
return c;
}
// Runs a read; prints the outcome. Returns false for a disabled API.
template <typename F>
void probe(const char* what, F&& f) {
try {
std::size_t n = f();
std::cout << " " << what << ": 200, " << n << " item(s)\n";
} catch (const GoogleError& e) {
std::string msg = e.message();
bool disabled = e.http_status() == 403 && msg.find("permission") == std::string::npos &&
(msg.find("has not been used") != std::string::npos || msg.find("is disabled") != std::string::npos ||
e.status() == "SERVICE_DISABLED");
if (disabled) {
std::cout << " API NOT ENABLED: " << what << " (" << e.http_status() << " " << e.status() << ": " << msg.substr(0, 90) << ")\n";
return;
}
std::cout << " " << what << ": " << e.http_status() << " " << e.status() << "\n";
FAIL(std::string(what) + " failed: " + e.status());
}
}
std::size_t count(const molo::clients::body::Json& j) { return j.is_array() ? j.as_array().size() : 0; }
#define LIVE_GUARD(name) \
if (!ready()) { \
std::cout << "SKIP " name " (set MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN and GOOGLE_CLOUD_PROJECT)\n"; \
return; \
}
} // namespace
TEST(cloudai_live, translation_reads) {
LIVE_GUARD("cloudai_live.translation_reads");
auto client = GoogleClient::from_env();
Translation t(client, context("global"));
probe("translation supportedLanguages", [&] {
auto j = t.supported_languages("en");
const auto* langs = j.find("languages");
return langs && langs->is_array() ? langs->as_array().size() : 0u;
});
probe("translation glossaries.list", [&] { return count(t.list_glossaries()); });
probe("translation adaptiveMtDatasets.list", [&] { return count(t.list_adaptive_datasets()); });
probe("translation models.list", [&] { return count(t.list_models()); });
}
TEST(cloudai_live, translation_one_paid_request) {
LIVE_GUARD("cloudai_live.translation_one_paid_request");
if (!std::getenv("MOLO_LIVE_PAID")) {
std::cout << "SKIP cloudai_live.translation_one_paid_request (set MOLO_LIVE_PAID=1: one short translateText)\n";
return;
}
auto client = GoogleClient::from_env();
Translation t(client, context("global"));
TranslateRequest r;
r.texts = {"hello"};
r.target = "nl";
r.source = "en";
probe("translation translateText (paid, 5 characters)", [&] {
auto out = t.translate(r);
std::cout << " -> " << (out.empty() ? "" : out[0].text) << "\n";
ASSERT_EQ(out.size(), 1u);
return out.size();
});
}
TEST(cloudai_live, other_apis_reads) {
LIVE_GUARD("cloudai_live.other_apis_reads");
auto client = GoogleClient::from_env();
Vision v(client, context("us-west1"));
probe("vision productSets.list", [&] { return count(v.list_product_sets()); });
DocumentAi d(client, context("us"));
probe("documentai processors.list", [&] { return count(d.list_processors()); });
probe("documentai processorTypes.list", [&] { return count(d.list_processor_types()); });
SpeechV2 s(client, context("us-central1"));
probe("speech recognizers.list", [&] { return count(s.list_recognizers()); });
LongAudio la(client, context("global"));
probe("texttospeech voices.list", [&] {
auto j = la.voices("nl-NL");
const auto* voices = j.find("voices");
return voices && voices->is_array() ? voices->as_array().size() : 0u;
});
VideoIntelligence vi(client, context("us-east1"));
probe("videointelligence operations.list", [&] { return count(vi.list_operations()); });
}