Table of Contents

Class AgentApiClientBase

Namespace
GenerativeAI.Clients
Assembly
GenerativeAI.dll

Shared plumbing of the clients of the agent-related Gemini API resources (Voices, Agents, Environments, Triggers, Credentials and Decisions), which — like the Interactions API — use snake_case JSON and live directly under the API version ({base}/{version}/{collection}, or .../projects/{project}/locations/{location}/{collection} on Vertex AI).

public abstract class AgentApiClientBase : BaseClient
Inheritance
AgentApiClientBase
Derived
Inherited Members
Extension Methods

Constructors

AgentApiClientBase(IPlatformAdapter, HttpClient?, ILogger?)

Initializes a new instance of the AgentApiClientBase class.

protected AgentApiClientBase(IPlatformAdapter platform, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

platform IPlatformAdapter

The platform adapter (Google AI or Vertex AI).

httpClient HttpClient

Optional HTTP client.

logger ILogger

Optional logger.

Properties

Options

Gets the serializer options, which must be initialized.

protected JsonSerializerOptions Options { get; }

Property Value

JsonSerializerOptions

Methods

BuildQuery(IEnumerable<KeyValuePair<string, string?>>)

Builds a query string (?a=1&b=2) from name/value pairs, skipping null values. Values are URL-encoded; repeated names are allowed (form style, exploded arrays).

protected static string BuildQuery(IEnumerable<KeyValuePair<string, string?>> parameters)

Parameters

parameters IEnumerable<KeyValuePair<string, string>>

The parameters.

Returns

string

The query string, or an empty string.

DeserializeJson<T>(string)

Deserializes json with the source-generated metadata of T.

protected T DeserializeJson<T>(string json) where T : new()

Parameters

json string

The JSON.

Returns

T

The value (a default instance for an empty body).

Type Parameters

T

The type.

FormatBool(bool?)

Formats a boolean query value (true / false).

protected static string? FormatBool(bool? value)

Parameters

value bool?

The value.

Returns

string

The formatted value, or null.

GetCollectionUrl(string)

Gets the URL of a collection, e.g. https://generativelanguage.googleapis.com/v1beta/voices.

protected string GetCollectionUrl(string collection)

Parameters

collection string

The collection name.

Returns

string

The collection URL.

GetResourceUrl(string, string)

Gets the URL of a resource in a collection. A {collection}/ prefix on id is accepted.

protected string GetResourceUrl(string collection, string id)

Parameters

collection string

The collection name.

id string

The resource ID.

Returns

string

The resource URL.

NormalizeId(string, string)

Removes an optional {collection}/ prefix from id and validates it.

protected static string NormalizeId(string collection, string id)

Parameters

collection string

The collection name.

id string

The resource ID.

Returns

string

The bare ID.

PageParameters(int?, string?)

Creates the page_size / page_token query parameters.

protected static List<KeyValuePair<string, string?>> PageParameters(int? pageSize, string? pageToken)

Parameters

pageSize int?

The page size.

pageToken string

The page token.

Returns

List<KeyValuePair<string, string>>

The parameters.

PaginateAsync<TPage, TItem>(Func<string?, CancellationToken, Task<TPage>>, Func<TPage, IEnumerable<TItem>?>, Func<TPage, string?>, CancellationToken)

Enumerates all items of a paged list, following next_page_token.

protected static IAsyncEnumerable<TItem> PaginateAsync<TPage, TItem>(Func<string?, CancellationToken, Task<TPage>> fetchPage, Func<TPage, IEnumerable<TItem>?> getItems, Func<TPage, string?> getNextPageToken, CancellationToken cancellationToken = default)

Parameters

fetchPage Func<string, CancellationToken, Task<TPage>>

Fetches a page for a page token.

getItems Func<TPage, IEnumerable<TItem>>

Gets the items of a page.

getNextPageToken Func<TPage, string>

Gets the next page token of a page.

cancellationToken CancellationToken

A cancellation token.

Returns

IAsyncEnumerable<TItem>

The items.

Type Parameters

TPage

The page type.

TItem

The item type.

ReadContentAsStringAsync(HttpContent, CancellationToken)

Reads an HTTP content as a string, honoring cancellationToken where supported.

protected static Task<string> ReadContentAsStringAsync(HttpContent content, CancellationToken cancellationToken)

Parameters

content HttpContent

The content.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<string>

The content as a string.

SendRawAsync(HttpRequestMessage, HttpCompletionOption, CancellationToken)

Sends a request and returns the raw (successful) response; the caller disposes it.

protected Task<HttpResponseMessage> SendRawAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)

Parameters

request HttpRequestMessage

The request. Authorization is added by this method.

completionOption HttpCompletionOption

When the operation should complete.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<HttpResponseMessage>

The response.

SendRequestAsync<TResponse>(HttpMethod, string, CancellationToken)

Sends a request without a body and deserializes the JSON response.

protected Task<TResponse> SendRequestAsync<TResponse>(HttpMethod method, string url, CancellationToken cancellationToken)

Parameters

method HttpMethod

The HTTP method.

url string

The URL.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResponse>

The response.

Type Parameters

TResponse

The response type.

SendRequestAsync<TRequest, TResponse>(HttpMethod, string, TRequest, CancellationToken)

Sends a request with a JSON body and deserializes the JSON response.

protected Task<TResponse> SendRequestAsync<TRequest, TResponse>(HttpMethod method, string url, TRequest body, CancellationToken cancellationToken)

Parameters

method HttpMethod

The HTTP method.

url string

The URL.

body TRequest

The request body.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResponse>

The response.

Type Parameters

TRequest

The request body type.

TResponse

The response type.