# Google API

Every handle of the Google fake: the `google` fixture, the people and objects a test creates on it, and what they read and wait for. How to test a Google app or agent, step by step, is in the [Google guide](https://channelstack.dev/docs/platforms/google).

Each call is shown in every SDK's language, with its required options (the table under it lists them all). Arguments go first; options are TypeScript's trailing object, Python's keyword arguments and Rust's builder setters. The variable names are the ones the calls return elsewhere on these pages, so `user` is a `GoogleUser` handle.

Besides the errors listed with each method, most methods can fail with [`action_requires`](https://channelstack.dev/docs/reference/api/errors#action-requires), [`cross_workspace_credential`](https://channelstack.dev/docs/reference/api/errors#cross-workspace-credential), [`fake_actor_in_live_world`](https://channelstack.dev/docs/reference/api/errors#fake-actor-in-live-world), [`internal`](https://channelstack.dev/docs/reference/api/errors#internal), [`invalid_argument`](https://channelstack.dev/docs/reference/api/errors#invalid-argument), [`live_misconfigured`](https://channelstack.dev/docs/reference/api/errors#live-misconfigured), [`live_not_allowlisted`](https://channelstack.dev/docs/reference/api/errors#live-not-allowlisted), [`not_found`](https://channelstack.dev/docs/reference/api/errors#not-found), [`session_not_found`](https://channelstack.dev/docs/reference/api/errors#session-not-found), [`unauthorized`](https://channelstack.dev/docs/reference/api/errors#unauthorized), [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): a bad request, a closed session, or live mode refusing or misconfigured.

## GoogleFixture

Returned by [`Session.google()`](https://channelstack.dev/docs/reference/api/client#google).

| Field    | Python / Rust | Type        |
| -------- | ------------- | ----------- |
| `app`    | `app`         | `GoogleApp` |
| `apiUrl` | `api_url`     | `string`    |

### createApp()

Make another app: a mailbox of its own, with its own address and credentials (decision 0027), or return the one made with this name and settings. Its events go to CHANNELSTACK_GOOGLE_APP\_\<NAME>\_WEBHOOK_URL.

```ts title="TypeScript"
const app = await google.createApp({ name: "…" });
```

```python title="Python"
app = await google.create_app(name="…")
```

```rust title="Rust"
let app = google.create_app().name("…").await?;
```

| Parameter | Type     | Required |        |
| --------- | -------- | -------- | ------ |
| `name`    | `string` | yes      | option |

**Returns** [`GoogleApp`](https://channelstack.dev/docs/reference/api/google#googleapp).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, each app is a sandbox app set up by hand, and this channel has no second one yet (decision 0027).

### apps()

The mailboxs this session used: the agent's first, then each it got from `createApp`, in the order it first did. Another session's are `not_found` here.

```ts title="TypeScript"
const apps = await google.apps();
```

```python title="Python"
apps = await google.apps()
```

```rust title="Rust"
let apps = google.apps().await?;
```

**Returns** [`GoogleApps`](https://channelstack.dev/docs/reference/api/google#googleapps).

### createUser()

Create a person: a mailbox, at `<name>@example.com` unless a test gives the address.

```ts title="TypeScript"
const user = await google.createUser({ name: "…" });
```

```python title="Python"
user = await google.create_user(name="…")
```

```rust title="Rust"
let user = google.create_user().name("…").await?;
```

| Parameter | Type     | Required |                                                                         |
| --------- | -------- | -------- | ----------------------------------------------------------------------- |
| `name`    | `string` | yes      | option                                                                  |
| `address` | `string` | no       | option                                                                  |
| `token`   | `string` | no       | option; read from `CHANNELSTACK_GOOGLE_USER_{name}_TOKEN` when left out |

**Returns** [`GoogleUser`](https://channelstack.dev/docs/reference/api/google#googleuser).

### users()

The people this session created.

```ts title="TypeScript"
const users = await google.users();
```

```python title="Python"
users = await google.users()
```

```rust title="Rust"
let users = google.users().await?;
```

**Returns** [`GoogleUsers`](https://channelstack.dev/docs/reference/api/google#googleusers).

### threads()

The threads this session's people started, oldest first.

```ts title="TypeScript"
const threads = await google.threads();
```

```python title="Python"
threads = await google.threads()
```

```rust title="Rust"
let threads = google.threads().await?;
```

**Returns** [`GoogleThreads`](https://channelstack.dev/docs/reference/api/google#googlethreads).

### deliveries()

Every push channelstack sent the agent for this session, with every attempt.

```ts title="TypeScript"
const deliveries = await google.deliveries({ type: "…" });
```

```python title="Python"
deliveries = await google.deliveries(type="…")
```

```rust title="Rust"
let deliveries = google.deliveries().r#type("…").await?;
```

| Parameter  | Python / Rust     | Type           | Required |        |
| ---------- | ----------------- | -------------- | -------- | ------ |
| `type`     | `type` · `r#type` | `string`       | no       | option |
| `resource` | `resource`        | `GoogleThread` | no       | option |
| `app`      | `app`             | `GoogleApp`    | no       | option |

**Returns** [`GoogleDeliveries`](https://channelstack.dev/docs/reference/api/google#googledeliveries).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### calls()

The Gmail calls the agent made about this session's mail, by the SDK's method name (`users.messages.send`).

```ts title="TypeScript"
const apiCalls = await google.calls({ operation: "…" });
```

```python title="Python"
api_calls = await google.calls(operation="…")
```

```rust title="Rust"
let api_calls = google.calls().operation("…").await?;
```

| Parameter   | Type             | Required |        |
| ----------- | ---------------- | -------- | ------ |
| `operation` | `string`         | no       | option |
| `error`     | `true \| string` | no       | option |

**Returns** [`GoogleApiCalls`](https://channelstack.dev/docs/reference/api/google#googleapicalls).

**Live:** needs proxy mode: Gmail doesn't report the mailbox's calls; proxy mode sees them. Proxy mode doesn't read the app's calls back live yet ([#24](https://github.com/christian-bromann/channelstack/issues/24)), so for now the call is refused with [`action_requires`](https://channelstack.dev/docs/reference/api/errors#action-requires) and the test is skipped.

### waitForCall()

Waits for the next API call from the agent after the last one this handle returned: it polls `calls()` and never sleeps longer than it must.

```ts title="TypeScript"
const apiCall = await google.waitForCall({ operation: "…" });
```

```python title="Python"
api_call = await google.wait_for_call(operation="…")
```

```rust title="Rust"
let api_call = google.wait_for_call().operation("…").await?;
```

| Parameter   | Type             | Required |             |
| ----------- | ---------------- | -------- | ----------- |
| `operation` | `string`         | no       | filter      |
| `error`     | `true \| string` | no       | filter      |
| `timeout`   | `Duration`       | no       | default 10s |

**Returns** [`GoogleApiCall`](https://channelstack.dev/docs/reference/api/google#googleapicall). When the time is up, it fails with what it was waiting for.

## GoogleApp

Returned by [`GoogleFixture.createApp()`](https://channelstack.dev/docs/reference/api/google#createapp).

| Field          | Python / Rust   | Type       |
| -------------- | --------------- | ---------- |
| `id`           | `id`            | `string`   |
| `channel`      | `channel`       | `"google"` |
| `kind`         | `kind`          | `"app"`    |
| `name`         | `name`          | `string`   |
| `botUserId`    | `bot_user_id`   | `string`   |
| `address`      | `address`       | `string`   |
| `clientId`     | `client_id`     | `string`   |
| `clientSecret` | `client_secret` | `string`   |
| `refreshToken` | `refresh_token` | `string`   |

### send()

An app a test made sends an email, through its own mailbox's API, as its own call would.

```ts title="TypeScript"
const email = await app.send({ to, subject: "…" });
```

```python title="Python"
email = await app.send(to=to, subject="…")
```

```rust title="Rust"
let email = app.send().to(to).subject("…").await?;
```

| Parameter | Type                             | Required |        |
| --------- | -------------------------------- | -------- | ------ |
| `to`      | `Array<GoogleUser \| GoogleApp>` | yes      | option |
| `cc`      | `Array<GoogleUser \| GoogleApp>` | no       | option |
| `subject` | `string`                         | yes      | option |
| `text`    | `string`                         | no       | option |
| `html`    | `string`                         | no       | option |
| `headers` | `Record<string, string>`         | no       | option |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, each app is a sandbox app set up by hand, and this channel has no second one yet (decision 0027).

### reply()

An app a test made replies to an email, in its thread, as its own call would.

```ts title="TypeScript"
const result = await app.reply(email, { text: "…" });
```

```python title="Python"
result = await app.reply(email, text="…")
```

```rust title="Rust"
let result = app.reply(&email).text("…").await?;
```

| Parameter | Type                     | Required |            |
| --------- | ------------------------ | -------- | ---------- |
| `email`   | `GoogleEmail`            | yes      | positional |
| `text`    | `string`                 | no       | option     |
| `html`    | `string`                 | no       | option     |
| `all`     | `boolean`                | no       | option     |
| `headers` | `Record<string, string>` | no       | option     |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, each app is a sandbox app set up by hand, and this channel has no second one yet (decision 0027).

## GoogleEmail

Returned by [`GoogleApp.send()`](https://channelstack.dev/docs/reference/api/google#send), [`GoogleApp.reply()`](https://channelstack.dev/docs/reference/api/google#reply), [`GoogleEmail.refresh()`](https://channelstack.dev/docs/reference/api/google#refresh), [`GoogleUser.send()`](https://channelstack.dev/docs/reference/api/google#send-1).

| Field       | Python / Rust    | Type                          |
| ----------- | ---------------- | ----------------------------- |
| `id`        | `id`             | `string`                      |
| `channel`   | `channel`        | `"google"`                    |
| `thread`    | `thread`         | `GoogleThread`                |
| `author`    | `author`         | `GooglePrincipal`             |
| `from`      | `from_` · `from` | `string`                      |
| `to`        | `to`             | `string[]`                    |
| `cc`        | `cc`             | `string[]`                    |
| `subject`   | `subject`        | `string`                      |
| `text`      | `text`           | `string`                      |
| `at`        | `at`             | `string`                      |
| `messageId` | `message_id`     | `string`                      |
| `headers`   | `headers`        | `Record<string, string>`      |
| `labels`    | `labels`         | `string[]`                    |
| `delivery`  | `delivery`       | `GoogleDelivery \| undefined` |

### refresh()

Re-read an email.

```ts title="TypeScript"
const updated = await email.refresh();
```

```python title="Python"
updated = await email.refresh()
```

```rust title="Rust"
let updated = email.refresh().await?;
```

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail).

### deliveryFor()

The delivery that told this app of the email (decision 0027), or nothing when it wasn't told.

```ts title="TypeScript"
const delivery = await email.deliveryFor(app);
```

```python title="Python"
delivery = await email.delivery_for(app)
```

```rust title="Rust"
let delivery = email.delivery_for(&app).await?;
```

| Parameter | Type        | Required |            |
| --------- | ----------- | -------- | ---------- |
| `app`     | `GoogleApp` | yes      | positional |

**Returns** [`GoogleDelivery | undefined`](https://channelstack.dev/docs/reference/api/google#googledelivery).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

## GoogleThread

| Field     | Type       |
| --------- | ---------- |
| `id`      | `string`   |
| `channel` | `"google"` |
| `subject` | `string`   |

### messages()

Every email in the thread, oldest first.

```ts title="TypeScript"
const messages = await thread.messages();
```

```python title="Python"
messages = await thread.messages()
```

```rust title="Rust"
let messages = thread.messages().await?;
```

**Returns** [`GoogleMessages`](https://channelstack.dev/docs/reference/api/google#googlemessages).

### replies()

The thread's answers: every email after the first, oldest first.

```ts title="TypeScript"
const replies = await thread.replies();
```

```python title="Python"
replies = await thread.replies()
```

```rust title="Rust"
let replies = thread.replies().await?;
```

**Returns** [`GoogleReplies`](https://channelstack.dev/docs/reference/api/google#googlereplies).

### transcript()

The thread as a person reads it: each email's own words, without the quoted history.

```ts title="TypeScript"
const transcript = await thread.transcript();
```

```python title="Python"
transcript = await thread.transcript()
```

```rust title="Rust"
let transcript = thread.transcript().await?;
```

**Returns** [`GoogleTranscript`](https://channelstack.dev/docs/reference/api/google#googletranscript).

### waitForReply()

Waits for the next reply after the last one this handle returned: it polls `replies()` and never sleeps longer than it must.

```ts title="TypeScript"
const email = await thread.waitForReply({ from: "my-agent" });
```

```python title="Python"
email = await thread.wait_for_reply(from_="my-agent")
```

```rust title="Rust"
let email = thread.wait_for_reply().from("my-agent").await?;
```

| Parameter | Python / Rust    | Type                        | Required |                     |
| --------- | ---------------- | --------------------------- | -------- | ------------------- |
| `from`    | `from_` · `from` | `GooglePrincipal \| string` | no       | a handle, or a name |
| `timeout` | `timeout`        | `Duration`                  | no       | default 10s         |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail). When the time is up, it fails with what it was waiting for.

### latestReply()

The latest reply, once the app is quiet. Waits for at least one: it polls `replies()` and never sleeps longer than it must.

```ts title="TypeScript"
const email = await thread.latestReply({ from: "my-agent" });
```

```python title="Python"
email = await thread.latest_reply(from_="my-agent")
```

```rust title="Rust"
let email = thread.latest_reply().from("my-agent").await?;
```

| Parameter  | Python / Rust    | Type                        | Required |                     |
| ---------- | ---------------- | --------------------------- | -------- | ------------------- |
| `from`     | `from_` · `from` | `GooglePrincipal \| string` | no       | a handle, or a name |
| `timeout`  | `timeout`        | `Duration`                  | no       | default 10s         |
| `quietFor` | `quiet_for`      | `Duration`                  | no       | default 750ms       |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail). When the time is up, it fails with what it was waiting for.

## GoogleDelivery

Returned by [`GoogleEmail.deliveryFor()`](https://channelstack.dev/docs/reference/api/google#deliveryfor), [`GoogleDelivery.refresh()`](https://channelstack.dev/docs/reference/api/google#refresh-1).

| Field          | Python / Rust     | Type                                                                      |
| -------------- | ----------------- | ------------------------------------------------------------------------- |
| `id`           | `id`              | `string`                                                                  |
| `channel`      | `channel`         | `"google"`                                                                |
| `app`          | `app`             | `string`                                                                  |
| `type`         | `type` · `r#type` | `string`                                                                  |
| `emailAddress` | `email_address`   | `string`                                                                  |
| `historyId`    | `history_id`      | `string`                                                                  |
| `payload`      | `payload`         | `unknown \| undefined`                                                    |
| `attempts`     | `attempts`        | `DeliveryAttempt[]`                                                       |
| `status`       | `status`          | `"pending" \| "acknowledged" \| "failed" \| "timed_out" \| "unreachable"` |

### refresh()

Re-read a delivery: its attempts and status.

```ts title="TypeScript"
const updated = await delivery.refresh();
```

```python title="Python"
updated = await delivery.refresh()
```

```rust title="Rust"
let updated = delivery.refresh().await?;
```

**Returns** [`GoogleDelivery`](https://channelstack.dev/docs/reference/api/google#googledelivery).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### redeliver()

Push the same message again, as Pub/Sub does when a push isn't acknowledged: the same message id.

```ts title="TypeScript"
const deliveryAttempt = await delivery.redeliver();
```

```python title="Python"
delivery_attempt = await delivery.redeliver()
```

```rust title="Rust"
let delivery_attempt = delivery.redeliver().await?;
```

**Returns** [`DeliveryAttempt`](https://channelstack.dev/docs/reference/api/client#deliveryattempt).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Pub/Sub has no API to push an acknowledged message again.

### resend()

Push it again, forged: with a token signed by another key, one that has expired, or one for another audience than the push endpoint.

```ts title="TypeScript"
const deliveryAttempt = await delivery.resend({ badSignature });
```

```python title="Python"
delivery_attempt = await delivery.resend(bad_signature=bad_signature)
```

```rust title="Rust"
let delivery_attempt = delivery.resend().bad_signature(bad_signature).await?;
```

| Parameter       | Python / Rust    | Type      | Required |        |
| --------------- | ---------------- | --------- | -------- | ------ |
| `badSignature`  | `bad_signature`  | `boolean` | no       | option |
| `stale`         | `stale`          | `boolean` | no       | option |
| `wrongAudience` | `wrong_audience` | `boolean` | no       | option |

**Returns** [`DeliveryAttempt`](https://channelstack.dev/docs/reference/api/client#deliveryattempt).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### settled()

Waits for the delivery to settle: it polls `refresh()` and never sleeps longer than it must.

```ts title="TypeScript"
const updated = await delivery.settled();
```

```python title="Python"
updated = await delivery.settled()
```

```rust title="Rust"
let updated = delivery.settled().await?;
```

| Parameter | Type       | Required |             |
| --------- | ---------- | -------- | ----------- |
| `timeout` | `Duration` | no       | default 10s |

**Returns** [`GoogleDelivery`](https://channelstack.dev/docs/reference/api/google#googledelivery). When the time is up, it fails with what it was waiting for.

## GoogleUser

Returned by [`GoogleFixture.createUser()`](https://channelstack.dev/docs/reference/api/google#createuser).

| Field     | Type       |
| --------- | ---------- |
| `id`      | `string`   |
| `channel` | `"google"` |
| `kind`    | `"user"`   |
| `name`    | `string`   |
| `address` | `string`   |

### send()

The person emails someone: the agent (`google.app`), another app, or another person.

```ts title="TypeScript"
const email = await user.send({ to, subject: "…" });
```

```python title="Python"
email = await user.send(to=to, subject="…")
```

```rust title="Rust"
let email = user.send().to(to).subject("…").await?;
```

| Parameter | Type                             | Required |        |
| --------- | -------------------------------- | -------- | ------ |
| `to`      | `Array<GoogleUser \| GoogleApp>` | yes      | option |
| `cc`      | `Array<GoogleUser \| GoogleApp>` | no       | option |
| `subject` | `string`                         | yes      | option |
| `text`    | `string`                         | no       | option |
| `html`    | `string`                         | no       | option |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail).

### reply()

The person replies to an email, in its thread: `Re:` its subject, with `In-Reply-To` and `References`.

```ts title="TypeScript"
const result = await user.reply(email, { text: "…" });
```

```python title="Python"
result = await user.reply(email, text="…")
```

```rust title="Rust"
let result = user.reply(&email).text("…").await?;
```

| Parameter | Type          | Required |            |
| --------- | ------------- | -------- | ---------- |
| `email`   | `GoogleEmail` | yes      | positional |
| `text`    | `string`      | no       | option     |
| `html`    | `string`      | no       | option     |
| `all`     | `boolean`     | no       | option     |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail).

### inbox()

What reached the person's mailbox, oldest first.

```ts title="TypeScript"
const inbox = await user.inbox();
```

```python title="Python"
inbox = await user.inbox()
```

```rust title="Rust"
let inbox = user.inbox().await?;
```

**Returns** [`GoogleInbox`](https://channelstack.dev/docs/reference/api/google#googleinbox).

### waitForEmail()

Waits for the next email after the last one this handle returned: it polls `inbox()` and never sleeps longer than it must.

```ts title="TypeScript"
const email = await user.waitForEmail({ from: "my-agent" });
```

```python title="Python"
email = await user.wait_for_email(from_="my-agent")
```

```rust title="Rust"
let email = user.wait_for_email().from("my-agent").await?;
```

| Parameter | Python / Rust    | Type                        | Required |                     |
| --------- | ---------------- | --------------------------- | -------- | ------------------- |
| `from`    | `from_` · `from` | `GooglePrincipal \| string` | no       | a handle, or a name |
| `timeout` | `timeout`        | `Duration`                  | no       | default 10s         |

**Returns** [`GoogleEmail`](https://channelstack.dev/docs/reference/api/google#googleemail). When the time is up, it fails with what it was waiting for.

## Types

Plain values the handles return and take.

### GoogleApps

```ts
type GoogleApps = GoogleApp[];
```

### GooglePrincipal

| Field  | Type              |
| ------ | ----------------- |
| `id`   | `string`          |
| `name` | `string`          |
| `kind` | `"user" \| "app"` |

### GoogleUsers

```ts
type GoogleUsers = GoogleUser[];
```

### GoogleThreads

```ts
type GoogleThreads = GoogleThread[];
```

### GoogleInbox

```ts
type GoogleInbox = GoogleEmail[];
```

### GoogleMessages

```ts
type GoogleMessages = GoogleEmail[];
```

### GoogleReplies

```ts
type GoogleReplies = GoogleEmail[];
```

### GoogleTranscript

| Field     | Type                      |
| --------- | ------------------------- |
| `entries` | `GoogleTranscriptEntry[]` |
| `text`    | `string`                  |

### GoogleTranscriptEntry

| Field      | Type              |
| ---------- | ----------------- |
| `id`       | `string`          |
| `author`   | `GooglePrincipal` |
| `at`       | `string`          |
| `text`     | `string`          |
| `controls` | `string[]`        |
| `edited`   | `boolean`         |
| `depth`    | `number`          |

### GoogleDeliveries

```ts
type GoogleDeliveries = GoogleDelivery[];
```

### GoogleApiCalls

```ts
type GoogleApiCalls = GoogleApiCall[];
```

### GoogleApiCall

| Field            | Python / Rust     | Type                   |
| ---------------- | ----------------- | ---------------------- |
| `id`             | `id`              | `string`               |
| `channel`        | `channel`         | `"google"`             |
| `operation`      | `operation`       | `string`               |
| `method`         | `method`          | `string`               |
| `path`           | `path`            | `string`               |
| `at`             | `at`              | `string`               |
| `durationMs`     | `duration_ms`     | `number`               |
| `request`        | `request`         | `unknown \| undefined` |
| `responseStatus` | `response_status` | `number`               |
| `response`       | `response`        | `unknown \| undefined` |
| `error`          | `error`           | `string \| undefined`  |
