# Google Drive

{% hint style="info" %}
The following documentation is for **Google Drive v2.0.1** *(BDK)*.
{% endhint %}

## Overview

Google Drive offers cloud storage and file synchronization with seamless integration across Google Workspace. This integration enables automated file management, sharing workflows, and document organization processes. Streamline file collaboration and maintain synchronized access to important documents.

## Prerequisites

### 1. Required Books

The following Book(s) need to be added to your agent so it can learn and understand the automation procedures defined within them:

* **Google Drive**

#### How to Add the Book(s)

1. Go to **Books** → **All Books**.
2. Search for the name of the book and click on it.
3. Click on <kbd>**Install**</kbd> or <kbd>**Add Connection**</kbd> to add the book to your agent.
4. If adding a connection, you'll be prompted for [**connectivity**](#connectivity) details.

## Connectivity

This section outlines the available methods for connecting to the Book, along with the required configuration details for each.

### Connect using Client Email, Token URI and Private Key

Gets the credentials from the service account keys.

| Label        | Description                                                                     | Type        |
| ------------ | ------------------------------------------------------------------------------- | ----------- |
| Client Email | The client email of the service account registered in the Google Cloud Console. | `text`      |
| Token URI    | The token URI of the Google Cloud Console.                                      | `text`      |
| Private Key  | The private key of the service account registered in the Google Cloud Console.  | `sensitive` |

## Procedures

### to copy an item to a folder

Copy an item to a folder

**Input Concepts**

| Concept                                    | Description                                                                                                        | Type                                                             | Required | Default Value |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- | -------- | ------------- |
| `item`                                     | The item (file or folder) to copy                                                                                  | `google drive file reference` or `google drive folder reference` | Yes      | (no default)  |
| [`folder`](#google-drive-folder-reference) | The folder to copy the item to                                                                                     | `google drive folder reference`                                  | Yes      | (no default)  |
| `conflict behavior`                        | The behavior to use on conflict scenarios. It should be one of the following values: ('fail', 'replace', 'rename') | `text`                                                           | No       | (no default)  |

**Examples**

Copy an item to a folder

```generic
get a folder at "Folder1/Folder2"
use the above as the base folder
copy the uploaded file to the base folder
```

Copy an item to a folder with a conflict behavior

```generic
get a folder at "Folder1/Folder2"
use the above as the testing folder
get a folder at "Folder1/Folder2/Conflictive Folder"
use the above as the conflictive folder
copy the conflictive folder to the testing folder
    the conflict behavior is "rename"
```

### to create a (folder) in another folder

Create a (folder) in another folder

**Input Concepts**

| Concept                                            | Description                                                                                                        | Type                            | Required | Default Value |
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------- | -------- | ------------- |
| [`another folder`](#google-drive-folder-reference) | The folder to create the (folder) in                                                                               | `google drive folder reference` | Yes      | (no default)  |
| `folder name`                                      | The name of the (folder) to create                                                                                 | `text`                          | Yes      | (no default)  |
| `conflict behavior`                                | The behavior to use on conflict scenarios. It should be one of the following values: ('fail', 'replace', 'rename') | `text`                          | No       | (no default)  |

**Output Concepts**

| Concept                                    | Description                              | Type                            |
| ------------------------------------------ | ---------------------------------------- | ------------------------------- |
| [`folder`](#google-drive-folder-reference) | a folder reference to the created folder | `google drive folder reference` |

**Examples**

Create a folder in Google Drive

```generic
get a root folder
use the root folder as the base folder
create a folder in the base folder
    the folder name is "My Test Folder"
```

Create a folder in Google Drive, with 'rename' as the conflict behavior

```generic
get a folder at "Folder1/Folder2"
use the folder as the base folder
create a folder in the base folder
    the folder name is "My Test Folder"
    the conflict behavior is "rename"
```

### to delete an item

Delete an item (file or folder)

**Input Concepts**

| Concept | Description                         | Type                                                             | Required | Default Value |
| ------- | ----------------------------------- | ---------------------------------------------------------------- | -------- | ------------- |
| `item`  | The item (file or folder) to delete | `google drive file reference` or `google drive folder reference` | Yes      | (no default)  |

**Examples**

Delete an item

```generic
get a folder at "Folder1/Folder2"
get the folder's items
use the first item as the file
delete the file
```

### to download a file

Download a file

**Input Concepts**

| Concept                                | Description                                | Type                          | Required | Default Value |
| -------------------------------------- | ------------------------------------------ | ----------------------------- | -------- | ------------- |
| [`file`](#google-drive-file-reference) | The file reference to the file to download | `google drive file reference` | Yes      | (no default)  |

**Output Concepts**

| Concept  | Description              | Type   |
| -------- | ------------------------ | ------ |
| `answer` | the file as an IO object | `file` |

**Examples**

Download a file

```generic
get a folder at "Folder1/Folder2"
get the folder's items
use the first item as the file
download the file
```

### to get a (folder) at a path

Gets a reference to a folder.

Google Drive allows for multiple folders with the same name in a parent folder. For now, as the blueprint interface returns only one reference, it is returning the first item on the returned list. In case there are more than one folder with the same name, an error will be raised. In the future, this may change to a list of FolderReferences.

**Input Concepts**

| Concept | Description            | Type   | Required | Default Value |
| ------- | ---------------------- | ------ | -------- | ------------- |
| `path`  | The path to the folder | `text` | Yes      | (no default)  |

**Output Concepts**

| Concept                                    | Description        | Type                            |
| ------------------------------------------ | ------------------ | ------------------------------- |
| [`folder`](#google-drive-folder-reference) | a folder reference | `google drive folder reference` |

**Examples**

Get a folder at the path 'The Folder/The Subfolder'

```generic
get a folder at "The Folder/The Subfolder"
```

### to get a (root folder)

Gets a reference to the root folder.

**Output Concepts**

| Concept                                         | Description        | Type                            |
| ----------------------------------------------- | ------------------ | ------------------------------- |
| [`root folder`](#google-drive-folder-reference) | a folder reference | `google drive folder reference` |

**Examples**

Get a root folder

```generic
get a root folder
```

### to get some (folder's items)

![FILTER - CAPABLE](https://img.shields.io/static/v1?label=FILTER\&message=CAPABLE\&color=blue)

Lists items from a folder reference

**Input Concepts**

| Concept                                    | Description                                       | Type                            | Required | Default Value |
| ------------------------------------------ | ------------------------------------------------- | ------------------------------- | -------- | ------------- |
| [`folder`](#google-drive-folder-reference) | The folder reference from which to list the items | `google drive folder reference` | Yes      | (no default)  |

**Output Concepts**

| Concept          | Description                                                         | Type                                                             |
| ---------------- | ------------------------------------------------------------------- | ---------------------------------------------------------------- |
| `folder's items` | a list of Items (file or folder) containing the items in the folder | `google drive file reference` or `google drive folder reference` |

**Examples**

List items in a folder

```generic
get a folder at "Folder1/Folder2"
get the folder's items
```

### to move an item to a folder

Move an item to a folder

**Input Concepts**

| Concept                                    | Description                                                                                                        | Type                                                             | Required | Default Value |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- | -------- | ------------- |
| `item`                                     | The item (file or folder) to move                                                                                  | `google drive file reference` or `google drive folder reference` | Yes      | (no default)  |
| [`folder`](#google-drive-folder-reference) | The folder to move the item to                                                                                     | `google drive folder reference`                                  | Yes      | (no default)  |
| `conflict behavior`                        | The behavior to use on conflict scenarios. It should be one of the following values: ('fail', 'replace', 'rename') | `text`                                                           | No       | (no default)  |

**Examples**

Move an item to a folder

```generic
get a folder at "Folder1/Folder2"
get the folder's items
use the first item as the file
get a folder at "Folder1/Folder2/Folder3"
use the above as the new folder
move the file to the new folder
```

Move an item to a folder with a conflict behavior

```generic
get a folder at "Folder1/Folder2"
get the folder's items
use the first item as the file
get a folder at "Folder1/Folder2/Folder3"
use the above as the new folder
move the file to the new folder
    the conflict behavior is "rename"
```

### to rename an item to a name

Rename an item (file or folder) to a given name

**Input Concepts**

| Concept             | Description                                                                                                        | Type                                                             | Required | Default Value |
| ------------------- | ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- | -------- | ------------- |
| `item`              | The item (file or folder) to rename                                                                                | `google drive file reference` or `google drive folder reference` | Yes      | (no default)  |
| `name`              | The new name of the item                                                                                           | `text`                                                           | Yes      | (no default)  |
| `conflict behavior` | The behavior to use on conflict scenarios. It should be one of the following values: ('fail', 'replace', 'rename') | `text`                                                           | No       | (no default)  |

**Examples**

Rename an item to a new name

```generic
get a folder at "Folder1/Folder2"
get the folder's items
use the first item as the file
rename the file to "The new name.jpg"
```

Rename an item to a new name, with 'rename' as the conflict behavior

```generic
get a folder at "Folder1/Folder2"
get the folder's items
use the first item as the file
rename the file to "The new name.jpg"
    the conflict behavior is "rename"
```

### to upload a (file) to a folder

Upload a file to a folder

**Input Concepts**

| Concept                                    | Description                                                                                                        | Type                            | Required | Default Value |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------ | ------------------------------- | -------- | ------------- |
| `file`                                     | The file to upload                                                                                                 | `file`                          | Yes      | (no default)  |
| [`folder`](#google-drive-folder-reference) | The folder to upload the file to                                                                                   | `google drive folder reference` | Yes      | (no default)  |
| `file name`                                | The name of the file to upload                                                                                     | `text`                          | Yes      | (no default)  |
| `conflict behavior`                        | The behavior to use on conflict scenarios. It should be one of the following values: ('fail', 'replace', 'rename') | `text`                          | No       | (no default)  |

**Output Concepts**

| Concept                                | Description                           | Type                          |
| -------------------------------------- | ------------------------------------- | ----------------------------- |
| [`file`](#google-drive-file-reference) | a file reference to the uploaded file | `google drive file reference` |

**Examples**

Upload a file to a folder

```generic
# Upload a file to the Playground
the file
get a folder at "Folder1/Folder2"
upload the file to the folder
    the file name is "uploaded-file.jpg"
```

Upload a file to a folder with a conflict behavior

```generic
# Upload a file to the Playground
the file
get a folder at "Folder1/Folder2"
upload the file to the folder
    the file name is "uploaded-file.jpg"
    the conflict behavior is "rename"
```

## Concepts

### Google drive file reference

Contains all information required to identify a file in Google Drive.

| Field Name                 | Description                   | Type           |
| -------------------------- | ----------------------------- | -------------- |
| `id`                       | The id of the file            | `text`         |
| `file_name`                | The name of the file          | `text`         |
| [`parents`](#list-of-text) | The ids of the parent folders | `list of text` |

### Google drive folder reference

Contains all information required to identify a folder in Google Drive.

| Field Name                 | Description                   | Type           |
| -------------------------- | ----------------------------- | -------------- |
| `id`                       | The id of the folder          | `text`         |
| `folder_name`              | The name of the folder        | `text`         |
| [`parents`](#list-of-text) | The ids of the parent folders | `list of text` |
