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
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;
#[derive(Debug,Clone,Copy,Eq,PartialEq)]
pub enum BucketType {
Public, Private, Snapshot
}
impl BucketType {
pub fn from_str(s: &str) -> Option<BucketType> {
match s {
"allPublic" => Some(BucketType::Public),
"allPrivate" => Some(BucketType::Private),
"snapshot" => Some(BucketType::Snapshot),
_ => None
}
}
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())
}
}
#[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
}
#[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>
}
impl<'a> B2Authorization<'a> {
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)
}
}
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)
}
}
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)
}
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)
}
}
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)
}
}