pub struct GcpHttpClient { /* private fields */ }Expand description
Authenticated, bounded HTTP client for Google Cloud REST APIs.
Consolidates the pattern shared by every Vertex backend in the
workspace: cached ADC auth headers (honoring CacheableResource
semantics), a redirect-disabled reqwest client with connect and
request timeouts, HTTPS-or-loopback endpoint validation, and bounded
JSON response reads with typed, consumer-branded errors.
§Example
use adk_core::ErrorComponent;
use adk_gcp::{GcpErrorCodes, GcpErrorContext, GcpHttpClient};
use serde_json::json;
const CODES: GcpErrorCodes = GcpErrorCodes {
invalid_input: "memory.vertex.invalid_input",
unauthorized: "memory.vertex.unauthorized",
forbidden: "memory.vertex.forbidden",
not_found: "memory.vertex.not_found",
rate_limited: "memory.vertex.rate_limited",
timeout: "memory.vertex.timeout",
unavailable: "memory.vertex.unavailable",
credentials_unavailable: "memory.vertex.credentials_unavailable",
invalid_response: "memory.vertex.invalid_response",
invalid_request: "memory.vertex.invalid_request",
upstream_error: "memory.vertex.upstream_error",
operation_failed: "memory.vertex.operation_failed",
};
let client = GcpHttpClient::builder(
GcpErrorContext::new(ErrorComponent::Memory, CODES, "vertex memory"),
"https://us-central1-aiplatform.googleapis.com",
)
.build()?;
let request = client
.request(reqwest::Method::POST, "projects/p/locations/l/reasoningEngines/1/memories:generate")
.await?
.json(&json!({ "scope": { "app_name": "app" } }));
let operation = client.send_value(request).await?;Implementations§
Source§impl GcpHttpClient
impl GcpHttpClient
Sourcepub fn builder(
errors: GcpErrorContext,
endpoint: impl Into<String>,
) -> GcpHttpClientBuilder
pub fn builder( errors: GcpErrorContext, endpoint: impl Into<String>, ) -> GcpHttpClientBuilder
Starts building a client for the given error identity and endpoint.
Sourcepub fn errors(&self) -> &GcpErrorContext
pub fn errors(&self) -> &GcpErrorContext
The error identity this client stamps on failures.
Sourcepub fn max_response_bytes(&self) -> usize
pub fn max_response_bytes(&self) -> usize
The configured maximum response size in bytes.
Sourcepub fn with_max_response_bytes(self, max_response_bytes: usize) -> Self
pub fn with_max_response_bytes(self, max_response_bytes: usize) -> Self
Overrides the maximum accepted response size after construction.
Lets consumers expose their own post-construction bound override without rebuilding credentials. The caller is responsible for selecting a bound appropriate for the deployment.
Sourcepub fn url(&self, path: &str) -> Result<Url>
pub fn url(&self, path: &str) -> Result<Url>
Builds the absolute URL for an API path (version prefix applied).
§Errors
Returns an error when the resulting URL cannot be constructed.
Sourcepub async fn request(
&self,
method: Method,
path: &str,
) -> Result<RequestBuilder>
pub async fn request( &self, method: Method, path: &str, ) -> Result<RequestBuilder>
Builds an authorized request for an API path.
§Errors
Returns an error when credential headers cannot be acquired within the configured auth timeout.
Sourcepub async fn apply_auth(
&self,
request: RequestBuilder,
) -> Result<RequestBuilder>
pub async fn apply_auth( &self, request: RequestBuilder, ) -> Result<RequestBuilder>
Applies cached credential headers to a request.
§Errors
Returns an error when credential headers cannot be acquired within the configured auth timeout.
Sourcepub async fn auth_headers(&self) -> Result<HeaderMap>
pub async fn auth_headers(&self) -> Result<HeaderMap>
Acquires credential headers, serving cached headers on NotModified.
§Errors
Returns an error when acquisition times out, the credential source
fails, or the source reports NotModified before any headers were
cached.
Sourcepub async fn send_value(&self, request: RequestBuilder) -> Result<Value>
pub async fn send_value(&self, request: RequestBuilder) -> Result<Value>
Sends a request and parses the response as JSON.
Empty or whitespace-only success bodies parse as an empty object.
§Errors
Returns an error when the request times out, transport fails, the response exceeds the configured size bound, the status is not a success, or the body is not valid JSON.
Sourcepub async fn send_value_counted(
&self,
request: RequestBuilder,
) -> Result<(Value, usize)>
pub async fn send_value_counted( &self, request: RequestBuilder, ) -> Result<(Value, usize)>
Sends a request, returning the parsed JSON and the decoded body size.
The byte count lets paginated callers enforce an aggregate response bound across pages.
§Errors
Returns the same errors as send_value.
Sourcepub async fn send_value_allow_not_found(
&self,
request: RequestBuilder,
) -> Result<Option<Value>>
pub async fn send_value_allow_not_found( &self, request: RequestBuilder, ) -> Result<Option<Value>>
Sends a request, mapping 404 Not Found to Ok(None).
§Errors
Returns the same errors as send_value, except
a 404 status which becomes Ok(None).