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
platformIPlatformAdapterThe platform adapter (Google AI or Vertex AI).
httpClientHttpClientOptional HTTP client.
loggerILoggerOptional logger.
Properties
Options
Gets the serializer options, which must be initialized.
protected JsonSerializerOptions Options { get; }
Property Value
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
parametersIEnumerable<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
jsonstringThe JSON.
Returns
- T
The value (a default instance for an empty body).
Type Parameters
TThe type.
FormatBool(bool?)
Formats a boolean query value (true / false).
protected static string? FormatBool(bool? value)
Parameters
valuebool?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
collectionstringThe 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
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
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
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
fetchPageFunc<string, CancellationToken, Task<TPage>>Fetches a page for a page token.
getItemsFunc<TPage, IEnumerable<TItem>>Gets the items of a page.
getNextPageTokenFunc<TPage, string>Gets the next page token of a page.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TItem>
The items.
Type Parameters
TPageThe page type.
TItemThe 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
contentHttpContentThe content.
cancellationTokenCancellationTokenA cancellation token.
Returns
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
requestHttpRequestMessageThe request. Authorization is added by this method.
completionOptionHttpCompletionOptionWhen the operation should complete.
cancellationTokenCancellationTokenA 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
methodHttpMethodThe HTTP method.
urlstringThe URL.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResponse>
The response.
Type Parameters
TResponseThe 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
methodHttpMethodThe HTTP method.
urlstringThe URL.
bodyTRequestThe request body.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResponse>
The response.
Type Parameters
TRequestThe request body type.
TResponseThe response type.