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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! This module defines various functions on the [`B2Authorization`] type for interacting with the
//! buckets on backblaze.
//!
//!  [`B2Authorization`]: ../authorize/struct.B2Authorization.html

use std::fmt;

use hyper::{self, Client};
use hyper::client::Body;

use serde::{Serialize, Deserialize};
use serde::ser::Serializer;
use serde::de::{self, Visitor, Deserializer};
use serde_json::{self, Value as JsonValue};

use B2Error;
use raw::authorize::B2Authorization;

/// Specifies the type of a bucket on backblaze.
#[derive(Debug,Clone,Copy,Eq,PartialEq)]
pub enum BucketType {
    Public, Private, Snapshot
}
impl BucketType {
    /// Creates a BucketType from a string. The strings are the ones used by the backblaze api.
    ///
    /// ```rust
    ///use backblaze_b2::raw::buckets::BucketType;
    ///
    ///assert_eq!(BucketType::from_str("allPublic"), Some(BucketType::Public));
    ///assert_eq!(BucketType::from_str("allPrivate"), Some(BucketType::Private));
    ///assert_eq!(BucketType::from_str("snapshot"), Some(BucketType::Snapshot));
    /// ```
    pub fn from_str(s: &str) -> Option<BucketType> {
        match s {
            "allPublic" => Some(BucketType::Public),
            "allPrivate" => Some(BucketType::Private),
            "snapshot" => Some(BucketType::Snapshot),
            _ => None
        }
    }
    /// This function returns the string needed to specify the bucket type to the backblaze api.
    pub fn as_str(&self) -> &'static str {
        match *self {
            BucketType::Public => "allPublic",
            BucketType::Private => "allPrivate",
            BucketType::Snapshot => "snapshot"
        }
    }
}
static BUCKET_TYPES: [&str; 3] = ["allPublic", "allPrivate", "snapshot"];
struct BucketTypeVisitor;
impl<'de> Visitor<'de> for BucketTypeVisitor {
    type Value = BucketType;
    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("allPublic, allPrivate or snapshot")
    }
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: de::Error {
        match BucketType::from_str(v) {
            None => Err(de::Error::unknown_variant(v, &BUCKET_TYPES)),
            Some(v) => Ok(v)
        }
    }
    fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> where E: de::Error {
        match BucketType::from_str(v) {
            None => Err(de::Error::unknown_variant(v, &BUCKET_TYPES)),
            Some(v) => Ok(v)
        }
    }
    fn visit_string<E>(self, v: String) -> Result<Self::Value, E> where E: de::Error {
        match BucketType::from_str(&v) {
            None => Err(de::Error::unknown_variant(&v, &BUCKET_TYPES)),
            Some(v) => Ok(v)
        }
    }
}
impl<'de> Deserialize<'de> for BucketType {
    fn deserialize<D>(deserializer: D) -> Result<BucketType, D::Error>
        where D: Deserializer<'de>
    {
        deserializer.deserialize_str(BucketTypeVisitor)
    }
}
impl Serialize for BucketType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        serializer.serialize_str(self.as_str())
    }
}

/// This struct contains a lifecycle rule as specified in the [backblaze b2
/// documentation](https://www.backblaze.com/b2/docs/lifecycle_rules.html).
#[derive(Serialize,Deserialize,Debug,Clone)]
#[serde(rename_all = "camelCase")]
pub struct LifecycleRule {
    days_from_uploading_to_hiding: Option<u32>,
    days_from_hiding_to_deleting: Option<u32>,
    file_name_prefix: String
}

/// This function contains various information about a backblaze bucket.
#[derive(Serialize,Deserialize,Debug,Clone)]
#[serde(rename_all = "camelCase")]
pub struct Bucket<InfoType=JsonValue> {
    pub account_id: String,
    pub bucket_id: String,
    pub bucket_name: String,
    pub bucket_type: BucketType,
    pub bucket_info: InfoType,
    pub lifecycle_rules: Vec<LifecycleRule>,
    pub revision: u32
}

#[derive(Deserialize)]
struct ListBucketsResponse<InfoType> {
    buckets: Vec<Bucket<InfoType>>
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct CreateBucketRequest<'a, InfoType> {
    account_id: &'a str,
    bucket_name: &'a str,
    bucket_type: BucketType,
    bucket_info: InfoType,
    lifecycle_rules: Vec<LifecycleRule>
}
/// Methods related to the [buckets module][1].
///
///  [1]: ../buckets/index.html
impl<'a> B2Authorization<'a> {
    /// Performs a [b2_list_buckets][1] api call.
    ///
    /// # Errors
    /// This function returns a [`B2Error`] in case something goes wrong. This function is only
    /// going to fail with the standard errors.
    ///
    ///  [1]: https://www.backblaze.com/b2/docs/b2_list_buckets.html
    ///  [`B2Error`]: ../../enum.B2Error.html
    pub fn list_buckets<InfoType>(&self, client: &Client)
        -> Result<Vec<Bucket<InfoType>>,B2Error>
        where for<'de> InfoType: Deserialize<'de>
    {
        let url_string: String = format!("{}/b2api/v1/b2_list_buckets?accountId={}",
                                               self.api_url, self.credentials.id);
        let url: &str = &url_string;
        let resp = try!(client.get(url)
            .header(self.auth_header())
            .send());
        if resp.status != hyper::status::StatusCode::Ok {
            Err(B2Error::from_response(resp))
        } else {
            let buckets: ListBucketsResponse<InfoType> = try!(serde_json::from_reader(resp));
            Ok(buckets.buckets)
        }
    }
    /// Performs a [b2_create_bucket][1] api call.
    ///
    /// # Errors
    /// This function returns a [`B2Error`] in case something goes wrong. Besides the standard
    /// errors, this function can fail with [`is_maximum_bucket_limit`],
    /// [`is_duplicate_bucket_name`] and [`is_invalid_bucket_name`].
    ///
    ///  [1]: https://www.backblaze.com/b2/docs/b2_create_bucket.html
    ///  [`B2Error`]: ../../enum.B2Error.html
    ///  [`is_maximum_bucket_limit`]: ../../enum.B2Error.html#method.is_maximum_bucket_limit
    ///  [`is_duplicate_bucket_name`]: ../../enum.B2Error.html#method.is_duplicate_bucket_name
    ///  [`is_invalid_bucket_name`]: ../../enum.B2Error.html#method.is_invalid_bucket_name
    pub fn create_bucket<InfoType>(&self,
                                   bucket_name: &str,
                                   bucket_type: BucketType,
                                   bucket_info: InfoType,
                                   lifecycle_rules: Vec<LifecycleRule>,
                                   client: &Client)
        -> Result<Bucket<InfoType>, B2Error>
        where for <'de> InfoType: Serialize + Deserialize<'de>
    {
        let url_string: String = format!("{}/b2api/v1/b2_create_bucket", self.api_url);
        let url: &str = &url_string;

        let body = try!(serde_json::to_string(&CreateBucketRequest {
            account_id: &self.credentials.id,
            bucket_name: bucket_name,
            bucket_type: bucket_type,
            bucket_info: bucket_info,
            lifecycle_rules: lifecycle_rules
        }));

        let resp = try!(client.post(url)
            .body(Body::BufBody(body.as_bytes(), body.len()))
            .header(self.auth_header())
            .send());
        if resp.status != hyper::status::StatusCode::Ok {
            Err(B2Error::from_response(resp))
        } else {
            let bucket: Bucket<InfoType> = try!(serde_json::from_reader(resp));
            Ok(bucket)
        }
    }
    /// Performs a [b2_create_bucket][1] api call. This function initializes the bucket with no
    /// info.
    ///
    /// # Errors
    /// This function returns a [`B2Error`] in case something goes wrong. Besides the standard
    /// errors, this function can fail with [`is_maximum_bucket_limit`],
    /// [`is_duplicate_bucket_name`] and [`is_invalid_bucket_name`].
    ///
    ///  [1]: https://www.backblaze.com/b2/docs/b2_create_bucket.html
    ///  [`B2Error`]: ../../enum.B2Error.html
    ///  [`is_maximum_bucket_limit`]: ../../enum.B2Error.html#method.is_maximum_bucket_limit
    ///  [`is_duplicate_bucket_name`]: ../../enum.B2Error.html#method.is_duplicate_bucket_name
    ///  [`is_invalid_bucket_name`]: ../../enum.B2Error.html#method.is_invalid_bucket_name
    pub fn create_bucket_no_info(&self,
                                   bucket_name: &str,
                                   bucket_type: BucketType,
                                   lifecycle_rules: Vec<LifecycleRule>,
                                   client: &Client)
        -> Result<Bucket<JsonValue>, B2Error>
    {
        self.create_bucket(bucket_name, bucket_type, JsonValue::Object(serde_json::map::Map::new()),
            lifecycle_rules, client)
    }
    /// Performs a [b2_delete_bucket][1] api call.
    ///
    /// # Errors
    /// This function returns a [`B2Error`] in case something goes wrong. Besides the standard
    /// errors, this function can fail with [`is_bucket_not_found`].
    ///
    ///  [1]: https://www.backblaze.com/b2/docs/b2_delete_bucket.html
    ///  [`B2Error`]: ../../enum.B2Error.html
    ///  [`is_bucket_not_found`]: ../../enum.B2Error.html#method.is_bucket_not_found
    pub fn delete_bucket_id<InfoType>(&self, bucket_id: &str, client: &Client)
        -> Result<Bucket<InfoType>, B2Error>
        where for <'de> InfoType: Deserialize<'de>
    {
        let url_string: String = format!("{}/b2api/v1/b2_delete_bucket", self.api_url);
        let url: &str = &url_string;

        let body: String =
            format!("{{\"accountId\":\"{}\", \"bucketId\":\"{}\"}}", self.credentials.id, bucket_id);

        let resp = try!(client.post(url)
            .body(Body::BufBody(body.as_bytes(), body.len()))
            .header(self.auth_header())
            .send());
        if resp.status != hyper::status::StatusCode::Ok {
            Err(B2Error::from_response(resp))
        } else {
            let bucket: Bucket<InfoType> = try!(serde_json::from_reader(resp));
            Ok(bucket)
        }
    }
    /// Performs a [b2_delete_bucket][1] api call.
    ///
    /// # Errors
    /// This function returns a [`B2Error`] in case something goes wrong. Besides the standard
    /// errors, this function can fail with [`is_bucket_not_found`].
    ///
    ///  [1]: https://www.backblaze.com/b2/docs/b2_delete_bucket.html
    ///  [`B2Error`]: ../../enum.B2Error.html
    ///  [`is_bucket_not_found`]: ../../enum.B2Error.html#method.is_bucket_not_found
    pub fn delete_bucket<InfoType>(&self, bucket: &Bucket<InfoType>, client: &Client)
        -> Result<Bucket<InfoType>, B2Error>
        where for <'de> InfoType: Deserialize<'de>
    {
        self.delete_bucket_id(&bucket.bucket_id, client)
    }

}