Read a hostname in a loop

This commit is contained in:
James E. Blair 2019-02-09 12:53:17 -08:00
parent 00a92c15c8
commit 5105fe9e96
1 changed files with 47 additions and 14 deletions

View File

@ -23,21 +23,54 @@
using namespace std;
vector<string> split(string in);
vector<string> split(string in)
{
istringstream stream(in);
vector<string> parts;
string part;
while (getline(stream, part, '.')) {
parts.push_back(part);
}
return parts;
}
int main(int, char**)
{
web::http::client::http_client client("https://zuul.opendev.org");
auto uri = web::uri_builder("/api/tenant/openstack/build");
uri.append_path("75031cad206c4014ad7a3387091d15ab");
auto response = client.request(
web::http::methods::GET, uri.to_string()).get();
// body is a web::json::value
auto body = response.extract_json().get();
cout << response.status_code() << endl;
cout << body.serialize() << endl;
// body["log_url"] returns a web::json::value
cout << "The log url is " << body["log_url"] << endl;
// body["log_url"].as_string() returns a const std::string&
cout << "The log url is " << body["log_url"].as_string() << endl;
// body.at("log_url").as_string() returns a const std::string&
cout << "The log url is " << body.at("log_url").as_string() << endl;
string hostname;
while (getline(cin, hostname)) {
// Expected hostname:
// site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
// Apache will drop "preview.opendev.org", so our expected input will be:
// site.75031cad206c4014ad7a3387091d15ab.openstack
auto parts = split(hostname);
if (parts.size() < 3) {
cout << "not enough args" << endl;
continue;
}
auto artifact = parts[0];
auto buildid = parts[1];
auto tenant = parts[2];
cout << artifact << endl
<< buildid << endl
<< tenant << endl;
// 75031cad206c4014ad7a3387091d15ab
auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
uri.append_path(buildid);
auto response = client.request(
web::http::methods::GET, uri.to_string()).get();
// body is a web::json::value
auto body = response.extract_json().get();
cout << response.status_code() << endl;
cout << body.serialize() << endl;
// TODO: use artifact
// body["log_url"].as_string() returns a const std::string&
cout << body["log_url"].as_string() << endl;
}
}