-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
727 additions
and
445 deletions.
There are no files selected for viewing
302 changes: 227 additions & 75 deletions
302
...ation-test/java/tech/pegasys/teku/ethereum/executionclient/rest/OkHttpRestClientTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...rc/main/java/tech/pegasys/teku/ethereum/executionclient/rest/AbstractResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2025 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.ethereum.executionclient.rest; | ||
|
||
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_UNSUPPORTED_MEDIA_TYPE; | ||
|
||
import java.io.IOException; | ||
import okhttp3.Request; | ||
import okhttp3.ResponseBody; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import tech.pegasys.teku.ethereum.executionclient.schema.Response; | ||
import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
|
||
public abstract class AbstractResponseHandler { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
abstract void handleFailure(final IOException exception); | ||
|
||
abstract void handleResponse(final Request request, final okhttp3.Response response); | ||
|
||
protected <T> boolean handleResponseError( | ||
final Request request, | ||
final okhttp3.Response response, | ||
final SafeFuture<Response<T>> futureResponse) { | ||
LOG.trace("{} {} {}", request.method(), request.url(), response.code()); | ||
if (response.isSuccessful()) { | ||
return false; | ||
} | ||
try { | ||
if (response.code() == SC_UNSUPPORTED_MEDIA_TYPE) { | ||
futureResponse.complete(Response.fromUnsupportedMediaTypeError()); | ||
} else { | ||
final String errorMessage = getErrorMessageForFailedResponse(response); | ||
futureResponse.complete(Response.fromErrorMessage(errorMessage)); | ||
} | ||
} catch (final Throwable ex) { | ||
futureResponse.completeExceptionally(ex); | ||
} | ||
return true; | ||
} | ||
|
||
private String getErrorMessageForFailedResponse(final okhttp3.Response response) | ||
throws IOException { | ||
try (final ResponseBody responseBody = response.body()) { | ||
if (bodyIsEmpty(responseBody)) { | ||
return response.code() + ": " + response.message(); | ||
} | ||
return responseBody.string(); | ||
} | ||
} | ||
|
||
protected boolean bodyIsEmpty(final ResponseBody responseBody) { | ||
return responseBody == null || responseBody.contentLength() == 0; | ||
} | ||
} |
Oops, something went wrong.