ve-tos-rust-sdk 2.0.0

volcengine offical tos rust sdk
Documentation
/*
 * Copyright (2024) Volcengine
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use async_trait::async_trait;

use crate::asynchronous::http::HttpResponse;
use crate::asynchronous::internal::{OutputParser, parse_json};
use crate::bucket::{CreateBucketInput, CreateBucketOutput, DeleteBucketInput, DeleteBucketOutput, HeadBucketInput, HeadBucketOutput, ListBucketsInput, ListBucketsOutput};
use crate::common::{Meta, RequestInfo};
use crate::constant::{HEADER_AZ_REDUNDANCY, HEADER_BUCKET_REGION, HEADER_LOCATION, HEADER_PROJECT_NAME, HEADER_STORAGE_CLASS};
use crate::enumeration::{AzRedundancyType, StorageClassType};
use crate::error::TosError;
use crate::http::HttpRequest;
use crate::internal::{get_header_value, get_header_value_str};

#[async_trait]
pub trait BucketAPI {
    async fn create_bucket(&self, input: &CreateBucketInput) -> Result<CreateBucketOutput, TosError>;
    async fn head_bucket(&self, input: &HeadBucketInput) -> Result<HeadBucketOutput, TosError>;
    async fn delete_bucket(&self, input: &DeleteBucketInput) -> Result<DeleteBucketOutput, TosError>;
    async fn list_buckets(&self, input: &ListBucketsInput) -> Result<ListBucketsOutput, TosError>;
}

#[async_trait]
impl OutputParser for ListBucketsOutput {
    async fn parse<B>(_: HttpRequest<'_, B>, response: HttpResponse, request_info: RequestInfo, _: Meta) -> Result<Self, TosError> where B: Send {
        let mut result = parse_json::<Self>(response).await?;
        result.request_info = request_info;
        Ok(result)
    }
}

#[async_trait]
impl OutputParser for CreateBucketOutput {
    async fn parse<B>(_: HttpRequest<'_, B>, response: HttpResponse, request_info: RequestInfo, _: Meta) -> Result<Self, TosError> where B: Send {
        let location = get_header_value(response.headers(), HEADER_LOCATION);
        Ok(Self { request_info, location })
    }
}

#[async_trait]
impl OutputParser for HeadBucketOutput {
    async fn parse<B>(_: HttpRequest<'_, B>, response: HttpResponse, request_info: RequestInfo, _: Meta) -> Result<Self, TosError> where B: Send {
        let region = get_header_value(response.headers(), HEADER_BUCKET_REGION);
        let storage_class = StorageClassType::from(get_header_value_str(response.headers(), HEADER_STORAGE_CLASS));
        let az_redundancy = AzRedundancyType::from(get_header_value_str(response.headers(), HEADER_AZ_REDUNDANCY));
        let project_name = get_header_value(response.headers(), HEADER_PROJECT_NAME);

        Ok(Self { request_info, region, storage_class, az_redundancy, project_name })
    }
}

#[async_trait]
impl OutputParser for DeleteBucketOutput {
    async fn parse<B>(_: HttpRequest<'_, B>, _: HttpResponse, request_info: RequestInfo, _: Meta) -> Result<Self, TosError> where B: Send {
        Ok(Self { request_info })
    }
}