rust-cnb 0.1.2

[DEPRECATED - use `cnb` crate instead] rust-cnb with Generated API client
Documentation
//! Assets API 客户端

use crate::error::{ApiError, Result};
use reqwest::Client;
use serde_json::Value;
use url::Url;

/// Assets API 客户端
pub struct AssetsClient {
    base_url: String,
    client: Client,
}

impl AssetsClient {
    /// 创建新的 Assets API 客户端
    pub fn new(base_url: String, client: Client) -> Self {
        Self { base_url, client }
    }

    /// 设置认证信息
    pub fn with_auth(self, token: &str) -> Self {
        // 这里可以扩展认证逻辑
        self
    }

    /// 发起一个获取 latest release 附件的请求,返回内容或者 302 到某个地址。Initiate a request to get latest release attachments, returns content or 302 redirect.
    pub async fn get_repo_releases_latest_download_file_name(
        &self,
        repo: String,
        file_name: String,
    ) -> Result<Value> {
        let path = format!("/{}/-/releases/latest/download/{}", repo, file_name);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

                let request = self.client.request(
            reqwest::Method::GET,
            url
        );
        



        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 发起一个上传 files 的请求,返回上传文件的url,请使用 put 发起流式上传。Initiate a request to upload files,returns upload URL.Use PUT to initiate a stream upload.
    pub async fn post_repo_upload_files(
        &self,
        repo: String,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/{}/-/upload/files", repo);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::POST,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 发起一个获取 release 附件的请求,返回内容或者 302 到某个地址。Initiate a request to get release attachments, returns content or 302 redirect.
    pub async fn get_repo_releases_download_tag_filename(
        &self,
        repo: String,
        tag: String,
        filename: String,
        share: Option<bool>,
    ) -> Result<Value> {
        let path = format!("/{}/-/releases/download/{}/{}", repo, tag, filename);
        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
        
        if let Some(value) = share {
            url.query_pairs_mut().append_pair("share", &value.to_string());
        }

                let request = self.client.request(
            reqwest::Method::GET,
            url
        );
        



        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 发起一个上传 imgs 的请求,返回上传文件的url,请使用 put 发起流式上传。Initiate a request to upload images,returns upload URL.Use PUT to initiate a stream upload.
    pub async fn post_repo_upload_imgs(
        &self,
        repo: String,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/{}/-/upload/imgs", repo);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::POST,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 发起一个获取 commits 附件的请求,返回内容或者 302 到某个地址。Get a request to fetch a commit assets and returns the content directly or a 302 redirect to the assets URL.
    pub async fn get_repo_commit_assets_download_commit_id_filename(
        &self,
        repo: String,
        commit_id: String,
        filename: String,
        share: Option<bool>,
    ) -> Result<Value> {
        let path = format!("/{}/-/commit-assets/download/{}/{}", repo, commit_id, filename);
        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
        
        if let Some(value) = share {
            url.query_pairs_mut().append_pair("share", &value.to_string());
        }

                let request = self.client.request(
            reqwest::Method::GET,
            url
        );
        



        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 发起一个上传 release 附件的请求,返回上传文件的url,附件上限是64GiB。请使用 put 发起流式上传。
    pub async fn post_repo_upload_releases_tag_name(
        &self,
        repo: String,
        tag_name: String,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/{}/-/upload/releases/{}", repo, tag_name);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::POST,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 发起一个上传 logo 的请求,返回上传文件的url,请使用 put 发起流式上传。Initiate a request to upload logo,returns upload URL.Use PUT to initiate a stream upload.
    pub async fn post_group_upload_logos(
        &self,
        group: String,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/{}/-/upload/logos", group);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::POST,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

}