Search in sources :

Example 1 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project teku by ConsenSys.

the class GetBlockHeaders method handle.

@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get block headers", tags = { TAG_BEACON }, description = "Retrieves block headers matching given query. By default it will fetch current head slot blocks.", queryParams = { @OpenApiParam(name = SLOT), @OpenApiParam(name = PARENT_ROOT, description = "Not currently supported.") }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetBlockHeadersResponse.class)), @OpenApiResponse(status = RES_BAD_REQUEST), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(@NotNull final Context ctx) throws Exception {
    final Map<String, List<String>> queryParameters = ctx.queryParamMap();
    final Optional<Bytes32> parentRoot = SingleQueryParameterUtils.getParameterValueAsBytes32IfPresent(queryParameters, PARENT_ROOT);
    final Optional<UInt64> slot = SingleQueryParameterUtils.getParameterValueAsUInt64IfPresent(queryParameters, SLOT);
    try {
        ctx.future(chainDataProvider.getBlockHeaders(parentRoot, slot).thenApplyChecked(jsonProvider::objectToJSON).exceptionallyCompose(error -> handleError(ctx, error)));
    } catch (final IllegalArgumentException e) {
        ctx.status(SC_BAD_REQUEST);
        ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
    }
}
Also used : AbstractHandler(tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) SLOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.SLOT) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) PARENT_ROOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.PARENT_ROOT) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) Map(java.util.Map) JsonProvider(tech.pegasys.teku.provider.JsonProvider) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) ChainDataProvider(tech.pegasys.teku.api.ChainDataProvider) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) Throwables(com.google.common.base.Throwables) SingleQueryParameterUtils(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils) Handler(io.javalin.http.Handler) List(java.util.List) OpenApiParam(io.javalin.plugin.openapi.annotations.OpenApiParam) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) Optional(java.util.Optional) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) GetBlockHeadersResponse(tech.pegasys.teku.api.response.v1.beacon.GetBlockHeadersResponse) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) NotNull(org.jetbrains.annotations.NotNull) DataProvider(tech.pegasys.teku.api.DataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) List(java.util.List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project teku by ConsenSys.

the class GetDepositContract method handle.

@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get deposit contract address", tags = { TAG_CONFIG }, description = "Retrieve deposit contract address and genesis fork version.", responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetDepositContractResponse.class)), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(final Context ctx) throws Exception {
    final int depositChainId = configProvider.getGenesisSpecConfig().getDepositChainId();
    if (depositContractResponse.isEmpty()) {
        this.depositContractResponse = Optional.of(jsonProvider.objectToJSON(new GetDepositContractResponse(depositChainId, depositContractAddress)));
    }
    ctx.status(SC_OK);
    ctx.json(this.depositContractResponse.orElse(""));
}
Also used : GetDepositContractResponse(tech.pegasys.teku.api.response.v1.config.GetDepositContractResponse) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 3 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project teku by ConsenSys.

the class PutLogLevel method handle.

@OpenApi(path = ROUTE, method = HttpMethod.PUT, summary = "Changes the log level without restarting.", tags = { TAG_TEKU }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = LogLevel.class) }, description = "```\n{\n  \"level\": (String; acceptable values: ALL, TRACE, DEBUG, INFO, ERROR, FATAL, OFF ),\n" + "  \"log_filter\": [(String; Optional)]\n}\n```"), description = "Changes the log level without restarting. You can change the log level for all logs, or the log level for specific packages or classes.", responses = { @OpenApiResponse(status = RES_NO_CONTENT, description = "The LogLevel was accepted and applied"), @OpenApiResponse(status = RES_BAD_REQUEST, description = INVALID_BODY_SUPPLIED), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(final Context ctx) throws Exception {
    try {
        final LogLevel params = parseRequestBody(ctx.body(), LogLevel.class);
        final String[] logFilters = params.getLogFilter().orElseGet(() -> new String[] { "" });
        for (final String logFilter : logFilters) {
            LoggingConfigurator.setAllLevels(logFilter, params.getLevel());
        }
        ctx.status(SC_NO_CONTENT);
    } catch (final IllegalArgumentException e) {
        ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
        ctx.status(SC_BAD_REQUEST);
    }
}
Also used : BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) LogLevel(tech.pegasys.teku.api.schema.LogLevel) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project teku by ConsenSys.

the class GetProtoArray method handle.

@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get current fork choice data", tags = { TAG_TEKU }, description = "Get the raw data stored in the fork choice protoarray to aid debugging. This API is considered unstable and the returned data format may change in the future.", responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetProtoArrayResponse.class)), @OpenApiResponse(status = RES_INTERNAL_ERROR), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = SERVICE_UNAVAILABLE) })
@Override
public void handle(final Context ctx) throws Exception {
    ctx.header(Header.CACHE_CONTROL, CACHE_NONE);
    final GetProtoArrayResponse response = new GetProtoArrayResponse(chainDataProvider.getProtoArrayData());
    ctx.result(jsonProvider.objectToJSON(response));
}
Also used : GetProtoArrayResponse(tech.pegasys.teku.api.response.v1.teku.GetProtoArrayResponse) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 5 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project teku by ConsenSys.

the class PostBlock method handle.

@OpenApi(path = ROUTE, method = HttpMethod.POST, summary = "Publish a signed block", tags = { TAG_BEACON, TAG_VALIDATOR_REQUIRED }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = SignedBlock.class) }), description = "Submit a signed beacon block to the beacon node to be imported." + " The beacon node performs the required validation.", responses = { @OpenApiResponse(status = RES_OK, description = "Block has been successfully broadcast, validated and imported."), @OpenApiResponse(status = RES_ACCEPTED, description = "Block has been successfully broadcast, but failed validation and has not been imported."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Unable to parse request body."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node experienced an internal error."), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = "Beacon node is currently syncing.") })
@Override
public void handle(final Context ctx) throws Exception {
    try {
        if (syncDataProvider.isSyncing()) {
            ctx.status(SC_SERVICE_UNAVAILABLE);
            ctx.json(BadRequest.serviceUnavailable(jsonProvider));
            return;
        }
        final SignedBeaconBlock signedBeaconBlock = validatorDataProvider.parseBlock(jsonProvider, ctx.body());
        ctx.future(validatorDataProvider.submitSignedBlock(signedBeaconBlock).thenApplyChecked(validatorBlockResult -> handleResponseContext(ctx, validatorBlockResult)));
    } catch (final JsonProcessingException ex) {
        ctx.status(SC_BAD_REQUEST);
        ctx.json(BadRequest.badRequest(jsonProvider, ex.getMessage()));
    } catch (final Exception ex) {
        LOG.error("Failed to post block due to internal error", ex);
        ctx.status(SC_INTERNAL_SERVER_ERROR);
        ctx.json(BadRequest.internalError(jsonProvider, ex.getMessage()));
    }
}
Also used : SC_INTERNAL_SERVER_ERROR(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR) RES_SERVICE_UNAVAILABLE(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_SERVICE_UNAVAILABLE) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) SC_SERVICE_UNAVAILABLE(javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE) ValidatorDataProvider(tech.pegasys.teku.api.ValidatorDataProvider) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) JsonProvider(tech.pegasys.teku.provider.JsonProvider) RES_ACCEPTED(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_ACCEPTED) SignedBlock(tech.pegasys.teku.api.schema.interfaces.SignedBlock) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) SyncDataProvider(tech.pegasys.teku.api.SyncDataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) TAG_VALIDATOR_REQUIRED(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Handler(io.javalin.http.Handler) Logger(org.apache.logging.log4j.Logger) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) SC_OK(javax.servlet.http.HttpServletResponse.SC_OK) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) OpenApiRequestBody(io.javalin.plugin.openapi.annotations.OpenApiRequestBody) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SC_ACCEPTED(javax.servlet.http.HttpServletResponse.SC_ACCEPTED) LogManager(org.apache.logging.log4j.LogManager) DataProvider(tech.pegasys.teku.api.DataProvider) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)21 Optional (java.util.Optional)9 BadRequest (tech.pegasys.teku.beaconrestapi.schema.BadRequest)9 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)8 Context (io.javalin.http.Context)5 HttpMethod (io.javalin.plugin.openapi.annotations.HttpMethod)5 OpenApiContent (io.javalin.plugin.openapi.annotations.OpenApiContent)5 OpenApiResponse (io.javalin.plugin.openapi.annotations.OpenApiResponse)5 List (java.util.List)5 DataProvider (tech.pegasys.teku.api.DataProvider)5 RES_BAD_REQUEST (tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST)5 RES_INTERNAL_ERROR (tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR)5 RES_OK (tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK)5 JsonProvider (tech.pegasys.teku.provider.JsonProvider)5 Handler (io.javalin.http.Handler)4 SC_BAD_REQUEST (javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)4 AbstractHandler (tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler)4 Throwables (com.google.common.base.Throwables)3 OpenApiParam (io.javalin.plugin.openapi.annotations.OpenApiParam)3