elemental-core 0.3.0

Core library for Elemental, providing common utilities and abstractions used across the project.
Documentation
use async_trait::async_trait;

use crate::auth::accounts::microsoft::{MICROSOFT_AUTHORIZER_ID, MicrosoftAccount};
use crate::auth::error::Result;
use crate::auth::protocol::microsoft::{
    delete_skin, minecraft_profile, profile_textures, refresh_microsoft_session, select_cape,
    upload_skin,
};
use crate::auth::providers::{TypedAccountProvider, TypedTextureProvider};
use crate::auth::texture::{ProfileTextures, SkinModel};
use crate::http::SwappableClient;

#[derive(Debug, Clone, Default)]
pub struct MicrosoftAccountProvider {
    http_client: SwappableClient,
}

impl MicrosoftAccountProvider {
    pub fn new(http_client: SwappableClient) -> Self {
        Self { http_client }
    }
}

#[async_trait]
impl TypedAccountProvider for MicrosoftAccountProvider {
    type Account = MicrosoftAccount;

    fn authorizer_id(&self) -> &'static str {
        MICROSOFT_AUTHORIZER_ID
    }

    async fn refresh(&self, account: &Self::Account) -> Result<Self::Account> {
        let session = refresh_microsoft_session(
            &self.http_client,
            &account.client_id,
            &account.refresh_token,
            account.xuid.clone(),
        )
        .await?;
        Ok(MicrosoftAccount::from_session(&account.client_id, session))
    }
}

#[async_trait]
impl TypedTextureProvider for MicrosoftAccountProvider {
    async fn textures(&self, account: &Self::Account) -> Result<ProfileTextures> {
        profile_textures(minecraft_profile(&self.http_client, &account.access_token).await?)
    }

    async fn upload_skin(
        &self,
        account: &Self::Account,
        image: Vec<u8>,
        model: SkinModel,
    ) -> Result<()> {
        upload_skin(&self.http_client, &account.access_token, image, model).await
    }

    async fn delete_skin(&self, account: &Self::Account) -> Result<()> {
        delete_skin(&self.http_client, &account.access_token).await
    }

    async fn delete_cape(&self, account: &Self::Account) -> Result<()> {
        select_cape(&self.http_client, &account.access_token, None).await
    }

    async fn select_cape(&self, account: &Self::Account, cape_id: Option<String>) -> Result<()> {
        select_cape(&self.http_client, &account.access_token, cape_id).await
    }
}