// C++17 + libcurl. Build: c++ -std=c++17 redrover.cpp $(pkg-config --cflags --libs libcurl) -o redrover
#include <curl/curl.h>
#include <cstdlib>
#include <iostream>
#include <string>
static size_t discard(char*, size_t size, size_t count, void*) { return size * count; }
int main() {
 const char *base=std::getenv("REDROVER_URL"), *secret=std::getenv("REDROVER_API_KEY");
 if(!base||!secret){std::cerr<<"Set REDROVER_URL and REDROVER_API_KEY.\n";return 1;}
 std::string url(base), key(secret);
 if(url.rfind("https://",0)!=0 && url.rfind("http://localhost:",0)!=0 && url.rfind("http://127.0.0.1:",0)!=0){std::cerr<<"Use HTTPS outside localhost.\n";return 1;}
 if(key.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")!=std::string::npos){std::cerr<<"Invalid API key.\n";return 1;}
 while(!url.empty()&&url.back()=='/')url.pop_back();url+="/api/v1/workspaces";
 if(curl_global_init(CURL_GLOBAL_DEFAULT)!=CURLE_OK)return 1;
 CURL* client=curl_easy_init();if(!client){curl_global_cleanup();return 1;}
 curl_slist* headers=nullptr;headers=curl_slist_append(headers,("X-API-Key: "+key).c_str());headers=curl_slist_append(headers,"Accept: application/json");
 curl_easy_setopt(client,CURLOPT_URL,url.c_str());curl_easy_setopt(client,CURLOPT_HTTPHEADER,headers);curl_easy_setopt(client,CURLOPT_TIMEOUT,30L);curl_easy_setopt(client,CURLOPT_FOLLOWLOCATION,0L);curl_easy_setopt(client,CURLOPT_WRITEFUNCTION,discard);
 const auto code=curl_easy_perform(client);long status=0;curl_easy_getinfo(client,CURLINFO_RESPONSE_CODE,&status);
 curl_slist_free_all(headers);curl_easy_cleanup(client);curl_global_cleanup();
 if(code!=CURLE_OK||status!=200){std::cerr<<"Request failed; HTTP "<<status<<".\n";return 1;}
 std::cout<<"Workspace request succeeded.\n";return 0;
}
