From 971e2c65d03c525b840a58243f35b75a63154cab Mon Sep 17 00:00:00 2001 From: augustearth Date: Wed, 6 Oct 2021 20:57:49 -0400 Subject: [PATCH] #7 --- .../config/application.yml | 9 ++ .../micronaut/config/AppConfig.groovy | 23 +++ .../micronaut/vine/DatabaseVine.groovy | 80 +++++++++++ .../micronaut/vine/GraphqlVine.groovy | 132 ++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 src/main/groovy/example/carnival/micronaut/vine/DatabaseVine.groovy create mode 100644 src/main/groovy/example/carnival/micronaut/vine/GraphqlVine.groovy diff --git a/carnival-micronaut-home/config/application.yml b/carnival-micronaut-home/config/application.yml index 641a717..71aa720 100644 --- a/carnival-micronaut-home/config/application.yml +++ b/carnival-micronaut-home/config/application.yml @@ -8,6 +8,15 @@ carnival-micronaut: graph: runtime: "neo4j" test: "tinker" + database: + server: database-server-name + port: database-port-as-integer + user: database-auth-username + password: database-auth-password + graphql-service: + api: + token: an-api-token + carnival: cache-mode: OPTIONAL directories: diff --git a/src/main/groovy/example/carnival/micronaut/config/AppConfig.groovy b/src/main/groovy/example/carnival/micronaut/config/AppConfig.groovy index 624d1ae..8f275b7 100644 --- a/src/main/groovy/example/carnival/micronaut/config/AppConfig.groovy +++ b/src/main/groovy/example/carnival/micronaut/config/AppConfig.groovy @@ -31,4 +31,27 @@ class AppConfig { } SubConfig subConfig = new SubConfig() + + @ConfigurationProperties("example-database") + static class ExampleDatabase { + String server + Integer port + String user + String password + } + ExampleDatabase exampleDatabase = new ExampleDatabase() + + + @ConfigurationProperties("graphql-service") + static class GraphqlService { + + @ConfigurationProperties("api") + static class Api { + String token + } + Api api = new Api() + + } + GraphqlService graphqlService = new GraphqlService() + } \ No newline at end of file diff --git a/src/main/groovy/example/carnival/micronaut/vine/DatabaseVine.groovy b/src/main/groovy/example/carnival/micronaut/vine/DatabaseVine.groovy new file mode 100644 index 0000000..928b638 --- /dev/null +++ b/src/main/groovy/example/carnival/micronaut/vine/DatabaseVine.groovy @@ -0,0 +1,80 @@ +package example.carnival.micronaut.vine + + + +import javax.inject.Singleton +import javax.inject.Inject + +import groovy.transform.ToString +import groovy.util.logging.Slf4j +import groovy.sql.Sql + +import static java.sql.ResultSet.* + +import io.micronaut.context.annotation.Property + +import carnival.util.GenericDataTable +import carnival.util.MappedDataTable +import carnival.core.vine.Vine +import carnival.core.vine.MappedDataTableVineMethod +import carnival.core.vine.GenericDataTableVineMethod + +import example.carnival.micronaut.config.AppConfig + + + +@ToString(includeNames=true) +@Slf4j +@Singleton +class DatabaseVine implements Vine { + + /////////////////////////////////////////////////////////////////////////// + // FIELDS + /////////////////////////////////////////////////////////////////////////// + + @Inject AppConfig config + + + /////////////////////////////////////////////////////////////////////////// + // UTILITY + /////////////////////////////////////////////////////////////////////////// + + Sql connect() { + Sql.newInstance( + driver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver', + url: "jdbc:sqlserver://${config.database.server}:${config.database.port};databaseName=TheDatabaseName;", + user: config.database.user, + password: config.database.password + ) + } + + + class MyQuery extends MappedDataTableVineMethod { + + MappedDataTable fetch(Map args) { + log.trace "database connect()" + def sql = connect() + + def mdt = createDataTable(idFieldName:'ID') + + String query = """\ +SELECT SomeTable.ID, SomeTable.* +FROM SomeTable +""" + log.debug "query: ${query}" + + try { + log.trace "sql.eachRow()" + sql.eachRow(query) { row -> + log.trace "row: $row" + mdt.dataAdd(row) + } + } finally { + if (sql) sql.close() + } + mdt + } + + } + +} \ No newline at end of file diff --git a/src/main/groovy/example/carnival/micronaut/vine/GraphqlVine.groovy b/src/main/groovy/example/carnival/micronaut/vine/GraphqlVine.groovy new file mode 100644 index 0000000..610bbb8 --- /dev/null +++ b/src/main/groovy/example/carnival/micronaut/vine/GraphqlVine.groovy @@ -0,0 +1,132 @@ +package example.carnival.micronaut.vine + + + +import groovy.transform.ToString +import groovy.transform.EqualsAndHashCode +import groovy.util.logging.Slf4j + +import javax.inject.Singleton +import javax.annotation.PostConstruct +import javax.annotation.PreDestroy +import java.text.DateFormat +import javax.inject.Inject + +import io.micronaut.http.HttpRequest +import io.micronaut.http.HttpResponse +import io.micronaut.http.HttpStatus +import io.micronaut.http.client.RxStreamingHttpClient +import io.micronaut.http.client.annotation.Client +import io.micronaut.context.annotation.Property +import io.reactivex.FlowableSubscriber +import io.reactivex.Flowable + +import org.reactivestreams.Subscription +import org.reactivestreams.Subscriber + +import carnival.util.MappedDataTable +import carnival.core.vine.Vine +import carnival.core.vine.JsonVineMethod +import carnival.core.vine.CacheMode +import example.carnival.micronaut.config.AppConfig + + + +@ToString(includeNames=true) +@Slf4j +@Singleton +class GraphqlVine implements Vine { + + + + /////////////////////////////////////////////////////////////////////////// + // SHARED CLASSES + /////////////////////////////////////////////////////////////////////////// + + @ToString(includeNames=true) + @EqualsAndHashCode(includes=['query']) + static class QueryRequest { + String query + } + + + @ToString(includeNames=true) + static class QueryResponse { + String account_id + } + + + + /////////////////////////////////////////////////////////////////////////// + // FIELDS + /////////////////////////////////////////////////////////////////////////// + + @Inject AppConfig config + + @Inject @Client("https://api.monday.com/v2") + RxStreamingHttpClient client + + + /////////////////////////////////////////////////////////////////////////// + // CONVENIENCE METHODS + /////////////////////////////////////////////////////////////////////////// + + String getApiToken() { config.monday.api.token } + + + + /////////////////////////////////////////////////////////////////////////// + // Get USERS + /////////////////////////////////////////////////////////////////////////// + + @ToString(includeNames=true) + static class GetUsersResponse extends QueryResponse { + + @ToString(includeNames=true) + static class UserData { + + @ToString(includeNames=true) + static class User { + Integer id + String email + } + + List users + } + UserData data + } + + + @Slf4j + class GetUsers extends JsonVineMethod { + GetUsersResponse fetch(Map args = [:]) { + log.trace "GetUsersResponse args:${args}" + + log.trace "getUsers args:${args}" + + QueryRequest body = new QueryRequest( + query: "query { users() {id email} }" + ) + log.trace "body: ${body}" + + HttpRequest req = HttpRequest.POST('/', body) + req.headers.add("Authorization", apiToken) + + GetUsersResponse response + client.exchange(req, GetUsersResponse.class) + .blockingSubscribe( + { HttpResponse res -> + log.trace "res.status = ${res.status}" + response = res.body() + }, + { Throwable t -> + log.error("getUsers", t) + } + ) + + response + } + } + + +}