Add HTTP API Server docs
All checks were successful
Build and Deploy MkDocs / build-next (push) Successful in 49s
All checks were successful
Build and Deploy MkDocs / build-next (push) Successful in 49s
This commit is contained in:
parent
05e53d3311
commit
50f28f68eb
47
docs/Docs/HTTP_API_Server.md
Normal file
47
docs/Docs/HTTP_API_Server.md
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
# HTTP API Server
|
||||||
|
|
||||||
|
PostalPoint runs a local HTTP server to allow communication with other devices on the LAN.
|
||||||
|
|
||||||
|
With the `httpserver` plugin API, plugins can add their own local API endpoints.
|
||||||
|
|
||||||
|
Valid HTTP API requests are POST requests with either a JSON request body, or an entirely empty body.
|
||||||
|
|
||||||
|
The default HTTP server settings are to bind to all addresses on port 7678. There is a basic API web client accessible on `/` for manual API access, for example, at http://localhost:7678/.
|
||||||
|
|
||||||
|
## Security
|
||||||
|
|
||||||
|
API requests are authenticated with a "Network Connection Key". Each installation of PostalPoint maintains a user-configurable list of keys which are allowed to connect to that installation's API server.
|
||||||
|
|
||||||
|
This means that, in order for a remote client to connect to PostalPoint, it must generate a random alphanumeric ID, which must be saved in the PostalPoint settings.
|
||||||
|
|
||||||
|
## Adding an API endpoint
|
||||||
|
|
||||||
|
This code adds an endpoint reachable by POST to `http://[local hostname]:[port]/testendpointname`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
global.apis.httpserver.addEndpoint("testendpointname", async function (request) {
|
||||||
|
// `request` is an object parsed from the request body.
|
||||||
|
if (request.abc == "123") {
|
||||||
|
// A non-string `body` is converted to JSON before the HTTP reply is sent.
|
||||||
|
return {body: {json: true, abc: 123}, httpcode: 200, contentType: "application/json"};
|
||||||
|
}
|
||||||
|
// A string `body` is sent to the client as-is using whatever contentType you specify.
|
||||||
|
return {body: "abc", httpcode: 200, contentType: "text/plain"};
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Connecting to a remote endpoint
|
||||||
|
|
||||||
|
By default, `sendRequestToRemote` uses the configured "Host PC", but a different hostname/IP may be specified.
|
||||||
|
|
||||||
|
This code hits the endpoint added above.
|
||||||
|
|
||||||
|
```js
|
||||||
|
try {
|
||||||
|
const responseObject = await global.apis.httpserver.sendRequestToRemote({abc: "123"}, "testendpointname");
|
||||||
|
// responseObject will be {json: true, abc: 123}
|
||||||
|
console.log(responseObject);
|
||||||
|
} catch (ex) {
|
||||||
|
global.apis.alert(ex.message, "Request Error");
|
||||||
|
}
|
||||||
|
```
|
||||||
@ -9,6 +9,7 @@ Add features to PostalPoint's integrated LAN HTTP API server.
|
|||||||
* [.addEndpoint(id, onCall)](#httpserver.addEndpoint)
|
* [.addEndpoint(id, onCall)](#httpserver.addEndpoint)
|
||||||
* [.getServerPort()](#httpserver.getServerPort) ⇒ <code>number</code>
|
* [.getServerPort()](#httpserver.getServerPort) ⇒ <code>number</code>
|
||||||
* [.getClientKey()](#httpserver.getClientKey) ⇒ <code>string</code>
|
* [.getClientKey()](#httpserver.getClientKey) ⇒ <code>string</code>
|
||||||
|
* [.sendRequestToRemote(data, endpointID, serverAddress, serverPort)](#httpserver.sendRequestToRemote) ⇒ <code>Promise.<Object></code>
|
||||||
|
|
||||||
<a name="httpserver.addEndpoint"></a>
|
<a name="httpserver.addEndpoint"></a>
|
||||||
|
|
||||||
@ -47,3 +48,22 @@ Get the local machine's HTTP client key it uses to authenticate with other
|
|||||||
installations of PostalPoint on the LAN.
|
installations of PostalPoint on the LAN.
|
||||||
|
|
||||||
**Kind**: static method of [<code>httpserver</code>](#httpserver)
|
**Kind**: static method of [<code>httpserver</code>](#httpserver)
|
||||||
|
<a name="httpserver.sendRequestToRemote"></a>
|
||||||
|
|
||||||
|
### httpserver.sendRequestToRemote(data, endpointID, serverAddress, serverPort) ⇒ <code>Promise.<Object></code>
|
||||||
|
Send a HTTP request to another PostalPoint installation on the local network.
|
||||||
|
|
||||||
|
**Kind**: static method of [<code>httpserver</code>](#httpserver)
|
||||||
|
**Returns**: <code>Promise.<Object></code> - - The JSON reply.
|
||||||
|
**Throws**:
|
||||||
|
|
||||||
|
- <code>Error</code> When there's a network or other unrecoverable error while completing the request. Error message is a human-readable description of the problem.
|
||||||
|
|
||||||
|
|
||||||
|
| Param | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| data | <code>Object</code> | Data to encode as JSON in the request body. |
|
||||||
|
| endpointID | <code>string</code> | Endpoint to call. |
|
||||||
|
| serverAddress | <code>string</code> \| <code>undefined</code> | Address of the PostalPoint server. If undefined, uses the host address configured in PostalPoint's Databases settings. |
|
||||||
|
| serverPort | <code>number</code> \| <code>undefined</code> | Port of the PostalPoint server. If undefined, the default PostalPoint port number is used. |
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user