43 lines
1.5 KiB
C++
43 lines
1.5 KiB
C++
#include "google/ops/api_state.h"
|
|||
|
|
|
||
|
|
namespace molo::google::ops {
|
||
|
|
|
||
|
|
std::string DisabledApi::loud() const {
|
||
|
|
return "API NOT ENABLED: " + (service.empty() ? std::string("(unnamed)") : service) +
|
||
|
|
(activation_url.empty() ? "" : ", a human enables it at " + activation_url);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string ApiState::service_in(const std::string& text) {
|
||
|
|
const std::string suffix = ".googleapis.com";
|
||
|
|
std::size_t at = 0;
|
||
|
|
while ((at = text.find(suffix, at)) != std::string::npos) {
|
||
|
|
std::size_t start = at;
|
||
|
|
while (start > 0) {
|
||
|
|
char c = text[start - 1];
|
||
|
|
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-') --start;
|
||
|
|
else break;
|
||
|
|
}
|
||
|
|
if (start < at) return text.substr(start, at + suffix.size() - start);
|
||
|
|
at += suffix.size();
|
||
|
|
}
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
std::optional<DisabledApi> ApiState::disabled(const GoogleError& e, const std::string& project) {
|
||
|
|
if (e.http_status() != 403) return std::nullopt;
|
||
|
|
const std::string& m = e.message();
|
||
|
|
bool off = e.status() == "SERVICE_DISABLED" ||
|
||
|
|
m.find("has not been used in project") != std::string::npos ||
|
||
|
|
m.find("it is disabled") != std::string::npos ||
|
||
|
|
m.find("SERVICE_DISABLED") != std::string::npos;
|
||
|
|
if (!off) return std::nullopt;
|
||
|
|
DisabledApi d;
|
||
|
|
d.service = service_in(m);
|
||
|
|
if (!d.service.empty() && !project.empty())
|
||
|
|
d.activation_url = "https://console.developers.google.com/apis/api/" + d.service +
|
||
|
|
"/overview?project=" + project;
|
||
|
|
return d;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::ops
|