Package io.openems.edge.common.jsonapi
Class JsonApiBuilder
java.lang.Object
io.openems.edge.common.jsonapi.JsonApiBuilder
TODO route interceptor/observer/notifications.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBuilder
(JsonApiBuilder builder) Adds aJsonApiBuilder
to the current builder.void
addEndpointAddedListener
(Consumer<JsonApiEndpoint> listener) Adds a listener to call when a endpoint got added to the current builder.void
addEndpointRemovedListener
(Consumer<JsonApiEndpoint> listener) Adds a listener to call when a endpoint got removed to the current builder.void
close()
Closes this builder.delegate
(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler) Delegates the handled request to another endpoint.delegate
(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder) Delegates the handled request to another endpoint.delegate
(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder, Supplier<List<Subrequest>> subroutes) Delegates the handled request to another endpoint.delegate
(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler) Delegates the handled request to another endpoint.delegate
(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder) Delegates the handled request to another endpoint.delegate
(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder, Function<JsonrpcResponse, JsonrpcResponse> responseMapper, Supplier<List<Subrequest>> subroutes) Delegates the handled request to another endpoint.void
handle
(Call<JsonrpcRequest, JsonrpcResponse> call) Handles aCall
.<REQUEST,
RESPONSE>
JsonApiBuilderhandleRequest
(EndpointRequestType<REQUEST, RESPONSE> endpointType, Consumer<EndpointDefinitionBuilder<REQUEST, RESPONSE>> defBuilder, ThrowingFunction<Call<REQUEST, RESPONSE>, RESPONSE, Exception> handler) Adds a rpc request handler to the current builder.handleRequest
(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcResponse, Exception> handler) Adds a rpc request handler to the current builder.handleRequest
(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcResponse, Exception> handler) Adds a rpc request handler to the current builder.boolean
isDebug()
void
removeBuilder
(JsonApiBuilder builder) Removes aJsonApiBuilder
and all its methods from this builder.removeEndpoint
(String method) Removes an endpoint by its method.void
removeEndpointAddedListener
(Consumer<JsonApiEndpoint> listener) Removes a listener which was subscribed to the endpoint added event.void
removeEndpointRemovedListener
(Consumer<JsonApiEndpoint> listener) Removes a listener which was subscribed to the endpoint removed event.rpc
(String method, ThrowingConsumer<Call<JsonrpcRequest, JsonrpcResponse>, Exception> handler) Adds a rpc handler to the current builder.rpc
(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingConsumer<Call<JsonrpcRequest, JsonrpcResponse>, Exception> handler) Adds a rpc handler to the current builder.rpc
(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, Supplier<List<Subrequest>> subroutes, ThrowingConsumer<Call<JsonrpcRequest, JsonrpcResponse>, Exception> handler) Adds a rpc handler to the current builder.<REQUEST> JsonApiBuilder
rpc
(String method, Consumer<EndpointDefinitionBuilder<REQUEST, JsonrpcResponse>> defBuilder, Supplier<List<Subrequest>> subroutes, ThrowingConsumer<Call<REQUEST, JsonrpcResponse>, Exception> handler, JsonSerializer<REQUEST> serializer) Adds a rpc handler to the current builder.<REQUEST,
RESPONSE>
JsonApiBuilderrpc
(String method, Consumer<EndpointDefinitionBuilder<REQUEST, RESPONSE>> defBuilder, ThrowingConsumer<Call<REQUEST, RESPONSE>, Exception> handler, JsonSerializer<REQUEST> requestSerializer, JsonSerializer<RESPONSE> responseSerializer) Adds a rpc handler to the current builder.void
setDebug
(boolean debug)
-
Constructor Details
-
JsonApiBuilder
public JsonApiBuilder()
-
-
Method Details
-
handleRequest
public JsonApiBuilder handleRequest(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcResponse, Exception> handler) Adds a rpc request handler to the current builder.- Parameters:
method
- the method of the handled rpc requesthandler
- the handler of the request- Returns:
- this
-
handleRequest
public JsonApiBuilder handleRequest(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcResponse, Exception> handler) Adds a rpc request handler to the current builder.- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the request- Returns:
- this
-
handleRequest
public <REQUEST,RESPONSE> JsonApiBuilder handleRequest(EndpointRequestType<REQUEST, RESPONSE> endpointType, Consumer<EndpointDefinitionBuilder<REQUEST, RESPONSE>> defBuilder, ThrowingFunction<Call<REQUEST, RESPONSE>, RESPONSE, Exception> handler) Adds a rpc request handler to the current builder.- Type Parameters:
REQUEST
- the type of the requestRESPONSE
- the type of the response- Parameters:
endpointType
- theEndpointRequestType
of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the request- Returns:
- this
-
rpc
public JsonApiBuilder rpc(String method, ThrowingConsumer<Call<JsonrpcRequest, JsonrpcResponse>, Exception> handler) Adds a rpc handler to the current builder.- Parameters:
method
- the method of the handled rpc requesthandler
- the handler of the request- Returns:
- this
-
rpc
public JsonApiBuilder rpc(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingConsumer<Call<JsonrpcRequest, JsonrpcResponse>, Exception> handler) Adds a rpc handler to the current builder.- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the request- Returns:
- this
-
rpc
public <REQUEST,RESPONSE> JsonApiBuilder rpc(String method, Consumer<EndpointDefinitionBuilder<REQUEST, RESPONSE>> defBuilder, ThrowingConsumer<Call<REQUEST, RESPONSE>, Exception> handler, JsonSerializer<REQUEST> requestSerializer, JsonSerializer<RESPONSE> responseSerializer) Adds a rpc handler to the current builder.- Type Parameters:
REQUEST
- the type of the requestRESPONSE
- the type of the response- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the requestrequestSerializer
- theJsonSerializer
of the requestresponseSerializer
- theJsonSerializer
of the response- Returns:
- this
-
rpc
public <REQUEST> JsonApiBuilder rpc(String method, Consumer<EndpointDefinitionBuilder<REQUEST, JsonrpcResponse>> defBuilder, Supplier<List<Subrequest>> subroutes, ThrowingConsumer<Call<REQUEST, JsonrpcResponse>, Exception> handler, JsonSerializer<REQUEST> serializer) Adds a rpc handler to the current builder.- Type Parameters:
REQUEST
- the type of the request- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
subroutes
- the subroutes which can be reached with this handlerhandler
- the handler of the requestserializer
- theJsonSerializer
of the request- Returns:
- this
-
rpc
public JsonApiBuilder rpc(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, Supplier<List<Subrequest>> subroutes, ThrowingConsumer<Call<JsonrpcRequest, JsonrpcResponse>, Exception> handler) Adds a rpc handler to the current builder.- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
subroutes
- the subroutes which can be reached with this handlerhandler
- the handler of the request- Returns:
- this
-
delegate
public JsonApiBuilder delegate(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder, Function<JsonrpcResponse, JsonrpcResponse> responseMapper, Supplier<List<Subrequest>> subroutes) Delegates the handled request to another endpoint.- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the request which returns the delegated requestbuilder
- the path to the builder which handles the delegated requestresponseMapper
- the mapper of the responsesubroutes
- the subroutes which can be reached with this handler- Returns:
- this
-
delegate
public JsonApiBuilder delegate(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder, Supplier<List<Subrequest>> subroutes) Delegates the handled request to another endpoint.- Parameters:
method
- the method of the handled rpc requesthandler
- the handler of the request which returns the delegated requestbuilder
- the path to the builder which handles the delegated requestsubroutes
- the subroutes which can be reached with this handler- Returns:
- this
-
delegate
public JsonApiBuilder delegate(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder) Delegates the handled request to another endpoint.- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the request which returns the delegated requestbuilder
- the path to the builder which handles the delegated request- Returns:
- this
-
delegate
public JsonApiBuilder delegate(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler, Function<JsonApiBuilder, JsonApiBuilder> builder) Delegates the handled request to another endpoint.- Parameters:
method
- the method of the handled rpc requesthandler
- the handler of the request which returns the delegated requestbuilder
- the path to the builder which handles the delegated request- Returns:
- this
-
delegate
public JsonApiBuilder delegate(String method, Consumer<EndpointDefinitionBuilder<JsonrpcRequest, JsonrpcResponse>> defBuilder, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler) Delegates the handled request to another endpoint.- Parameters:
method
- the method of the handled rpc requestdefBuilder
- the builder for theEndpointDefinitionBuilder
handler
- the handler of the request which returns the delegated request- Returns:
- this
-
delegate
public JsonApiBuilder delegate(String method, ThrowingFunction<Call<JsonrpcRequest, JsonrpcResponse>, JsonrpcRequest, Exception> handler) Delegates the handled request to another endpoint.- Parameters:
method
- the method of the handled rpc requesthandler
- the handler of the request which returns the delegated request- Returns:
- this
-
removeEndpoint
Removes an endpoint by its method.- Parameters:
method
- the method of the endpoint to remove- Returns:
- the endpoint which got removed or null if no endpoint with this method exists
-
addBuilder
Adds aJsonApiBuilder
to the current builder. All methods are "copied" to this builder and available for both then.- Parameters:
builder
- theJsonApiBuilder
to add- See Also:
-
removeBuilder
Removes aJsonApiBuilder
and all its methods from this builder.- Parameters:
builder
- theJsonApiBuilder
to remove- See Also:
-
addEndpointAddedListener
Adds a listener to call when a endpoint got added to the current builder.- Parameters:
listener
- the listener to call when the event happened
-
removeEndpointAddedListener
Removes a listener which was subscribed to the endpoint added event.- Parameters:
listener
- the listener to remove
-
addEndpointRemovedListener
Adds a listener to call when a endpoint got removed to the current builder.- Parameters:
listener
- the listener to call when the event happened
-
removeEndpointRemovedListener
Removes a listener which was subscribed to the endpoint removed event.- Parameters:
listener
- the listener to remove
-
getEndpoints
-
isDebug
public boolean isDebug() -
setDebug
public void setDebug(boolean debug) -
close
public void close()Closes this builder.Removes all
JsonApiBuilder
from this builder. -
handle
Handles aCall
.A
Call
is usually created and passed to a builder when a request from an user is called in the UI or via code directly (BackendController, WebsocketController, RestApi, ...).- Parameters:
call
- the call to handle
-