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 b9462a6 commit 7391a32
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
24 changes: 10 additions & 14 deletions src/main/java/de/malkusch/km200/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ 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 {
var status = response.status();
if (status >= 200 && status <= 299) {
return response;

} else {
throw switch (status) {
case 400 -> new KM200Exception.BadRequest(request + " was a bad request");
case 403 -> new KM200Exception.Forbidden(request + " is forbidden");
case 404 -> new KM200Exception.NotFound(request + " was not found");
case 423 -> new KM200Exception.Locked(request + " was locked");
case 500 -> new KM200Exception.ServerError(request + " resulted in a server error");
default -> new KM200Exception(request + " failed with response code " + status);
};
}
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());
};
}
}
3 changes: 2 additions & 1 deletion src/main/java/de/malkusch/km200/http/RetryHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public Response exchange(Request request) throws IOException, InterruptedExcepti
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);
case Throwable cause -> throw new KM200Exception("Unexpected retry error for " + request.path(), cause);
case null -> throw new KM200Exception("Unexpected retry error for " + request.path(), e);
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/de/malkusch/km200/http/UrlHttp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.malkusch.km200.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
Expand Down Expand Up @@ -45,9 +46,11 @@ protected Response exchange(Request request) throws IOException, InterruptedExce
throw new IOException(request + " received invalid HTTP response");
}

try (var input = connection.getErrorStream() != null ? connection.getErrorStream()
: connection.getInputStream()) {

var input = switch (connection.getErrorStream()) {
case InputStream error -> error;
case null -> connection.getInputStream();
};
try (input) {
var body = input.readAllBytes();
var response = new Response(status, body);
return assertHttpOk(request, response);
Expand Down

0 comments on commit 7391a32

Please sign in to comment.