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 11bab6b commit cf39c47
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 146 deletions.
2 changes: 0 additions & 2 deletions src/main/java/de/malkusch/km200/KM200.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ public KM200(String uri, int retries, Duration timeout, String gatewayPassword,
this.comm = new KM200Comm();

{
// Http http = new JdkHttp(uri.replaceAll("/*$", ""), USER_AGENT,
// timeout);
Http http = new UrlHttp(uri.replaceAll("/*$", ""), USER_AGENT, timeout);

/*
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/de/malkusch/km200/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public String toString() {
}

public static record Response(int status, byte[] body) {

public static Response successfullResponse(Request request, int status, byte[] body) throws KM200Exception {
return switch ((Integer) status) {
case Integer s when (s >= 200 && s <= 299) -> new Response(status, body);

case 400 -> throw new KM200Exception.BadRequest(request + " was a bad request");
case 403 -> throw new KM200Exception.Forbidden(request + " is forbidden");
case 404 -> throw new KM200Exception.NotFound(request + " was not found");
case 423 -> throw new KM200Exception.Locked(request + " was locked");
case 500 -> throw new KM200Exception.ServerError(request + " resulted in a server error");
default -> throw new KM200Exception(request + " failed with response code " + status);
};
}

}

public final Response get(String path) throws KM200Exception, IOException, InterruptedException {
Expand All @@ -39,17 +53,4 @@ public final Response post(String path, byte[] body) throws KM200Exception, IOEx
}

protected abstract Response exchange(Request request) throws IOException, InterruptedException, KM200Exception;

static Response assertHttpOk(Request request, Response response) throws KM200Exception {
return switch ((Integer) response.status()) {
case Integer status when (status >= 200 && status <= 299) -> response;

case 400 -> throw new KM200Exception.BadRequest(request + " was a bad request");
case 403 -> throw new KM200Exception.Forbidden(request + " is forbidden");
case 404 -> throw new KM200Exception.NotFound(request + " was not found");
case 423 -> throw new KM200Exception.Locked(request + " was locked");
case 500 -> throw new KM200Exception.ServerError(request + " resulted in a server error");
default -> throw new KM200Exception(request + " failed with response code " + response.status());
};
}
}
129 changes: 0 additions & 129 deletions src/main/java/de/malkusch/km200/http/JdkHttp.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/de/malkusch/km200/http/UrlHttp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.malkusch.km200.http;

import static de.malkusch.km200.http.Http.Response.successfullResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -52,8 +54,7 @@ protected Response exchange(Request request) throws IOException, InterruptedExce
};
try (input) {
var body = input.readAllBytes();
var response = new Response(status, body);
return assertHttpOk(request, response);
return successfullResponse(request, status, body);
}

} catch (SocketTimeoutException e) {
Expand Down

0 comments on commit cf39c47

Please sign in to comment.