bedrock-agentcore 0.1.0

Amazon Bedrock AgentCore SDK for Rust
Documentation
use std::fmt;

use aws_sdk_bedrockagentcore::Client;

use super::{check_provider_name, resolve_workload_access_token, secret, service_error};
use crate::{
    Error, ErrorKind,
    context::{self, WorkloadAccessToken},
};

secret! {
    /// An API key from a credential provider of AgentCore Identity.
    ApiKey
}

/// A request for the API key of one credential provider. Create one with
/// [`IdentityClient::api_key`](super::IdentityClient::api_key).
#[derive(Clone)]
#[must_use = "the request is made when `send` is called and awaited"]
pub struct ApiKeyBuilder {
    client: Client,
    provider: String,
    workload_access_token: Option<WorkloadAccessToken>,
}

impl ApiKeyBuilder {
    pub(super) fn new(client: Client, provider: String) -> Self {
        Self { client, provider, workload_access_token: None }
    }

    /// Sets the workload access token of this request.
    ///
    /// Without this setter the request uses the token of the ambient request context.
    /// A token set here is used as given: when it is empty the request fails, even
    /// inside a context that has a token.
    pub fn workload_access_token(mut self, token: WorkloadAccessToken) -> Self {
        self.workload_access_token = Some(token);
        self
    }

    /// Requests the API key with `GetResourceApiKey`.
    ///
    /// The workload access token is read when the returned future is first polled, so
    /// the ambient context that counts is the one the future runs in.
    ///
    /// # Errors
    ///
    /// - [`ErrorKind::InvalidConfig`] when the provider name is not 1 to 128 letters,
    ///   digits, hyphens and underscores. No request is made.
    /// - [`ErrorKind::MissingWorkloadToken`] when there is no nonempty token. No
    ///   request is made.
    /// - [`ErrorKind::Service`] when the call fails. Its text has the provider, the
    ///   error code of the service, the HTTP status and the request ID, and its
    ///   source is the `SdkError` of the generated operation.
    /// - [`ErrorKind::NoApiKey`] when the call succeeds without an API key.
    pub async fn send(self) -> Result<ApiKey, Error> {
        let Self { client, provider, workload_access_token } = self;
        check_provider_name(&provider)?;
        let token = resolve_workload_access_token(workload_access_token, context::current().as_ref())?;
        let output = client
            .get_resource_api_key()
            .resource_credential_provider_name(&provider)
            .workload_identity_token(token.expose())
            .send()
            .await
            .map_err(|error| service_error("GetResourceApiKey", &provider, error))?;
        if output.api_key.is_empty() {
            return Err(Error::new(ErrorKind::NoApiKey, format!("No API key returned for provider: {provider}")));
        }
        Ok(ApiKey::new(output.api_key))
    }
}

impl fmt::Debug for ApiKeyBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ApiKeyBuilder")
            .field("provider", &self.provider)
            .field("workload_access_token", &self.workload_access_token)
            .finish_non_exhaustive()
    }
}