From 7a4afde6933a0409315e3fd9e8a00cc2737d65e2 Mon Sep 17 00:00:00 2001
From: Brandon Clayton <bclayton@usgs.gov>
Date: Thu, 10 Mar 2022 14:28:52 -0700
Subject: [PATCH] Custom service pattern string

---
 .../nshmp/netcdf/www/SwaggerController.java   | 51 +++++++++++++++++-
 .../nshmp/netcdf/www/SwaggerController.java   | 54 ++++++++++++++++++-
 .../nshmp/netcdf/www/NetcdfWsUtils.java       | 51 ++----------------
 3 files changed, 108 insertions(+), 48 deletions(-)

diff --git a/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java b/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java
index 1300367..c56272f 100644
--- a/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java
+++ b/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/SwaggerController.java
@@ -1,5 +1,6 @@
 package gov.usgs.earthquake.nshmp.netcdf.www;
 
+import java.io.IOException;
 import java.nio.file.Path;
 
 import gov.usgs.earthquake.nshmp.netcdf.NetcdfGroundMotions;
@@ -49,10 +50,58 @@ public class SwaggerController {
   @Get(produces = MediaType.TEXT_EVENT_STREAM)
   public HttpResponse<String> doGet(HttpRequest<?> request) {
     try {
-      var openApi = NetcdfWsUtils.getOpenApi(request, netcdf.netcdfData(), "/spectra");
+      var openApi = NetcdfWsUtils.getOpenApi(
+          request,
+          netcdf.netcdfData(),
+          servicePatternSection(request));
       return HttpResponse.ok(Yaml.pretty(openApi));
     } 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 + "/spectra?latitude={number}&longitude={number}\n" +
+                url + "/spectra?latitude={number}&longitude={number}&siteClass={string}\n" +
+                "````\n" +
+
+                "Example:\n" +
+                "```text\n" +
+                url + "/spectra?latitude=34&longitude=-118\n" +
+                url + "/spectra?latitude=34&longitude=-118&siteClass=BC\n" +
+                "```\n")
+        .append(
+            "### Slash Pattern\n" +
+
+                "The slash based service call is in the form of:\n" +
+
+                "```text\n" +
+                url + "/spectra/{latitude}/{longitude}\n" +
+                url + "/spectra/{latitude}/{longitude}/{siteClass}\n" +
+                "```\n" +
+
+                "Example:\n" +
+
+                "```text\n" +
+                url + "/spectra/34/-118\n" +
+                url + "/spectra/34/-118/BC\n" +
+                "```\n")
+        .append("</details>")
+        .toString();
+  }
+
 }
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 6db0187..819b18b 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,5 +1,6 @@
 package gov.usgs.earthquake.nshmp.netcdf.www;
 
+import java.io.IOException;
 import java.nio.file.Path;
 
 import gov.usgs.earthquake.nshmp.netcdf.NetcdfHazardCurves;
@@ -50,11 +51,62 @@ public class SwaggerController {
   @Get(produces = MediaType.TEXT_EVENT_STREAM)
   public HttpResponse<String> doGet(HttpRequest<?> request) {
     try {
-      var openApi = NetcdfWsUtils.getOpenApi(request, netcdf.netcdfData(), "/hazard");
+      var openApi = NetcdfWsUtils.getOpenApi(
+          request,
+          netcdf.netcdfData(),
+          servicePatternSection(request));
       SwaggerUtils.imtSchema(openApi.getComponents().getSchemas(), netcdf.netcdfData().imts());
       return HttpResponse.ok(Yaml.pretty(openApi));
     } 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();
+  }
 }
diff --git a/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/NetcdfWsUtils.java b/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/NetcdfWsUtils.java
index 8c4614e..83ac0be 100644
--- a/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/NetcdfWsUtils.java
+++ b/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/NetcdfWsUtils.java
@@ -66,8 +66,10 @@ public class NetcdfWsUtils {
     return HttpResponse.serverError(response);
   }
 
-  public static OpenAPI getOpenApi(HttpRequest<?> request, NetcdfData netcdfData,
-      String servicePath) throws IOException {
+  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());
@@ -81,7 +83,7 @@ public class NetcdfWsUtils {
     // Update description
     var description = new StringBuilder()
         .append(scienceBaseMetadata.description + "\n")
-        .append(servicePatternSection(request, servicePath))
+        .append(serviceSection)
         .append(swaggerParameterSection(netcdfData))
         .append(swaggerScienceBaseSection(scienceBaseMetadata))
         .toString();
@@ -102,49 +104,6 @@ public class NetcdfWsUtils {
     }
   }
 
-  private static String servicePatternSection(HttpRequest<?> request, String servicePath)
-      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 + servicePath + "?longitude={number}&latitude={number}&siteClass={string}\n" +
-                "````\n" +
-
-                "Example:\n" +
-                "```text\n" +
-                url + servicePath + "?longitude=-118&latitude=34&siteClass=BC\n" +
-                "```\n")
-        .append(
-            "### Slash Pattern\n" +
-
-                "The slash based service call is in the form of:\n" +
-
-                "```text\n" +
-                url + servicePath + "/{longitude}/{latitude}/{siteClass}\n" +
-                "```\n" +
-
-                "Example:\n" +
-
-                "```text\n" +
-                url + servicePath + "/-118/34/BC\n" +
-                "```\n" +
-
-                "> Note: To obtain data for all site classes simply " +
-                "call the query or slashed based service with no site class parameter.\n")
-        .append("</details>")
-        .toString();
-  }
-
   private static String swaggerParameterSection(NetcdfData netcdfData) {
     return new StringBuilder()
         .append(
-- 
GitLab