# Migrating from the Drive API

This document outlines how to migrate code using the Drive API for permission
management to the Data Studio API. For common Drive API endpoints, it shows
the corresponding Data Studio API code.

> [!NOTE]
> **Note:** There are no client libraries for using the Data Studio API. To make these calls, use an appropriate HTTP library for your language.

## Files

For the Drive API files endpoints, the Data Studio API only has an equivalent
endpoint for the `Files: list` endpoint.

### List

| API | Method | Endpoint |
|---|---|---|
| Drive | `POST` | `/drive/v3/files` |
| Data Studio | `GET` | `/v1/assets:search` |

Comparison:

**Drive**

```javascript
const oAuthToken = '123' // This should be replaced with a valid OAuth token.
fetch(`https://www.googleapis.com/drive/v3/files`, {
  headers: {
    Authorization: `Bearer ${oAuthToken}`
  },
  method: "POST",
})
```

**Data Studio**

```javascript
const oAuthToken = '123' // This should be replaced with a valid OAuth token.
fetch(`https://datastudio.googleapis.com/v1/assets:search?assetTypes={ASSET_TYPE}`, {
  headers: {
    Authorization: `Bearer ${oAuthToken}`
  }
})
```

See [search assets](https://developers.google.com/data-studio/integrate/api/reference/assets/search.md.txt).

## Permissions

### Create, Delete, And Get

| API | Method | Endpoint |
|---|---|---|
| Drive | `POST` | `/drive/v3/files/fileId/permissions` |
| Drive | `DELETE` | `/drive/v3/files/fileId/permissions/permissionId` |
| Drive | `GET` | `/drive/v3/files/fileId/permissions/permissionId` |

There are no corresponding endpoints in the Data Studio API for managing
multiple `Permissions` objects. There is only one permissions object for a
Data Studio asset, and it always exists.

- To remove someone from an asset, see [revoke all permissions](https://developers.google.com/data-studio/integrate/api/reference/permissions/revokeAllPermissions.md.txt)
- To add someone to an asset, see [add members](https://developers.google.com/data-studio/integrate/api/reference/permissions/addMembers.md.txt)
- To view a permissions object for an asset, see [get permissions](https://developers.google.com/data-studio/integrate/api/reference/permissions/get.md.txt)

### List

There isn't a 1-to-1 match between Drive and Data Studio, but the endpoints
serve similar goals. The main difference is that a Drive file can have many
permissions objects, and Data Studio has exactly one.

| API | Method | Endpoint |
|---|---|---|
| Drive | `GET` | `/drive/v3/files/fileId/permissions` |
| Data Studio | `GET` | `/v1/assets/assetId/permissions` |

Comparison:

**Drive**

This following code lists all the permissions objects for the Drive API.
Depending on your code, you may call this method multiple times using pagination
tokens (as shown) to ensure you can see all of the permissions that are set for
a file.

```javascript
const fileId = '123'; // This should be replaced with a valid Drive ID.
const oAuthToken = '123'; // This should be replaced with a valid OAuth token.
let nextPageToken = undefined;
let permissions = [];
do {
  const permissionsData = await fetch(`https://www.googleapis.com/drive/v3/files/${fileId}/permissions`, {
    headers: {
      Authorization: `Bearer ${oAuthToken}`
    }
  });
  nextPageToken = permissionsData.nextPageToken;
  permissions = permissions.concat(permissionsData.permissions)
} while (nextPageToken !== undefined);
```

**Data Studio**

Since there is only one permission object for a Data Studio asset, you don't
have to account for pagination.

```javascript
const oAuthToken = '123' // This should be replaced with a valid OAuth token.
const assetId = '123' // This should be replaced with a valid asset ID.
fetch(`https://datastudio.googleapis.com/v1/assets/{ASSET_ID}/permissions`, {
  headers: {
    Authorization: `Bearer ${oAuthToken}`
  }
}
```

See [get permissions](https://developers.google.com/data-studio/integrate/api/reference/permissions/get.md.txt).

## Update

For updating permissions, the Data Studio and Drive APIs have very similar
functionality. The main difference is you cannot set an `expirationTime` on a
Data Studio permission.

| API | Method | Endpoint |
|---|---|---|
| Drive | `PATCH` | `/drive/v3/files/fileId/permissions/permissionId` |
| Data Studio | `PATCH` | `/v1/assets/assetId/permissions` |

Comparison:

**Drive**

```javascript
const fileId = '123'; // This should be replaced with a valid Drive ID.
const oAuthToken = '123'; // This should be replaced with a valid OAuth token.
const newPermissionsObject = {
  expirationTime: '...',
  role: 'owner', // Or any other option
}
fetch(`https://www.googleapis.com/drive/v3/files/${fileId}/permissions/permissionId`, {
  headers: {
    Authorization: `Bearer ${oAuthToken}`
  },
  method: "PATCH",
  body: JSON.stringify(newPermissionsObject)
})
```

**Data Studio**

```javascript
const oAuthToken = '123' // This should be replaced with a valid OAuth token.
const assetId = '123' // This should be replaced with a valid asset ID.
const newPermissionsObject = {
  permissions: {
    //...
  }
}

fetch(`https://datastudio.googleapis.com/v1/assets/${assetId}/permissions`, {
  headers: {
    Authorization: `Bearer ${oAuthToken}`
  },
  method: "PATCH",
  body: JSON.stringify({
    name: assetId,
    permissions: newPermissionsObject
  })
})
```

For use-case specific alternatives, see:

- [add members](https://developers.google.com/data-studio/integrate/api/reference/permissions/addMembers.md.txt)
- [revoke all permissions](https://developers.google.com/data-studio/integrate/api/reference/permissions/revokeAllPermissions.md.txt)