diff --git a/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerHazardCurves.java b/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerHazardCurves.java new file mode 100644 index 0000000000000000000000000000000000000000..2e2d592a2f686556bbc1ef03b5a15b6e585b0aef --- /dev/null +++ b/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerHazardCurves.java @@ -0,0 +1,111 @@ +package gov.usgs.earthquake.nshmp.netcdf.www; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import gov.usgs.earthquake.nshmp.netcdf.Nshm; +import gov.usgs.earthquake.nshmp.www.SwaggerUtils; + +import io.micronaut.http.HttpRequest; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.parser.util.SchemaTypeUtil; + +/** + * Swagger page updates for hazard services. + * + * @author U.S. Geological Survey + */ +public class SwaggerHazardCurves extends Swagger<NetcdfServiceHazardCurves> { + + SwaggerHazardCurves(HttpRequest<?> request, NetcdfServiceHazardCurves service) { + super(request, service); + } + + @Override + String descriptionHeader() { + return "NSHM Data Sets"; + } + + @Override + String serviceInfo() { + return String.join("", + "Get hazard curves for user-specified NSHM, latitudes, ", + "longitudes, and/or site classes of the National Seismic Hazard Models data releases."); + } + + @Override + String servicePatternSection() { + var url = NetcdfWsUtils.getRequestUrl(request); + url = url.endsWith("/swagger") ? url.replace("/swagger", "") : url; + + return new StringBuilder() + .append( + "<details>\n" + + "<summary>Service Call Patterns</summary>\n") + .append( + "### Query Pattern\n" + + + "The query based service call is in the form of:\n" + + + "```text\n" + + url + "/hazard?nshm={nshm}&longitude={number}&latitude={number}\n" + + url + "/hazard" + + "?nshm={nshm}&longitude={number}&latitude={number}&siteClass={string}\n" + + url + "/hazard" + + "?nshm={nshm}&longitude={number}&latitude={number}&siteClass={string}&imt={string}\n" + + "````\n" + + + "Example:\n" + + "```text\n" + + url + "/hazard?nshm=CONUS_2018&longitude=-118&latitude=34\n" + + url + "/hazard?nshm=CONUS_2018&longitude=-118&latitude=34&siteClass=BC\n" + + url + "/hazard?nshm=CONUS_2018&longitude=-118&latitude=34&siteClass=BC&imt=PGA\n" + + "```\n") + .append( + "### Slash Pattern\n" + + + "The slash based service call is in the form of:\n" + + + "```text\n" + + url + "/hazard/{nshm}/{longitude}/{latitude}\n" + + url + "/hazard/{nshm}/{longitude}/{latitude}/{siteClass}\n" + + url + "/hazard/{nshm}/{longitude}/{latitude}/{siteClass}/{imt}\n" + + "```\n" + + + "Example:\n" + + + "```text\n" + + url + "/hazard/CONUS_2018/-118/34\n" + + url + "/hazard/CONUS_2018/-118/34/BC\n" + + url + "/hazard/CONUS_2018/-118/34/BC/PGA\n" + + "```\n") + .append("</details>") + .toString(); + } + + @Override + OpenAPI updateOpenApi() throws IOException { + OpenAPI openApi = super.updateOpenApi(); + + SwaggerUtils.imtSchema( + openApi.getComponents().getSchemas(), + List.copyOf(service.netcdfDataFiles().imts())); + + nshmSchema(openApi.getComponents().getSchemas(), service.netcdfDataFiles().nshms()); + + return openApi; + } + + private void nshmSchema(Map<String, Schema> schemas, Set<Nshm> nshms) { + Schema<Nshm> schema = new Schema<>(); + schema.setType(SchemaTypeUtil.STRING_TYPE); + nshms.stream() + .sorted() + .forEach(nshm -> schema.addEnumItemObject(nshm)); + + schemas.put(Nshm.class.getSimpleName(), schema); + } +}