Files
moloapp/src/google/discovery/model.h
T

119 lines
4.2 KiB
C++
Raw Normal View History

#pragma once
// Parsed Discovery document: parameters, methods, schemas. Pure data plus
// classification of what the engine can and cannot do with each method.
// Every shape here was read from the real documents (see SPEC.md section
// "Shapes seen"); anything else is rejected loudly by Api::parse.
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "clients/body/json.h"
namespace molo::google::discovery {
// Full: called as documented. Limited: callable, but part of the method's
// surface is not reachable (media bytes, non-JSON reply); notes say which.
// Rejected: never called; the notes say why.
enum class Support { kFull, kLimited, kRejected };
const char* support_name(Support s);
struct Param {
std::string name;
std::string type; // string integer number boolean (object: rejected)
std::string format;
std::string location; // path or query
std::string pattern;
std::string description;
bool required = false;
bool repeated = false;
bool deprecated = false;
std::vector<std::string> enum_values;
std::optional<double> minimum;
std::optional<double> maximum;
};
struct MediaUpload {
bool supported = false;
bool simple = false;
bool resumable = false;
double max_size = 0; // bytes, 0: not stated
};
struct Method {
std::string id; // bigquery.jobs.query
std::string http_method; // GET POST PUT PATCH DELETE
std::string path; // v1/{+name}:cancel (relative to base)
std::string flat_path;
std::string request_ref; // schema name, empty: no body
std::string response_ref; // schema name, empty: no body
std::map<std::string, Param> params;
std::vector<std::string> param_order;
std::vector<std::string> scopes;
MediaUpload media;
bool supports_download = false;
bool deprecated = false;
std::string streaming_type;
Support support = Support::kFull;
std::vector<std::string> notes; // why Limited or Rejected, one per cause
bool needs_oauth() const { return !scopes.empty(); }
clients::body::Json summary() const;
};
struct EndpointRow {
std::string location;
std::string url; // root URL, trailing slash
std::string description;
};
class Api {
public:
// Throws GoogleError (502 BAD_DOCUMENT) when the document is not one the
// engine can trust: wrong kind/protocol, host outside googleapis.com,
// over the method cap, duplicate method ids.
static Api parse(const clients::body::Json& doc);
const std::string& id() const { return id_; }
const std::string& name() const { return name_; }
const std::string& version() const { return version_; }
const std::string& title() const { return title_; }
const std::string& root_url() const { return root_url_; }
const std::string& service_path() const { return service_path_; }
// root_url + service_path without trailing slash.
std::string base_url() const;
const std::vector<EndpointRow>& endpoints() const { return endpoints_; }
const std::vector<Method>& methods() const { return methods_; }
const Method* find(const std::string& method_id) const;
const clients::body::Json* schema(const std::string& name) const;
std::size_t schema_count() const;
// Query parameters every method accepts, as declared by the document.
const std::vector<std::string>& global_params() const { return global_params_; }
bool batch_path_declared() const { return !batch_path_.empty(); }
bool is_operation_schema(const std::string& ref) const;
bool is_pageable(const Method& m) const;
// The array property to collect when paging m, or "" when none/ambiguous.
std::string items_key(const Method& m) const;
// Methods ending in "operations.get" usable to poll an operation.
std::vector<const Method*> operation_getters() const;
// Raw bytes of the parsed text, for cache accounting.
std::size_t raw_bytes = 0;
private:
std::string id_, name_, version_, title_, root_url_, service_path_, batch_path_;
std::vector<EndpointRow> endpoints_;
std::vector<Method> methods_;
std::map<std::string, std::size_t> index_;
clients::body::Json schemas_ = clients::body::Json::object();
std::vector<std::string> global_params_;
};
// True for an https URL whose host is googleapis.com or a subdomain of it.
bool is_google_host_url(const std::string& url);
} // namespace molo::google::discovery