mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 14:10:50 -05:00
chore: first commit
This commit is contained in:
69 files changed
+3697
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package io.floci.cli.http;
|
||||
|
||||
public class FlociException extends Exception {
|
||||
public FlociException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package io.floci.cli.http;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FlociHttpClient {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private final String endpoint;
|
||||
private final HttpClient http;
|
||||
|
||||
public FlociHttpClient(String endpoint) {
|
||||
this.endpoint = endpoint.endsWith("/") ? endpoint.substring(0, endpoint.length() - 1) : endpoint;
|
||||
this.http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(5))
|
||||
.build();
|
||||
}
|
||||
|
||||
public HealthInfo health() throws FlociException {
|
||||
JsonNode node = getJson("/_floci/health");
|
||||
return new HealthInfo(
|
||||
node.path("version").asText("unknown"),
|
||||
node.path("edition").asText("community"),
|
||||
node.path("services").isArray()
|
||||
? MAPPER.convertValue(node.path("services"), String[].class)
|
||||
: new String[0]);
|
||||
}
|
||||
|
||||
public Optional<HealthInfo> healthOptional() {
|
||||
try {
|
||||
return Optional.of(health());
|
||||
} catch (Exception e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public ServerInfo info() throws FlociException {
|
||||
JsonNode node = getJson("/_floci/info");
|
||||
return new ServerInfo(
|
||||
node.path("version").asText("unknown"),
|
||||
node.path("edition").asText("community"));
|
||||
}
|
||||
|
||||
public InitState initState() throws FlociException {
|
||||
JsonNode node = getJson("/_floci/init");
|
||||
JsonNode completed = node.path("completed");
|
||||
return new InitState(
|
||||
completed.path("boot").asBoolean(),
|
||||
completed.path("start").asBoolean(),
|
||||
completed.path("ready").asBoolean(),
|
||||
completed.path("shutdown").asBoolean());
|
||||
}
|
||||
|
||||
public boolean isReachable() {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + "/_floci/health"))
|
||||
.timeout(Duration.ofSeconds(3))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<Void> resp = http.send(req, HttpResponse.BodyHandlers.discarding());
|
||||
return resp.statusCode() == 200;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> postSnapshot(String name) throws FlociException {
|
||||
return postJson("/_floci/snapshots/" + name, "{}");
|
||||
}
|
||||
|
||||
public Map<String, Object> loadSnapshot(String name) throws FlociException {
|
||||
return postJson("/_floci/snapshots/" + name + "/load", "{}");
|
||||
}
|
||||
|
||||
public JsonNode listSnapshots() throws FlociException {
|
||||
return getJson("/_floci/snapshots");
|
||||
}
|
||||
|
||||
public void deleteSnapshot(String name) throws FlociException {
|
||||
deleteRequest("/_floci/snapshots/" + name);
|
||||
}
|
||||
|
||||
private JsonNode getJson(String path) throws FlociException {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.header("Accept", "application/json")
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString());
|
||||
if (resp.statusCode() >= 400) {
|
||||
throw new FlociException("Server returned HTTP " + resp.statusCode() + " for " + path);
|
||||
}
|
||||
return MAPPER.readTree(resp.body());
|
||||
} catch (FlociException e) {
|
||||
throw e;
|
||||
} catch (java.net.ConnectException e) {
|
||||
throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try 'floci status' or 'floci start'.");
|
||||
} catch (Exception e) {
|
||||
throw new FlociException("Request failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> postJson(String path, String body) throws FlociException {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.timeout(Duration.ofSeconds(30))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString());
|
||||
if (resp.statusCode() >= 400) {
|
||||
throw new FlociException("Server returned HTTP " + resp.statusCode() + " for " + path);
|
||||
}
|
||||
if (resp.body() == null || resp.body().isBlank()) return Map.of();
|
||||
return MAPPER.readValue(resp.body(), Map.class);
|
||||
} catch (FlociException e) {
|
||||
throw e;
|
||||
} catch (java.net.ConnectException e) {
|
||||
throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try 'floci start'.");
|
||||
} catch (Exception e) {
|
||||
throw new FlociException("Request failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteRequest(String path) throws FlociException {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.DELETE()
|
||||
.build();
|
||||
HttpResponse<Void> resp = http.send(req, HttpResponse.BodyHandlers.discarding());
|
||||
if (resp.statusCode() >= 400) {
|
||||
throw new FlociException("Server returned HTTP " + resp.statusCode() + " for " + path);
|
||||
}
|
||||
} catch (FlociException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new FlociException("Request failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public record HealthInfo(String version, String edition, String[] services) {}
|
||||
public record ServerInfo(String version, String edition) {}
|
||||
public record InitState(boolean boot, boolean start, boolean ready, boolean shutdown) {}
|
||||
}
|
||||
Reference in new issue
Block a user