Skip to content

ProviderHttp

Namespace: Vercel.AI.ProviderUtils

public sealed class ProviderHttp

Sends JSON and SSE requests with the SDK retry policy.

Inheritance: object

View source

ProviderHttp(HttpClient, RetryPolicy?, TimeSpan?)

Section titled “ProviderHttp(HttpClient, RetryPolicy?, TimeSpan?)”
public ProviderHttp(HttpClient httpClient, RetryPolicy? retry = null, TimeSpan? timeout = null)

Creates a provider HTTP client.

Parameters

Name Type Description
httpClient HttpClient
retry RetryPolicy
timeout TimeSpan?

View source

public static bool IsRetryable(int statusCode)

True for 408, 429, and 500–599.

Parameters

Name Type Description
statusCode int

Returns: bool

View source

public static ApiException MapStatus(int statusCode, string? body)

Maps an HTTP status onto the SDK exception hierarchy.

Parameters

Name Type Description
statusCode int
body string

Returns: ApiException

View source

public static ApiException MapStatus(int statusCode, string message, string? body)

Maps an HTTP status onto the SDK exception hierarchy with a provider-specific <code class=“paramref”>message</code>.

Parameters

Name Type Description
statusCode int
message string
body string

Returns: ApiException

View source

OpenJsonLinesAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, int?, CancellationToken)

Section titled “OpenJsonLinesAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, int?, CancellationToken)”
public Task<IAsyncEnumerable<JsonElement>> OpenJsonLinesAsync(HttpMethod method, Uri uri, string? jsonBody, IReadOnlyDictionary<string, string?>? headers, int? maxLineBytes, CancellationToken cancellationToken)

Sends a request and returns once the response status is known. Failed responses throw here. The returned sequence reads one JSON value per line of the body. Enumerate it once: it disposes the response when it ends. A row above <code class=“paramref”>maxLineBytes</code> UTF-8 bytes throws a DownloadError and cancels the download.

Parameters

Name Type Description
method HttpMethod
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
maxLineBytes int?
cancellationToken CancellationToken

Returns: Task<IAsyncEnumerable<JsonElement>>

View source

OpenSseAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “OpenSseAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<IAsyncEnumerable<string>> OpenSseAsync(Uri uri, string jsonBody, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and returns once the response status is known. A failed response throws here. The returned sequence yields SSE data payloads as they arrive. Enumerate it once: it disposes the response when it ends or the consumer stops.

Parameters

Name Type Description
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<IAsyncEnumerable<string>>

View source

OpenSseWithResourceAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “OpenSseWithResourceAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<(IAsyncEnumerable<string> Events, IDisposable Resource)> OpenSseWithResourceAsync(Uri uri, string jsonBody, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Like CancellationToken), and also returns the response so a caller that never enumerates the events can release it.

Parameters

Name Type Description
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<(IAsyncEnumerable<string> Events, IDisposable Resource)>

View source

SendBytesAsync(HttpMethod, Uri, HttpContent, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “SendBytesAsync(HttpMethod, Uri, HttpContent, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<byte[]> SendBytesAsync(HttpMethod method, Uri uri, HttpContent content, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Sends a request and returns the raw bytes.

Parameters

Name Type Description
method HttpMethod
uri Uri
content HttpContent
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<byte[]>

View source

SendJsonAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “SendJsonAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<JsonDocument> SendJsonAsync(HttpMethod method, Uri uri, string? jsonBody, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and returns the parsed body.

Parameters

Name Type Description
method HttpMethod
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<JsonDocument>

View source

SendJsonLinesAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, int?, CancellationToken)

Section titled “SendJsonLinesAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, int?, CancellationToken)”
public IAsyncEnumerable<JsonElement> SendJsonLinesAsync(HttpMethod method, Uri uri, string? jsonBody, IReadOnlyDictionary<string, string?>? headers, int? maxLineBytes, CancellationToken cancellationToken)

Sends a request and yields one JSON value per line of the response body. A row above <code class=“paramref”>maxLineBytes</code> UTF-8 bytes throws a DownloadError and cancels the download.

Parameters

Name Type Description
method HttpMethod
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
maxLineBytes int?
cancellationToken CancellationToken

Returns: IAsyncEnumerable<JsonElement>

View source

SendJsonStringAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “SendJsonStringAsync(HttpMethod, Uri, string?, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<ProviderTextResponse> SendJsonStringAsync(HttpMethod method, Uri uri, string? jsonBody, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and returns the response body text and headers.

Parameters

Name Type Description
method HttpMethod
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<ProviderTextResponse>

View source

SendSseAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “SendSseAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public IAsyncEnumerable<string> SendSseAsync(Uri uri, string jsonBody, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and yields SSE data payloads.

Parameters

Name Type Description
uri Uri
jsonBody string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: IAsyncEnumerable<string>

View source

Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product