Skip to content

Commit 7500b95

Browse files
refactor: improve code stability
1 parent 0c4996c commit 7500b95

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/env/infrastructure/wot/WotClient.java

+28-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package infrastructure.wot;
1010

1111
import city.sane.wot.DefaultWot;
12-
import city.sane.wot.Wot;
1312
import city.sane.wot.WotException;
1413
import city.sane.wot.thing.ConsumedThing;
1514
import okhttp3.OkHttpClient;
@@ -45,13 +44,15 @@ public class WotClient {
4544
public WotClient(final String thingId) {
4645
try {
4746
final String thingUrl = System.getenv(THING_DIRECTORY_VARIABLE) + thingId;
48-
final Wot wotClient = DefaultWot.clientOnly();
49-
final String thingDescription = this.obtainThingDescription(thingUrl);
50-
this.thing = wotClient.consume(thingDescription);
47+
final String thingDescription = this.obtainThingDescription(thingUrl).orElse("");
48+
if (thingDescription.isEmpty()) {
49+
throw new IllegalArgumentException("Thing Description for " + thingId + " not available");
50+
}
51+
this.thing = DefaultWot.clientOnly().consume(thingDescription);
5152
} catch (final WotException e) {
5253
Logger.getLogger(WotClient.class.toString()).info("Wot TDD configuration error");
5354
} catch (IOException e) {
54-
Logger.getLogger(WotClient.class.toString()).info(thingId + " does not have a Thing Description associated");
55+
Logger.getLogger(WotClient.class.toString()).info("Error in obtaining Thing Description for " + thingId);
5556
}
5657
}
5758

@@ -61,25 +62,34 @@ public WotClient(final String thingId) {
6162
* @param inputs the inputs of the action.
6263
*/
6364
public void invoke(final String action, final Map<String, Object> inputs) {
64-
try {
65-
this.thing.getActions().get(action).invoke(inputs).get();
66-
} catch (final InterruptedException | ExecutionException e) {
67-
Logger.getLogger(WotClient.class.toString()).info("Error in invoking '" + action + "' on " + thing.getId());
68-
}
65+
Optional.ofNullable(this.thing.getActions().get(action)).ifPresentOrElse(thingAction -> {
66+
try {
67+
thingAction.invoke(inputs).get();
68+
} catch (final InterruptedException | ExecutionException e) {
69+
Logger.getLogger(WotClient.class.toString())
70+
.info("Error in invoking '" + action + "' on " + this.thing.getId());
71+
}
72+
},
73+
() -> Logger.getLogger(WotClient.class.toString())
74+
.info("Action " + action + " not available in " + this.thing.getId())
75+
);
6976
}
7077

71-
private String obtainThingDescription(final String thingUrl) throws IOException {
78+
private Optional<String> obtainThingDescription(final String thingUrl) throws IOException {
7279
// We need that because sane-city wot doesn't support application/td+json schema.
7380
final OkHttpClient httpClient = new OkHttpClient();
7481
final Request request = new Request.Builder().url(thingUrl).get().build();
7582
try (Response response = httpClient.newCall(request).execute()) {
76-
return Optional.ofNullable(response.body()).map(responseBody -> {
77-
try {
78-
return responseBody.string();
79-
} catch (IOException e) {
80-
return "";
81-
}
82-
}).orElse("");
83+
return Optional.of(response)
84+
.filter(Response::isSuccessful)
85+
.map(Response::body)
86+
.map(responseBody -> {
87+
try {
88+
return responseBody.string();
89+
} catch (IOException e) {
90+
return "";
91+
}
92+
});
8393
}
8494
}
8595
}

0 commit comments

Comments
 (0)