Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nshmp-ws-static
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ghsc
National Seismic Hazard Model Project
nshmp-ws-static
Commits
e0688ecd
Commit
e0688ecd
authored
2 years ago
by
Clayton, Brandon Scott
Browse files
Options
Downloads
Patches
Plain Diff
abstract class for updating swagger landing page
parent
ce8fd3f6
No related branches found
No related tags found
2 merge requests
!128
Production Release | nshmp-ws-static
,
!127
Resolves - Handle Multiple NetCDF Files for AASHTO Service
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/Swagger.java
+173
-0
173 additions, 0 deletions
...in/java/gov/usgs/earthquake/nshmp/netcdf/www/Swagger.java
with
173 additions
and
0 deletions
src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/Swagger.java
0 → 100644
+
173
−
0
View file @
e0688ecd
package
gov.usgs.earthquake.nshmp.netcdf.www
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
gov.usgs.earthquake.nshmp.geo.Bounds
;
import
gov.usgs.earthquake.nshmp.geo.Location
;
import
gov.usgs.earthquake.nshmp.geo.LocationList
;
import
gov.usgs.earthquake.nshmp.netcdf.NetcdfDataFiles
;
import
gov.usgs.earthquake.nshmp.netcdf.data.NetcdfData
;
import
gov.usgs.earthquake.nshmp.netcdf.data.ScienceBaseMetadata
;
import
gov.usgs.earthquake.nshmp.www.SwaggerUtils
;
import
io.micronaut.http.HttpRequest
;
import
io.swagger.v3.oas.models.Components
;
import
io.swagger.v3.oas.models.OpenAPI
;
import
io.swagger.v3.oas.models.media.Schema
;
import
io.swagger.v3.parser.OpenAPIV3Parser
;
/**
* Update Swagger landing page.
*
* @author U.S. Geological Survey
*/
abstract
class
Swagger
<
T
extends
NetcdfService
<?>>
{
final
HttpRequest
<?>
request
;
final
T
service
;
Swagger
(
HttpRequest
<?>
request
,
T
service
)
{
this
.
request
=
request
;
this
.
service
=
service
;
}
/**
* Returns the main description header text.
*/
abstract
String
descriptionHeader
();
/**
* Returns the service info text.
*/
abstract
String
serviceInfo
();
/**
* Returns the service patterns sections
*/
abstract
String
servicePatternSection
();
/**
* Creeates the main swagger description section.
*
* @param netcdfDataFiles The data files
*/
String
description
(
NetcdfDataFiles
<?>
netcdfDataFiles
)
{
StringBuilder
builder
=
new
StringBuilder
()
.
append
(
serviceInfo
())
.
append
(
"\n## "
+
descriptionHeader
()
+
"\n"
);
netcdfDataFiles
.
forEach
(
netcdf
->
{
ScienceBaseMetadata
metadata
=
netcdf
.
netcdfData
().
scienceBaseMetadata
();
builder
.
append
(
"### "
+
metadata
.
label
+
"\n"
)
.
append
(
metadata
.
description
+
"\n"
)
.
append
(
parameterSection
(
netcdf
.
netcdfData
())
+
"\n"
)
.
append
(
scienceBaseSection
(
metadata
)
+
"\n"
);
});
return
builder
.
toString
();
}
/**
* Creates the parameter section for the description.
*
* @param netcdfData The NetCDF data
*/
String
parameterSection
(
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
();
}
/**
* Creates the response format section for the description.
*/
String
responseFormatSection
()
{
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
();
}
/**
* Creates the science base section for the description.
*
* @param scienceBaseMetadata The metadata.
*/
String
scienceBaseSection
(
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
();
}
void
updateLocationBounds
(
OpenAPI
openApi
)
{
List
<
Location
>
locations
=
service
.
netcdfDataFiles
().
stream
()
.
map
(
netcdf
->
netcdf
.
netcdfData
())
.
flatMap
(
netcdfData
->
List
.
of
(
netcdfData
.
minimumBounds
(),
netcdfData
.
maximumBounds
()).
stream
())
.
collect
(
Collectors
.
toList
());
Bounds
bounds
=
LocationList
.
copyOf
(
locations
).
bounds
();
SwaggerUtils
.
addLocationBounds
(
openApi
,
bounds
.
min
,
bounds
.
max
);
}
/**
* Update the {@link OpenAPI} documentation.
*
* @throws IOException
*/
OpenAPI
updateOpenApi
()
throws
IOException
{
OpenAPI
openApi
=
new
OpenAPIV3Parser
().
read
(
"META-INF/swagger/nshmp-ws-static.yml"
);
updateLocationBounds
(
openApi
);
Components
components
=
openApi
.
getComponents
();
Map
<
String
,
Schema
>
schemas
=
components
.
getSchemas
();
SwaggerUtils
.
siteClassSchema
(
schemas
,
List
.
copyOf
(
service
.
netcdfDataFiles
().
siteClasses
()));
openApi
.
servers
(
null
);
openApi
.
getInfo
().
setTitle
(
service
.
getServiceName
());
// Update description
String
description
=
new
StringBuilder
()
.
append
(
description
(
service
.
netcdfDataFiles
()))
.
append
(
"## Formating \n"
+
responseFormatSection
()
+
"\n"
)
.
append
(
"## Service Patterns \n"
+
servicePatternSection
())
.
toString
();
openApi
.
getInfo
().
setDescription
(
description
);
return
openApi
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment