Add JSON schema endpoint
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
dependencies {
|
||||
compile ratpack.dependency('jdbc-tx')
|
||||
compile 'com.fasterxml.jackson.module:jackson-module-jsonSchema:2.9.0'
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import com.devsoap.dbt.config.DBTConfig
|
||||
import com.devsoap.dbt.data.LedgerData
|
||||
import com.devsoap.dbt.handlers.ExecutorHandler
|
||||
import com.devsoap.dbt.handlers.ConfigInfoHandler
|
||||
import com.devsoap.dbt.handlers.JsonSchemaHandler
|
||||
import com.devsoap.dbt.handlers.LedgerGetTransactionHandler
|
||||
import com.devsoap.dbt.handlers.LedgerListTransactionsHandler
|
||||
import com.devsoap.dbt.handlers.LedgerUpdateTransactionHandler
|
||||
@@ -47,12 +48,14 @@ class DBTModule extends ConfigurableModule<DBTConfig> {
|
||||
|
||||
bind(ExecutorChainAction)
|
||||
bind(ExecutorHandler)
|
||||
bind(ConfigInfoHandler)
|
||||
|
||||
bind(LedgerData)
|
||||
bind(LedgerService)
|
||||
bind(TransactionManagerService)
|
||||
|
||||
bind(ConfigInfoHandler)
|
||||
bind(JsonSchemaHandler)
|
||||
|
||||
Multibinder.newSetBinder(binder(), HandlerDecorator).addBinding()
|
||||
.toInstance(HandlerDecorator.prependHandlers(LedgerChainAction))
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.devsoap.dbt.actions
|
||||
import com.devsoap.dbt.config.DBTConfig
|
||||
import com.devsoap.dbt.handlers.ExecutorHandler
|
||||
import com.devsoap.dbt.handlers.ConfigInfoHandler
|
||||
import com.devsoap.dbt.handlers.JsonSchemaHandler
|
||||
import com.google.inject.Inject
|
||||
import groovy.util.logging.Slf4j
|
||||
import ratpack.groovy.handling.GroovyChainAction
|
||||
@@ -42,5 +43,6 @@ class ExecutorChainAction extends GroovyChainAction {
|
||||
log.info("Registering executor at /$executorPath")
|
||||
path(executorPath, ExecutorHandler)
|
||||
path("$executorPath/config", ConfigInfoHandler)
|
||||
path("$executorPath/schema", JsonSchemaHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,11 @@ package com.devsoap.dbt.actions
|
||||
|
||||
import com.devsoap.dbt.config.DBTConfig
|
||||
import com.devsoap.dbt.handlers.ConfigInfoHandler
|
||||
import com.devsoap.dbt.handlers.JsonSchemaHandler
|
||||
import com.devsoap.dbt.handlers.LedgerGetTransactionHandler
|
||||
import com.devsoap.dbt.handlers.LedgerListTransactionsHandler
|
||||
import com.devsoap.dbt.handlers.LedgerUpdateTransactionHandler
|
||||
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator
|
||||
import com.google.inject.Inject
|
||||
import groovy.util.logging.Slf4j
|
||||
import ratpack.groovy.handling.GroovyChainAction
|
||||
@@ -48,5 +50,6 @@ class LedgerChainAction extends GroovyChainAction {
|
||||
registry.get(LedgerListTransactionsHandler)
|
||||
))
|
||||
path("$ledgerPath/config", ConfigInfoHandler)
|
||||
path("$ledgerPath/schema", JsonSchemaHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.devsoap.dbt.data
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import groovy.transform.ToString
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
@@ -25,6 +26,7 @@ class BlockTransaction implements Serializable {
|
||||
|
||||
String id = UUID.randomUUID().toString().replace('-','')
|
||||
|
||||
@JsonProperty(required = true)
|
||||
List<Query> queries = []
|
||||
|
||||
boolean completed = false
|
||||
@@ -61,6 +63,8 @@ class BlockTransaction implements Serializable {
|
||||
// A block in the chain
|
||||
@ToString
|
||||
static final class Query implements Serializable {
|
||||
|
||||
@JsonProperty(required = true)
|
||||
String query
|
||||
String id
|
||||
String parent
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2018 Devsoap Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.devsoap.dbt.handlers
|
||||
|
||||
import com.devsoap.dbt.config.DBTConfig
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2018 Devsoap Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.devsoap.dbt.handlers
|
||||
|
||||
import com.devsoap.dbt.data.BlockTransaction
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator
|
||||
import ratpack.handling.Context
|
||||
import ratpack.handling.Handler
|
||||
import ratpack.http.HttpMethod
|
||||
import ratpack.jackson.Jackson
|
||||
|
||||
import javax.inject.Inject
|
||||
|
||||
class JsonSchemaHandler implements Handler {
|
||||
|
||||
private final JsonSchemaGenerator generator
|
||||
|
||||
@Inject
|
||||
JsonSchemaHandler(ObjectMapper mapper) {
|
||||
this.generator = new JsonSchemaGenerator(mapper)
|
||||
}
|
||||
|
||||
@Override
|
||||
void handle(Context ctx) throws Exception {
|
||||
if(ctx.request.method == HttpMethod.GET) {
|
||||
def schema = generator.generateSchema(BlockTransaction)
|
||||
ctx.render Jackson.json(schema)
|
||||
} else {
|
||||
ctx.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user