Skip to content
Snippets Groups Projects
Commit 57de20ff authored by Clayton, Brandon Scott's avatar Clayton, Brandon Scott
Browse files

move swagger utils to swagger class

parent 4c5845c7
No related branches found
No related tags found
2 merge requests!128Production Release | nshmp-ws-static,!127Resolves - Handle Multiple NetCDF Files for AASHTO Service
......@@ -3,29 +3,21 @@ package gov.usgs.earthquake.nshmp.netcdf.www;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import gov.usgs.earthquake.nshmp.gmm.Imt;
import gov.usgs.earthquake.nshmp.netcdf.NetcdfVersion;
import gov.usgs.earthquake.nshmp.netcdf.data.NetcdfData;
import gov.usgs.earthquake.nshmp.netcdf.data.ScienceBaseMetadata;
import gov.usgs.earthquake.nshmp.www.ResponseBody;
import gov.usgs.earthquake.nshmp.www.ResponseMetadata;
import gov.usgs.earthquake.nshmp.www.SwaggerUtils;
import gov.usgs.earthquake.nshmp.www.WsUtils.EnumSerializer;
import gov.usgs.earthquake.nshmp.www.WsUtils.NaNSerializer;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
public class NetcdfWsUtils {
static final Gson GSON;
......@@ -69,33 +61,6 @@ public class NetcdfWsUtils {
return HttpResponse.serverError(response);
}
public static OpenAPI getOpenApi(
HttpRequest<?> request,
NetcdfData netcdfData,
String serviceSection) throws IOException {
var openApi = new OpenAPIV3Parser().read("META-INF/swagger/nshmp-ws-static.yml");
var scienceBaseMetadata = netcdfData.scienceBaseMetadata();
SwaggerUtils.addLocationBounds(openApi, netcdfData.minimumBounds(), netcdfData.maximumBounds());
var components = openApi.getComponents();
var schemas = components.getSchemas();
SwaggerUtils.siteClassSchema(schemas, netcdfData.siteClasses());
openApi.servers(null);
openApi.getInfo().setTitle(scienceBaseMetadata.label);
// Update description
var description = new StringBuilder()
.append(scienceBaseMetadata.description + "\n")
.append(swaggerResponseFormatSection())
.append(serviceSection)
.append(swaggerParameterSection(netcdfData))
.append(swaggerScienceBaseSection(scienceBaseMetadata))
.toString();
openApi.getInfo().setDescription(description);
return openApi;
}
public static enum Key {
LATITUDE,
LONGITUDE,
......@@ -107,52 +72,4 @@ public class NetcdfWsUtils {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
private static String swaggerParameterSection(NetcdfData netcdfData) {
return new StringBuilder()
.append(
"<details>\n" +
"<summary>Parameters</summary>\n")
.append(
SwaggerUtils.locationBoundsInfo(netcdfData.minimumBounds(), netcdfData.maximumBounds(),
Optional.of("###")))
.append(SwaggerUtils.siteClassInfo(netcdfData.siteClasses(), Optional.of("###")))
.append("</details>")
.toString();
}
private static String swaggerResponseFormatSection() {
return new StringBuilder()
.append(
"<details>\n" +
"<summary>Response Format: CSV or JSON</summary>\n")
.append(
"The web service can respond in JSON or CSV format.\n" +
"<br><br>" +
"Choose the format type by adding a `format` query to the service call:" +
"`?format=JSON` or `?format=CSV`\n\n" +
"<br><br>" +
"Default: `JSON`")
.append("</details>")
.toString();
}
private static String swaggerScienceBaseSection(ScienceBaseMetadata scienceBaseMetadata) {
return new StringBuilder()
.append(
"<details>\n" +
"<summary>ScienceBase Information</summary>\n")
.append("Data history: " + scienceBaseMetadata.history)
.append("<br><br>")
.append(
String.format(
"Returned data are spatially interpolated from " +
"the %s grid data from ScienceBase:\n",
scienceBaseMetadata.gridStep))
.append(Arrays.stream(scienceBaseMetadata.scienceBaseInfo)
.map(info -> String.format("- [%s](%s)", info.url, info.url))
.collect(Collectors.joining("\n")))
.append("</details>")
.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment