Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample | How to add API spec / swagger UI to allow api testing from the WebUI #268

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ <h1>IthoWifi - API</h1>
</div>
<h3>API Description</h3>
<strong>General information WebAPI</strong><br><br>
<a href="swaggerUI.html" target="_blank">View OpenAPI specs and test your API here via SwaggerUI</a>
A simple WebAPI is available at the following URL: <a href='api.html' target='_blank'>api.html</a><br><br>
The request should be formatted as follows: <br>http://[DNS or IP]/api.html?[param]=[value]<br><br>
ie. http://192.168.4.1/api.html?command=medium<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IthoWifi API - SwaggerUI</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.5.0/swagger-ui.css">
<style>
body {
margin: 0;
padding: 0;
}
.swagger-ui {
margin: 20px auto;
}
.swagger-ui .topbar {
display: none;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.5.0/swagger-ui-bundle.min.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.5.0/swagger-ui-standalone-preset.min.js"> </script>
<script>
window.onload = function() {
const spec = `openapi: 3.0.0
info:
title: Web API
version: 1.0.0
paths:
/api.html:
get:
parameters:
- name: vremotecmd
in: query
schema:
type: string
enum: ["away", "low", "medium", "high", "timer1", "timer2", "timer3", "join", "leave", "auto", "autonight", "cook30", "cook60"]
- name: vremoteindex
in: query
schema:
type: string
pattern: "^[0-11]$"
- name: vremotename
in: query
schema:
type: string
- name: username
in: query
schema:
type: string
maxLength: 20
- name: password
in: query
schema:
type: string
maxLength: 20
- name: clearqueue
in: query
schema:
type: string
enum: ["true"]
- name: get
in: query
schema:
type: string
enum: ["ithostatus"]
- name: speed
in: query
schema:
type: string
pattern: "^[0-255]$"
- name: command
in: query
schema:
type: string
enum: ["low", "medium", "high", "timer1", "timer2", "timer3", "clearqueue"]
- name: timer
in: query
schema:
type: string
pattern: "^[0-65535]$"
- name: currentspeed
in: query
schema:
type: string
enum: ["ithostatus"]
responses:
'200':
description: Successful response
content:
text/plain:
example: OK
'400':
description: Unsuccessful response
content:
text/plain:
example: NOK
description: Format the request as http://[DNS or IP]/api.html?[param]=[value]
summary: Simple Web API`;

const ui = SwaggerUIBundle({
spec: jsyaml.load(spec),
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout"
});
window.ui = ui;
}
</script>
</body>
</html>