diff --git a/dbt-core/build.gradle b/dbt-core/build.gradle index 7c0734c..18fc565 100644 --- a/dbt-core/build.gradle +++ b/dbt-core/build.gradle @@ -1,3 +1,4 @@ dependencies { compile ratpack.dependency('jdbc-tx') + compile 'com.fasterxml.jackson.module:jackson-module-jsonSchema:2.9.0' } \ No newline at end of file diff --git a/dbt-core/src/main/groovy/com/devsoap/dbt/DBTModule.groovy b/dbt-core/src/main/groovy/com/devsoap/dbt/DBTModule.groovy index 29c542d..a5302ed 100644 --- a/dbt-core/src/main/groovy/com/devsoap/dbt/DBTModule.groovy +++ b/dbt-core/src/main/groovy/com/devsoap/dbt/DBTModule.groovy @@ -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 { 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)) diff --git a/dbt-core/src/main/groovy/com/devsoap/dbt/actions/ExecutorChainAction.groovy b/dbt-core/src/main/groovy/com/devsoap/dbt/actions/ExecutorChainAction.groovy index 0d24418..f5db9e6 100644 --- a/dbt-core/src/main/groovy/com/devsoap/dbt/actions/ExecutorChainAction.groovy +++ b/dbt-core/src/main/groovy/com/devsoap/dbt/actions/ExecutorChainAction.groovy @@ -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) } } diff --git a/dbt-core/src/main/groovy/com/devsoap/dbt/actions/LedgerChainAction.groovy b/dbt-core/src/main/groovy/com/devsoap/dbt/actions/LedgerChainAction.groovy index b914609..c8701dd 100644 --- a/dbt-core/src/main/groovy/com/devsoap/dbt/actions/LedgerChainAction.groovy +++ b/dbt-core/src/main/groovy/com/devsoap/dbt/actions/LedgerChainAction.groovy @@ -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) } } diff --git a/dbt-core/src/main/groovy/com/devsoap/dbt/data/BlockTransaction.groovy b/dbt-core/src/main/groovy/com/devsoap/dbt/data/BlockTransaction.groovy index 11496e7..dc8756a 100644 --- a/dbt-core/src/main/groovy/com/devsoap/dbt/data/BlockTransaction.groovy +++ b/dbt-core/src/main/groovy/com/devsoap/dbt/data/BlockTransaction.groovy @@ -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 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 diff --git a/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/ConfigInfoHandler.groovy b/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/ConfigInfoHandler.groovy index 9cd8c78..d33d28d 100644 --- a/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/ConfigInfoHandler.groovy +++ b/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/ConfigInfoHandler.groovy @@ -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 diff --git a/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/JsonSchemaHandler.groovy b/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/JsonSchemaHandler.groovy new file mode 100644 index 0000000..9f922df --- /dev/null +++ b/dbt-core/src/main/groovy/com/devsoap/dbt/handlers/JsonSchemaHandler.groovy @@ -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() + } + } +}