Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Nov 7, 2023
1 parent d2bb4a0 commit b9462a6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
8 changes: 6 additions & 2 deletions src/main/java/de/malkusch/km200/KM200.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,12 @@ private static void assertHttpUri(String var) {
assertNotBlank(var, "Wrong uri " + var);
var uri = URI.create(var);

var scheme = uri.getScheme();
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
switch (uri.getScheme()) {
case String s when s.equalsIgnoreCase("http"):
break;
case String s when s.equalsIgnoreCase("https"):
break;
case null, default:
throw new IllegalArgumentException("Wrong uri " + var);
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/de/malkusch/km200/KM200Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import de.malkusch.km200.KM200Exception.Forbidden;

public abstract class KM200Endpoint {
public abstract sealed class KM200Endpoint {
private final String path;
private final String type;

Expand All @@ -30,7 +30,7 @@ public String toString() {
return String.format("%s [%s]", path, type);
}

public static class Value extends KM200Endpoint {
public static final class Value extends KM200Endpoint {
private final String body;
private final boolean writeable;
private final boolean recordable;
Expand All @@ -57,13 +57,13 @@ public String toString() {
}
}

public static class ForbiddenNode extends KM200Endpoint {
public static final class ForbiddenNode extends KM200Endpoint {
ForbiddenNode(String path) {
super(path, "Forbidden");
}
}

public static class UnknownNode extends KM200Endpoint {
public static final class UnknownNode extends KM200Endpoint {
private final String value;

UnknownNode(String path, String type, String value) {
Expand Down Expand Up @@ -139,14 +139,11 @@ private static Value value(String path, String type, JsonNode json) {
var writeable = json.path("writeable").asBoolean(false);
var recordable = json.path("recordable").asBoolean(false);

String value;
if (json.has("value")) {
value = json.get("value").asText();
} else if (json.has("values")) {
value = json.get("values").toString();
} else {
value = json.toString();
}
String value = switch (json) {
case JsonNode j when j.has("value") -> json.get("value").asText();
case JsonNode j when j.has("values") -> json.get("values").toString();
default -> json.toString();
};

String allowedValues = null;
if (json.has("allowedValues")) {
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/de/malkusch/km200/http/RetryHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ public Response exchange(Request request) throws IOException, InterruptedExcepti
return retry.get(() -> http.exchange(request));

} catch (FailsafeException e) {
if (e.getCause() instanceof IOException cause) {
throw cause;

} else if (e.getCause() instanceof InterruptedException cause) {
throw cause;

} else if (e.getCause() instanceof KM200Exception cause) {
throw cause;

} else {
throw new KM200Exception("Unexpected retry error for " + request.path(), e);
switch (e.getCause()) {
case IOException cause -> throw cause;
case InterruptedException cause -> throw cause;
case KM200Exception cause -> throw cause;
default -> throw new KM200Exception("Unexpected retry error for " + request.path(), e);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/de/malkusch/km200/http/UrlHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ protected Response exchange(Request request) throws IOException, InterruptedExce

private HttpURLConnection connect(Request request) throws IOException {
try {
var connection = (HttpURLConnection) new URL(uri + request.path()).openConnection();
var url = new URL(uri + request.path());

if (!(url.openConnection() instanceof HttpURLConnection connection)) {
throw new IllegalStateException(url + " is not a http url");
}

connection.setConnectTimeout(timeoutMillis);
connection.setReadTimeout(timeoutMillis);
connection.setRequestProperty("User-Agent", userAgent);
Expand All @@ -74,7 +79,6 @@ private HttpURLConnection connect(Request request) throws IOException {
}

connection.connect();

return connection;

} catch (MalformedURLException e) {
Expand Down

0 comments on commit b9462a6

Please sign in to comment.