From b8613cc7209fafea9094e8aac28146d62627ee4f Mon Sep 17 00:00:00 2001
From: Brandon Clayton <bclayton@usgs.gov>
Date: Thu, 21 Jul 2022 09:43:33 -0600
Subject: [PATCH] Use swagger class for updating open api

---
 .../nshmp/netcdf/www/SwaggerController.java   | 68 ++-----------------
 1 file changed, 7 insertions(+), 61 deletions(-)

diff --git a/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java b/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java
index ac31ade..40b1f27 100644
--- a/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java
+++ b/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java
@@ -1,11 +1,9 @@
 package gov.usgs.earthquake.nshmp.netcdf.www;
 
-import java.io.IOException;
 import java.nio.file.Path;
 
-import gov.usgs.earthquake.nshmp.netcdf.NetcdfHazardCurves;
+import gov.usgs.earthquake.nshmp.netcdf.NetcdfDataFilesHazardCurves;
 import gov.usgs.earthquake.nshmp.www.NshmpMicronautServlet;
-import gov.usgs.earthquake.nshmp.www.SwaggerUtils;
 
 import io.micronaut.context.annotation.Value;
 import io.micronaut.context.event.StartupEvent;
@@ -35,79 +33,27 @@ public class SwaggerController {
   @Inject
   private NshmpMicronautServlet servlet;
 
-  @Value("${nshmp-ws-static.netcdf-file}")
+  @Value("${nshmp-ws-static.netcdf-path}")
   Path netcdfPath;
 
-  NetcdfHazardCurves netcdf;
+  NetcdfServiceHazardCurves service;
 
   /**
    * Read in data type and return the appropriate service to use.
    */
   @EventListener
   void startup(StartupEvent event) {
-    netcdf = new NetcdfHazardCurves(netcdfPath);
+    var netcdfDataFiles = new NetcdfDataFilesHazardCurves(netcdfPath);
+    service = new NetcdfServiceHazardCurves(netcdfDataFiles);
   }
 
   @Get(produces = MediaType.TEXT_EVENT_STREAM)
   public HttpResponse<String> doGet(HttpRequest<?> request) {
     try {
-      var openApi = NetcdfWsUtils.getOpenApi(
-          request,
-          netcdf.netcdfData(),
-          servicePatternSection(request));
-      SwaggerUtils.imtSchema(openApi.getComponents().getSchemas(), netcdf.netcdfData().imts());
-      return HttpResponse.ok(Yaml.pretty(openApi));
+      SwaggerHazardCurves swagger = new SwaggerHazardCurves(request, service);
+      return HttpResponse.ok(Yaml.pretty(swagger.updateOpenApi()));
     } catch (Exception e) {
       return NetcdfWsUtils.handleError(e, "Swagger", request.getUri().getPath());
     }
   }
-
-  private static String servicePatternSection(HttpRequest<?> request)
-      throws IOException {
-    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?longitude={number}&latitude={number}\n" +
-                url + "/hazard?longitude={number}&latitude={number}&siteClass={string}\n" +
-                url +
-                "/hazard?longitude={number}&latitude={number}&siteClass={string}&imt={string}\n" +
-                "````\n" +
-
-                "Example:\n" +
-                "```text\n" +
-                url + "/hazard?longitude=-118&latitude=34\n" +
-                url + "/hazard?longitude=-118&latitude=34&siteClass=BC\n" +
-                url + "/hazard?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/{longitude}/{latitude}\n" +
-                url + "/hazard/{longitude}/{latitude}/{siteClass}\n" +
-                url + "/hazard/{longitude}/{latitude}/{siteClass}/{imt}\n" +
-                "```\n" +
-
-                "Example:\n" +
-
-                "```text\n" +
-                url + "/hazard/-118/34\n" +
-                url + "/hazard/-118/34/BC\n" +
-                url + "/hazard/-118/34/BC/PGA\n" +
-                "```\n")
-        .append("</details>")
-        .toString();
-  }
 }
-- 
GitLab