Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add r2 endpoints (#170) #171

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cloudflare/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod argo_tunnel;
pub mod dns;
pub mod load_balancing;
pub mod plan;
pub mod r2;
pub mod user;
pub mod workers;
pub mod workerskv;
Expand Down
81 changes: 81 additions & 0 deletions cloudflare/src/endpoints/r2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
)
}
}