DEV Community

Cover image for Groogle 4.0.0 (Google DSL)
Jorge
Jorge

Posted on • Updated on

Groogle 4.0.0 (Google DSL)

Groogle is a DSL (Domain Specific Language) oriented to interact with Google Cloud services in an easy way. It provides a concise language so you can create scripts or integrate into your application and consume Google Cloud services as Drive, Sheet or Gmail

In this post series I'll (try) to explain the origin, aim and how to use it with several real examples

Requirement

  • A Google account
  • Java 11+ and Groovy 4.x installed
  • A Google credentials file (more details above)

Example

With Groogle you can create a script, for example, to list all the files in your Drive:

groogle = GroogleBuilder.build {
    withOAuthCredentials {
        applicationName 'test'
        scopes DriveScopes.DRIVE
        usingCredentials "client_secret.json"
        storeCredentials true
    }
}

groogle.with {
    service(DriveService).with {
        findFiles {
            eachFile {
                println "$id = $file.name"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Origin

A few years ago I was fascinated about how easily you can write your own DSL using ApacheGroovy so I started a project called Groogle (Groovy + Google)

I started it using the com.puravida-software.groogle maven coordinates but as PuraVida Software company run out, I've decided to rewrite/improve it under the new es.edn.groogle coordinates

First steps

Before to start you need to have a Google Cloud account and a project created, say QuickStart for example

Create an Oauth 2.0 credentials (service credentials will be covered in another post):

Image description

Image description

Download the .json file but don't share them nor store in your repo. For our examples this file will be called client_secret.json

First script

In the same folder you downloaded the json create a example.groovy file:

import com.google.api.services.sheets.v4.SheetsScopes
import com.google.api.services.drive.DriveScopes
import es.edn.groogle.*

@Grab("es.edn:groogle:4.0.0-rc4")
@GrabConfig(systemClassLoader=true)

groogle = GroogleBuilder.build {
    withOAuthCredentials {
        applicationName 'test'
        scopes DriveScopes.DRIVE, SheetsScopes.SPREADSHEETS
        usingCredentials "client_secret.json"
        storeCredentials true
    }
}

groogle.with {
    service(DriveService).with {
        findFiles {
            eachFile {
                println "$id = $file.name"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

in a terminal console execute:

groovy example.groovy

if all goes well a browser will be opened and you need to indicate which Google account you want to use

Select account

and allow access to the application

Allow access

Now you can close the browser and see how the script was able to iterate over your files

Script console

Next

In following posts we'll see how to create or download files from your Drive, create and modify Sheets or send emails

GitHub logo edn-es / groogle

Groovy Google DSL

Top comments (0)