Search in sources :

Example 11 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project cwms-radar-api by USACE.

the class BlobController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = false, description = "Specifies the owning office. If this field is not specified, matching information from all offices shall be returned."), @OpenApiParam(name = "page", required = false, description = "This end point can return a lot of data, this identifies where in the request you are. This is an opaque value, and can be obtained from the 'next-page' value in the response."), @OpenApiParam(name = "pageSize", required = false, type = Integer.class, description = "How many entries per page returned. Default " + defaultPageSize + "."), @OpenApiParam(name = "like", required = false, type = String.class, description = "Posix regular expression describing the blob id's you want") }, responses = { @OpenApiResponse(status = "200", description = "A list of blobs.", content = { @OpenApiContent(type = Formats.JSONV2, from = Blobs.class), @OpenApiContent(type = Formats.XMLV2, from = Blobs.class) }) }, tags = { "Blob" })
@Override
public void getAll(Context ctx) {
    getAllRequests.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        String office = ctx.queryParam("office");
        Optional<String> officeOpt = Optional.ofNullable(office);
        String cursor = ctx.queryParamAsClass("cursor", String.class).allowNullable().get();
        cursor = cursor != null ? cursor : ctx.queryParamAsClass("page", String.class).getOrDefault("");
        if (CwmsDTOPaginated.CURSOR_CHECK.invoke(cursor) != true) {
            ctx.json(new RadarError("cursor or page passed in but failed validation")).status(HttpCode.BAD_REQUEST);
            return;
        }
        int pageSize = ctx.queryParamAsClass("pageSize", Integer.class).getOrDefault(ctx.queryParamAsClass("pagesize", Integer.class).getOrDefault(defaultPageSize));
        String like = ctx.queryParamAsClass("like", String.class).getOrDefault(".*");
        String formatParm = ctx.queryParamAsClass("format", String.class).getOrDefault("");
        String formatHeader = ctx.header(Header.ACCEPT);
        ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, formatParm);
        BlobDao dao = new BlobDao(dsl);
        List<Blob> blobList = dao.getAll(officeOpt, like);
        Blobs blobs = new Blobs.Builder(cursor, pageSize, 0).addAll(blobList).build();
        String result = Formats.format(contentType, blobs);
        ctx.result(result);
        ctx.contentType(contentType.toString());
        requestResultSize.update(result.length());
    }
}
Also used : Blob(cwms.radar.data.dto.Blob) Blobs(cwms.radar.data.dto.Blobs) ContentType(cwms.radar.formatters.ContentType) BlobDao(cwms.radar.data.dao.BlobDao) DSLContext(org.jooq.DSLContext) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 12 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project cwms-radar-api by USACE.

the class LocationCategoryController method getOne.

@OpenApi(pathParams = { @OpenApiParam(name = "category-id", required = true, description = "Specifies the Category whose data is to be included in the response.") }, queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the Location Category whose data is to be included in the response.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(from = LocationCategory.class, type = Formats.JSON) }) }, description = "Retrieves requested Location Category", tags = { TAG })
@Override
public void getOne(Context ctx, String categoryId) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationCategoryDao dao = new LocationCategoryDao(dsl);
        String office = ctx.queryParam("office");
        Optional<LocationCategory> grp = dao.getLocationCategory(office, categoryId);
        if (grp.isPresent()) {
            String formatHeader = ctx.header(Header.ACCEPT);
            ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null);
            String result = Formats.format(contentType, grp.get());
            ctx.result(result).contentType(contentType.toString());
            requestResultSize.update(result.length());
            ctx.status(HttpServletResponse.SC_OK);
        } else {
            final RadarError re = new RadarError("Cannot requested location category id");
            logger.info(() -> {
                StringBuilder builder = new StringBuilder(re.toString()).append("with url:").append(ctx.fullUrl());
                return builder.toString();
            });
            ctx.json(re).status(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) LocationCategory(cwms.radar.data.dto.LocationCategory) DSLContext(org.jooq.DSLContext) LocationCategoryDao(cwms.radar.data.dao.LocationCategoryDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 13 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project cwms-radar-api by USACE.

the class LocationGroupController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "office", description = "Specifies the owning office of the location group(s) whose data is to be included in the response. If this field is not specified, matching location groups information from all offices shall be returned."), @OpenApiParam(name = "includeAssigned", type = Boolean.class, description = "Include the assigned locations in the returned location groups. (default: false)") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(isArray = true, from = LocationGroup.class, type = Formats.JSON), @OpenApiContent(isArray = true, from = CsvV1LocationGroup.class, type = Formats.CSV) }) }, description = "Returns CWMS Location Groups Data", tags = { TAG })
@Override
public void getAll(Context ctx) {
    getAllRequests.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationGroupDao cdm = new LocationGroupDao(dsl);
        String office = ctx.queryParam("office");
        boolean includeValues = ctx.queryParamAsClass("includeAssigned", Boolean.class).getOrDefault(false);
        List<LocationGroup> grps = cdm.getLocationGroups(office, includeValues);
        if (!grps.isEmpty()) {
            String formatHeader = ctx.header(Header.ACCEPT);
            ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, "");
            String result = Formats.format(contentType, grps, LocationGroup.class);
            ctx.result(result);
            ctx.contentType(contentType.toString());
            requestResultSize.update(result.length());
            ctx.status(HttpServletResponse.SC_OK);
        } else {
            RadarError re = new RadarError("No location groups for office provided");
            logger.info(() -> {
                return new StringBuilder().append(re.toString()).append(System.lineSeparator()).append("for request ").append(ctx.fullUrl()).toString();
            });
            ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re);
        }
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) LocationGroupDao(cwms.radar.data.dao.LocationGroupDao) DSLContext(org.jooq.DSLContext) CsvV1LocationGroup(cwms.radar.formatters.csv.CsvV1LocationGroup) LocationGroup(cwms.radar.data.dto.LocationGroup) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 14 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project cwms-radar-api by USACE.

the class LocationGroupController method getOne.

@OpenApi(pathParams = { @OpenApiParam(name = "group-id", required = true, description = "Specifies the location_group whose data is to be included in the response") }, queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the location group whose data is to be included in the response."), @OpenApiParam(name = "category-id", required = true, description = "Specifies the category containing the location group whose data is to be included in the response.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(from = LocationGroup.class, type = Formats.JSON), @OpenApiContent(from = CsvV1LocationGroup.class, type = Formats.CSV), @OpenApiContent(type = Formats.GEOJSON) }) }, description = "Retrieves requested Location Group", tags = { TAG })
@Override
public void getOne(Context ctx, String groupId) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationGroupDao cdm = new LocationGroupDao(dsl);
        String office = ctx.queryParam("office");
        String categoryId = ctx.queryParam("category-id");
        String formatHeader = ctx.header(Header.ACCEPT);
        ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, "");
        String result;
        if (Formats.GEOJSON.equals(contentType.getType())) {
            FeatureCollection fc = cdm.buildFeatureCollectionForLocationGroup(office, categoryId, groupId, "EN");
            ObjectMapper mapper = ctx.appAttribute("ObjectMapper");
            result = mapper.writeValueAsString(fc);
        } else {
            Optional<LocationGroup> grp = cdm.getLocationGroup(office, categoryId, groupId);
            if (grp.isPresent()) {
                result = Formats.format(contentType, grp.get());
            } else {
                RadarError re = new RadarError("Unable to find location group based on parameters given");
                logger.info(() -> {
                    return new StringBuilder().append(re.toString()).append(System.lineSeparator()).append("for request ").append(ctx.fullUrl()).toString();
                });
                ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re);
                return;
            }
        }
        ctx.result(result);
        ctx.contentType(contentType.toString());
        requestResultSize.update(result.length());
        ctx.status(HttpServletResponse.SC_OK);
    } catch (JsonProcessingException e) {
        RadarError re = new RadarError("Failed to process request");
        logger.log(Level.SEVERE, re.toString(), e);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : ContentType(cwms.radar.formatters.ContentType) LocationGroupDao(cwms.radar.data.dao.LocationGroupDao) DSLContext(org.jooq.DSLContext) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) FeatureCollection(org.geojson.FeatureCollection) CsvV1LocationGroup(cwms.radar.formatters.csv.CsvV1LocationGroup) LocationGroup(cwms.radar.data.dto.LocationGroup) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 15 with OpenApi

use of io.javalin.plugin.openapi.annotations.OpenApi in project cwms-radar-api by USACE.

the class OfficeController method getOne.

@OpenApi(pathParams = @OpenApiParam(name = "office", description = "The 3 letter office ID you want more information for", type = String.class), queryParams = @OpenApiParam(name = "format", required = false, deprecated = true, description = "(Deprecated in favor of Accept header) Specifies the encoding format of the response. Valid value for the format field for this URI are:\r\n1. tab\r\n2. csv\r\n 3. xml\r\n4. json (default)"), responses = { @OpenApiResponse(status = "200", description = "A list of offices.", content = { @OpenApiContent(from = OfficeFormatV1.class, type = ""), @OpenApiContent(from = Office.class, isArray = true, type = Formats.JSONV2), @OpenApiContent(from = OfficeFormatV1.class, type = Formats.JSON), @OpenApiContent(from = TabV1Office.class, type = Formats.TAB), @OpenApiContent(from = CsvV1Office.class, type = Formats.CSV), @OpenApiContent(from = CsvV1Office.class, type = Formats.XML) }) }, tags = { "Offices" })
@Override
public void getOne(Context ctx, String officeId) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        OfficeDao dao = new OfficeDao(dsl);
        Optional<Office> office = dao.getOfficeById(officeId);
        if (office.isPresent()) {
            String formatParm = ctx.queryParamAsClass("format", String.class).getOrDefault("");
            String formatHeader = ctx.header(Header.ACCEPT);
            ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, formatParm);
            String result = Formats.format(contentType, office.get());
            ctx.result(result).contentType(contentType.toString());
            requestResultSize.update(result.length());
        } else {
            RadarError re = new RadarError("Not Found", new HashMap<String, String>() {

                {
                    put("office", "An office with that name does not exist");
                }
            });
            ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re);
        }
    }
}
Also used : CsvV1Office(cwms.radar.formatters.csv.CsvV1Office) Office(cwms.radar.data.dto.Office) TabV1Office(cwms.radar.formatters.tab.TabV1Office) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) OfficeDao(cwms.radar.data.dao.OfficeDao) DSLContext(org.jooq.DSLContext) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)56 Timer (com.codahale.metrics.Timer)35 DSLContext (org.jooq.DSLContext)32 RadarError (cwms.radar.api.errors.RadarError)26 ContentType (cwms.radar.formatters.ContentType)22 Optional (java.util.Optional)9 BadRequest (tech.pegasys.teku.beaconrestapi.schema.BadRequest)9 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)8 IOException (java.io.IOException)7 TimeSeriesDao (cwms.radar.data.dao.TimeSeriesDao)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 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 SQLException (java.sql.SQLException)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