Groogle 4.0.0 (Google DSL)
Tags: google, googlecloud, groovy, dsl
Published: 2024-07-11 14:00:49

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"
            }
        }
    }
}
```

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyv3nvx16j574k884a54.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5qlgt48duwxgznh63h0.png)

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"
            }
        }
    }
}
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wl1k1kjl22dkit2ghzfz.png)

and allow access to the application 


![Allow access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01cj5vk58mnks9wc1iwa.png)

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


![Script console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2snq47fbrcf1l68ujrp7.png)


## Next

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