import java.io.IOException;
import java.time.Duration;
import okhttp3.*;
/** Server-side Java 21 example. Never embed the API key in a browser or distributed app. */
public final class RedRoverClient {
 public static void main(String[] args) throws IOException {
  String base = required("REDROVER_URL"), key = required("REDROVER_API_KEY");
  HttpUrl url = HttpUrl.get(base.replaceAll("/+$", "") + "/api/v1/workspaces");
  if (!url.isHttps() && !java.util.Set.of("localhost", "127.0.0.1", "::1").contains(url.host())) throw new IllegalArgumentException("Use HTTPS outside localhost.");
  OkHttpClient client = new OkHttpClient.Builder().callTimeout(Duration.ofSeconds(30)).followRedirects(false).followSslRedirects(false).retryOnConnectionFailure(false).build();
  Request request = new Request.Builder().url(url).header("X-API-Key", key).header("Accept", "application/json").build();
  try (Response response = client.newCall(request).execute()) {
   if (!response.isSuccessful()) throw new IOException("RedRover returned HTTP " + response.code());
   // Parse the response in your application. Avoid logging private workspace contents.
   System.out.println("Workspace request succeeded (" + response.code() + ").");
  } finally { client.dispatcher().executorService().shutdown(); client.connectionPool().evictAll(); }
 }
 private static String required(String name) { String value=System.getenv(name);if(value==null||value.isBlank())throw new IllegalArgumentException("Set "+name);return value; }
}
