1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use chrono::offset::Utc;
use chrono::DateTime;
use serde::Deserialize;
use std::collections::HashMap;

use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::response::ApiResult;

/// A Bucket is a collection of Objects stored in R2.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct Bucket {
    /// Bucket name
    pub name: String,
    /// Creation date of the bucket
    pub creation_date: DateTime<Utc>,
}

/// ListBucketsResult contains a list of buckets in an account.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct ListBucketsResult {
    pub buckets: Vec<Bucket>,
}

type EmptyMap = HashMap<(), ()>;
impl ApiResult for EmptyMap {}
impl ApiResult for ListBucketsResult {}

/// Lists all buckets within the account.
#[derive(Debug)]
pub struct ListBuckets<'a> {
    pub account_identifier: &'a str,
}

impl<'a> Endpoint<ListBucketsResult> for ListBuckets<'a> {
    fn method(&self) -> Method {
        Method::Get
    }
    fn path(&self) -> String {
        format!("accounts/{}/r2/buckets", self.account_identifier)
    }
}

/// Creates a bucket with the given name.
/// A 400 is returned if the account already owns a bucket with this name.
/// A bucket must be explicitly deleted to be replaced.
#[derive(Debug)]
pub struct CreateBucket<'a> {
    pub account_identifier: &'a str,
    pub bucket_name: &'a str,
}

impl<'a> Endpoint<EmptyMap> for CreateBucket<'a> {
    fn method(&self) -> Method {
        Method::Put
    }
    fn path(&self) -> String {
        format!(
            "accounts/{}/r2/buckets/{}",
            self.account_identifier, self.bucket_name
        )
    }
}

/// Deletes a bucket with the given name.
#[derive(Debug)]
pub struct DeleteBucket<'a> {
    pub account_identifier: &'a str,
    pub bucket_name: &'a str,
}

impl<'a> Endpoint<EmptyMap> for DeleteBucket<'a> {
    fn method(&self) -> Method {
        Method::Delete
    }
    fn path(&self) -> String {
        format!(
            "accounts/{}/r2/buckets/{}",
            self.account_identifier, self.bucket_name
        )
    }
}