nle-cloud-sdk 0.1.1

Unofficial asynchronous Rust SDK for the NLECloud (新大陆物联网云平台) IoT platform
Documentation
use crate::client::NleCloudClient;
use crate::error::{Result, SdkError};
use crate::models::{AccountLoginDto, AccountLoginResultDto};
use reqwest::Method;

impl NleCloudClient {
    /// Authenticates a user with account credentials and returns login details including the AccessToken.
    ///
    /// Endpoint: `POST /users/login`
    pub async fn login(&self, req: &AccountLoginDto) -> Result<AccountLoginResultDto> {
        if req.account.trim().is_empty() || req.password.trim().is_empty() {
            return Err(SdkError::InvalidParameter(
                "Account username and password must not be empty".to_string(),
            ));
        }

        let body = serde_json::to_value(req)?;
        self.request_with_envelope(
            Method::POST,
            "users/login",
            &[],
            Some(body),
            None,
            false, // login endpoint does not require prior authentication
        )
        .await
    }

    /// Convenience method to log in directly with username and password.
    pub async fn login_with_credentials(
        &self,
        account: &str,
        password: &str,
        is_remember_me: bool,
    ) -> Result<AccountLoginResultDto> {
        let req = AccountLoginDto {
            account: account.to_string(),
            password: password.to_string(),
            is_remember_me: Some(is_remember_me),
        };
        self.login(&req).await
    }

    /// Queries information for the current active authentication token.
    ///
    /// Endpoint: `POST /users/getcurrenttoken`
    pub async fn get_current_token(
        &self,
        token_override: Option<&str>,
    ) -> Result<serde_json::Value> {
        self.request_with_envelope(
            Method::POST,
            "users/getcurrenttoken",
            &[],
            None,
            token_override,
            true,
        )
        .await
    }
}