Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ public void boot() throws Exception {

/**
* Get state of the agent and the client
*
* @return Optional containing the State if the agent exists, or empty if not found
*/
public State state() throws Exception {
public Optional<State> state() throws Exception {
String caid = controller != null ? controller.getPre() : null;
if (caid == null) {
throw new IllegalArgumentException("Controller not initialized");
Expand All @@ -167,7 +169,7 @@ public State state() throws Exception {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

if (response.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new IllegalArgumentException("Agent does not exist for controller " + caid);
return Optional.empty();
}

if (response.statusCode() != HttpURLConnection.HTTP_OK
Expand All @@ -177,22 +179,22 @@ public State state() throws Exception {

Map<String, Object> data = Utils.fromJson(response.body(), new TypeReference<>() {});

return State.builder()
return Optional.of(State.builder()
.agent(data.getOrDefault(SignifyFields.AGENT.getValue(), null))
.controller(data.getOrDefault(SignifyFields.CONTROLLER.getValue(), null))
.ridx((Integer) data.getOrDefault(SignifyFields.RIDX.getValue(), 0))
.pidx((Integer) data.getOrDefault(SignifyFields.PIDX.getValue(), 0))
.build();
.build());
}

/**
* Connect to a KERIA agent
*/
public void connect() throws Exception {
State state = state();
if (state == null) {
throw new RuntimeException("State not initialized");
}
String caid = controller != null ? controller.getPre() : "unknown";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controller is never null, actually. It gets initalized without the up-to-date state from KERIA in the constructor, and this function re-initializes it with the state (which is kind of weird, but inherited from Signify-TS poor design).

Perhaps we can tidy up any controller != null checks in this file! getPre() should always be work too.

State state = state()
.orElseThrow(() -> new IllegalArgumentException(
"Agent not booted for controller " + caid + " \u2014 call boot() first"));
this.pidx = state.getPidx();

// Create controller representing the local client AID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public KeyEvents(SignifyClient client) {
}

/**
* Retrieve the key state for an identifier
* Retrieve the key events for an identifier
* @param pre Identifier prefix
* @return A map representing the key states
* @return A list representing the key events (empty list if none found)
* @throws Exception if the fetch operation fails
*/
public Object get(String pre) throws Exception {
Expand All @@ -76,6 +76,11 @@ public Config(SignifyClient client) {
this.client = client;
}

/**
* Retrieve the agent configuration
* @return The agent configuration
* @throws Exception if the fetch operation fails
*/
public Object get() throws Exception {
String path = "/config";
String method = "GET";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,10 @@ public static SignifyClient getOrCreateClient(String bran) throws Exception {
}

SignifyClient client = new SignifyClient(url, bran, Salter.Tier.low, bootUrl, null);
try {
client.connect();
} catch (Exception e) {
if (client.state().isEmpty()) {
client.boot();
client.connect();
}
client.connect();
System.out.println("Client: " +
Map.of("agent", client.getAgent() != null ? client.getAgent().getPre() : null,
"controller", client.getController().getPre()
Expand Down