ResourceBuilder

Defines resources inline, as an alternative to subclassing CoapResource:

val server = CoapServer(endpoint) {
resource("sensors") {
resource("temp") {
attributes = ResourceAttributes(resourceTypes = listOf("temperature-c"))
observable()
get { CoapResponse.content(readTemperature()) }
}
}
resource("echo") { post { CoapResponse(CoapCode.CHANGED, payload = payload) } }
resource("api") {
access = AccessPolicy.Authenticated // for /api and everything below it
resource("config") { put { CoapResponse.changed() } }
}
add(FirmwareResource())
}

A resource without handlers only groups children: it answers 4.04 and is not listed in /.well-known/core. One with handlers answers 4.05 for the other methods.

Properties

Link copied to clipboard

Who may use the resource and the resources below it without a policy of their own (ADR 0008); null, the default, inherits the parent's. At the top level of the server's routes it is the root's, which governs every resource that sets none. See AccessPolicy.

Link copied to clipboard

Link attributes for /.well-known/core.

Link copied to clipboard

Whether /.well-known/core lists the resource (when it has handlers).

Functions

Link copied to clipboard

Adds a subclassed resource as a child.

Link copied to clipboard

Handles DELETE (RFC 7252 §5.8.4).

Link copied to clipboard

Handles FETCH (RFC 8132 §2).

Link copied to clipboard

Handles GET (RFC 7252 §5.8.1); for an observable resource also every notification.

Link copied to clipboard

Handles iPATCH (RFC 8132 §3).

Link copied to clipboard
fun observable(type: CoapMessageType = CoapMessageType.CON)

Makes the resource observable (RFC 7641) with notifications of type.

Link copied to clipboard
fun on(method: CoapCode, handler: RequestHandlerFunction)

Handles method, for methods without a shortcut.

Link copied to clipboard

Handles PATCH (RFC 8132 §3).

Link copied to clipboard

Handles POST (RFC 7252 §5.8.2).

Link copied to clipboard

Handles PUT (RFC 7252 §5.8.3).

Link copied to clipboard
fun recognize(vararg optionNumbers: Int)

Critical options the handlers process themselves (see CoapResource.recognizedOptions).

Link copied to clipboard
fun resource(name: String, block: ResourceBuilder.() -> Unit = {}): ObservableResource

Adds a child defined by block and returns it, for example to call ObservableResource.notifyObservers.