-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serve a Thing Description for the gateway - closes #2927
- Loading branch information
1 parent
7aca9ee
commit 3713d6e
Showing
6 changed files
with
347 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* API Root Controller. | ||
* | ||
* Handles requests to /. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import express from 'express'; | ||
import Gateway from '../models/gateway'; | ||
|
||
function build(): express.Router { | ||
const controller = express.Router(); | ||
|
||
/** | ||
* WoT Thing Description Directory | ||
* https://www.w3.org/TR/wot-discovery/#exploration-directory | ||
*/ | ||
controller.get('/', (request, response) => { | ||
const host = request.headers.host; | ||
const secure = request.secure; | ||
const td = Gateway.getDescription(host, secure); | ||
response.set('Content-type', 'application/td+json'); | ||
response.status(200).send(td); | ||
}); | ||
|
||
return controller; | ||
} | ||
|
||
export default build; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
/** | ||
* Gateway Model. | ||
* | ||
* Represents the gateway and its interaction affordances, including | ||
* acting as a Thing Description Directory. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import * as Constants from '../constants'; | ||
import { ThingDescription } from './thing'; | ||
|
||
export default class Gateway { | ||
|
||
/** | ||
* | ||
* Get a JSON Thing Description for this gateway. | ||
* | ||
* @param {String} reqHost request host, if coming via HTTP | ||
* @param {Boolean} reqSecure whether or not the request is secure, i.e. TLS | ||
* @returns {ThingDescription} A Thing Description describing the gateway. | ||
*/ | ||
static getDescription(reqHost?: string, reqSecure?: boolean): ThingDescription { | ||
const origin = `${reqSecure ? 'https' : 'http'}://${reqHost}`; | ||
const desc: ThingDescription = { | ||
'@context': [ | ||
'https://www.w3.org/2022/wot/td/v1.1', | ||
'https://www.w3.org/2022/wot/discovery' | ||
], | ||
'@type': 'ThingDirectory', | ||
'id': origin, | ||
'base': origin, | ||
'title': 'WebThings Gateway', | ||
'securityDefinitions': { | ||
'oauth2_sc': { | ||
'scheme': 'oauth2', | ||
'flow': 'code', | ||
'authorization': `${origin}${Constants.OAUTH_PATH}/authorize`, | ||
'token': `${origin}${Constants.OAUTH_PATH}/token`, | ||
'scopes': [Constants.THINGS_PATH, `${Constants.THINGS_PATH}:readwrite`], | ||
} | ||
}, | ||
'security': 'oauth2_sc', | ||
'properties': { | ||
'things': { | ||
'title': 'Things', | ||
'description': 'Retrieve all Thing Descriptions', | ||
'type': 'array', | ||
'items': { | ||
'type': 'object' | ||
}, | ||
'forms': [ | ||
{ | ||
'href': '/things', | ||
'htv:methodName': 'GET', | ||
'response': { | ||
'description': 'Success response', | ||
'htv:statusCodeValue': 200, | ||
'contentType': 'application/json' | ||
}, | ||
'additionalResponses': [ | ||
{ | ||
'description': 'Token must contain scope', | ||
'htv:statusCodeValue': 400 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
'actions': { | ||
'createAnonymousThing': { | ||
'description': 'Create a Thing Description', | ||
'input': { | ||
'type': 'object' | ||
}, | ||
'forms': [ | ||
{ | ||
'href': '/things', | ||
'htv:methodName': 'POST', | ||
'contentType': 'application/json', | ||
'response': { | ||
'htv:statusCodeValue': 201 | ||
}, | ||
'additionalResponses': [ | ||
{ | ||
'description': 'Invalid or duplicate Thing Description', | ||
'htv:statusCodeValue': 400 | ||
}, | ||
{ | ||
'description': 'Internal error saving new Thing Description', | ||
'htv:statusCodeValue': 500 | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
'retrieveThing': { | ||
'description': 'Retrieve a Thing Description', | ||
'uriVariables': { | ||
'id': { | ||
'@type': 'ThingID', | ||
'title': 'Thing Description ID', | ||
'type': 'string', | ||
'format': 'iri-reference' | ||
} | ||
}, | ||
'output': { | ||
'description': 'The schema is implied by the content type', | ||
'type': 'object' | ||
}, | ||
'safe': true, | ||
'idempotent': true, | ||
'forms': [ | ||
{ | ||
'href': '/things/{id}', | ||
'htv:methodName': 'GET', | ||
'response': { | ||
'description': 'Success response', | ||
'htv:statusCodeValue': 200, | ||
'contentType': 'application/json' | ||
}, | ||
'additionalResponses': [ | ||
{ | ||
'description': 'TD with the given id not found', | ||
'htv:statusCodeValue': 404 | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
'updateThing': { | ||
'description': 'Update a Thing Description', | ||
'uriVariables': { | ||
'id': { | ||
'@type': 'ThingID', | ||
'title': 'Thing Description ID', | ||
'type': 'string', | ||
'format': 'iri-reference' | ||
} | ||
}, | ||
'input': { | ||
'type': 'object' | ||
}, | ||
'forms': [ | ||
{ | ||
'href': '/things/{id}', | ||
'htv:methodName': 'PUT', | ||
'contentType': 'application/json', | ||
'response': { | ||
'description': 'Success response', | ||
'htv:statusCodeValue': 200 | ||
}, | ||
'additionalResponses': [ | ||
{ | ||
'description': 'Invalid serialization or TD', | ||
'htv:statusCodeValue': 400 | ||
}, | ||
{ | ||
'description': 'Failed to update Thing', | ||
'htv:statusCodeValue': 500 | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
'partiallyUpdateThing': { | ||
'description': 'Partially update a Thing Description', | ||
'uriVariables': { | ||
'id': { | ||
'@type': 'ThingID', | ||
'title': 'Thing Description ID', | ||
'type': 'string', | ||
'format': 'iri-reference' | ||
} | ||
}, | ||
'input': { | ||
'type': 'object' | ||
}, | ||
'forms': [ | ||
{ | ||
'href': '/things/{id}', | ||
'htv:methodName': 'PATCH', | ||
'contentType': 'application/merge-patch+json', | ||
'response': { | ||
'description': 'Success response', | ||
'htv:statusCodeValue': 200 | ||
}, | ||
'additionalResponses': [ | ||
{ | ||
'description': 'Request body missing required parameters', | ||
'htv:statusCodeValue': 400 | ||
}, | ||
{ | ||
'description': 'TD with the given id not found', | ||
'htv:statusCodeValue': 404 | ||
}, | ||
{ | ||
'description': 'Failed to update Thing', | ||
'htv:statusCodeValue': 500 | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
'deleteThing': { | ||
'description': 'Delete a Thing Description', | ||
'uriVariables': { | ||
'id': { | ||
'@type': 'ThingID', | ||
'title': 'Thing Description ID', | ||
'type': 'string', | ||
'format': 'iri-reference' | ||
} | ||
}, | ||
'forms': [ | ||
{ | ||
'href': '/things/{id}', | ||
'htv:methodName': 'DELETE', | ||
'response': { | ||
'description': 'Success response', | ||
'htv:statusCodeValue': 204 | ||
}, | ||
'additionalResponses': [ | ||
{ | ||
'description': 'TD with the given id not found', | ||
'htv:statusCodeValue': 404 | ||
}, | ||
{ | ||
'description': 'Failed to remove Thing', | ||
'htv:statusCodeValue': 500 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
|
||
return desc; | ||
} | ||
} |
Oops, something went wrong.