Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
quettabit committed Jun 20, 2022
1 parent 9be59f6 commit 9aece41
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 41 deletions.
25 changes: 23 additions & 2 deletions src/v1/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::collections::BTreeMap;
#[doc(hidden)]
#[macro_export]
macro_rules! __make_obj_url {
($url:expr, $id:expr) => {{
($url:expr, $authority:expr, $id:expr) => {{
format!(
$url,
$authority.as_str(),
percent_encoding::percent_encode($id.bucket().as_ref(), crate::util::PATH_ENCODE_SET),
percent_encoding::percent_encode($id.object().as_ref(), crate::util::PATH_ENCODE_SET)
)
Expand All @@ -26,6 +27,7 @@ mod rewrite;
pub use delete::*;
pub use download::*;
pub use get::*;
use http::uri::Authority;
pub use insert::*;
pub use list::*;
pub use patch::*;
Expand All @@ -35,7 +37,26 @@ pub type Timestamp = time::OffsetDateTime;

/// Helper struct used to collate all of the operations available for
/// [Objects](https://cloud.google.com/storage/docs/json_api/v1/objects)
pub struct Object;
pub struct Object {
authority: Authority,
}

impl Object {
pub fn with_authority(authority: Authority) -> Self {
Self {
authority: authority,
}
}
}

impl Default for Object {
fn default() -> Self {
Self {
authority: Authority::from_static("storage.googleapis.com"),
}
}
}


/// [Metadata](https://cloud.google.com/storage/docs/json_api/v1/objects#resource)
/// associated with an Object.
Expand Down
3 changes: 2 additions & 1 deletion src/v1/objects/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ impl super::Object {
///
/// [Complete API documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/delete)
pub fn delete<'a, OID>(
&self,
id: &OID,
optional: Option<DeleteObjectOptional<'_>>,
) -> Result<http::Request<std::io::Empty>, Error>
where
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri = crate::__make_obj_url!("https://www.googleapis.com/storage/v1/b/{}/o/{}", id);
let mut uri = crate::__make_obj_url!("https://{}/storage/v1/b/{}/o/{}", self.authority, id);

let query = optional.unwrap_or_default();
let query_params = serde_urlencoded::to_string(query)?;
Expand Down
4 changes: 3 additions & 1 deletion src/v1/objects/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ impl super::Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/get)
pub fn download<'a, OID>(
&self,
id: &OID,
optional: Option<DownloadObjectOptional<'_>>,
) -> Result<http::Request<std::io::Empty>, Error>
where
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri = crate::__make_obj_url!(
"https://www.googleapis.com/storage/v1/b/{}/o/{}?alt=media",
"https://{}/storage/v1/b/{}/o/{}?alt=media",
self.authority,
id
);

Expand Down
4 changes: 3 additions & 1 deletion src/v1/objects/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ impl super::Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/get)
pub fn get<'a, OID>(
&self,
id: &OID,
optional: Option<GetObjectOptional<'_>>,
) -> Result<http::Request<std::io::Empty>, Error>
where
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri = crate::__make_obj_url!(
"https://www.googleapis.com/storage/v1/b/{}/o/{}?alt=json",
"https://{}/storage/v1/b/{}/o/{}?alt=json",
self.authority,
id
);

Expand Down
4 changes: 3 additions & 1 deletion src/v1/objects/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl super::Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/insert)
pub fn insert_simple<'a, OID, B>(
&self,
id: &OID,
content: B,
length: u64,
Expand All @@ -90,7 +91,8 @@ impl super::Object {
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri = format!(
"https://www.googleapis.com/upload/storage/v1/b/{}/o?name={}&uploadType=media",
"https://{}/upload/storage/v1/b/{}/o?name={}&uploadType=media",
self.authority.as_str(),
percent_encoding::percent_encode(id.bucket().as_ref(), crate::util::PATH_ENCODE_SET,),
percent_encoding::percent_encode(id.object().as_ref(), crate::util::QUERY_ENCODE_SET,),
);
Expand Down
4 changes: 3 additions & 1 deletion src/v1/objects/insert/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/insert)
pub fn insert_multipart<B>(
&self,
bucket: &BucketName<'_>,
content: B,
length: u64,
Expand All @@ -223,7 +224,8 @@ impl Object {
};

let mut uri = format!(
"https://www.googleapis.com/upload/storage/v1/b/{}/o?uploadType=multipart",
"https://{}/upload/storage/v1/b/{}/o?uploadType=multipart",
self.authority.as_str(),
percent_encoding::percent_encode(bucket.as_ref(), crate::util::PATH_ENCODE_SET,),
);

Expand Down
5 changes: 4 additions & 1 deletion src/v1/objects/insert/resumable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,17 @@ impl Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/performing-resumable-uploads#initiate-session)
pub fn resumable_insert_init<'a, OID>(
&self,
id: &OID,
content_type: Option<&str>,
) -> Result<http::Request<()>, Error>
where
OID: ObjectIdentifier<'a> + ?Sized,
{

let uri = format!(
"https://www.googleapis.com/upload/storage/v1/b/{}/o?uploadType=resumable&name={}",
"https://{}/upload/storage/v1/b/{}/o?uploadType=resumable&name={}",
self.authority.as_str(),
percent_encoding::percent_encode(id.bucket().as_ref(), crate::util::PATH_ENCODE_SET,),
percent_encoding::percent_encode(id.object().as_ref(), crate::util::QUERY_ENCODE_SET,),
);
Expand Down
3 changes: 2 additions & 1 deletion src/v1/objects/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ impl super::Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/list)
pub fn list(
&self,
bucket: &BucketName<'_>,
optional: Option<ListOptional<'_>>,
) -> Result<http::Request<std::io::Empty>, Error> {
let mut uri = format!("https://www.googleapis.com/storage/v1/b/{}/o", bucket);
let mut uri = format!("https://{}/storage/v1/b/{}/o", self.authority.as_str(), bucket);

let query = optional.unwrap_or_default();
let query_params = serde_urlencoded::to_string(query)?;
Expand Down
3 changes: 2 additions & 1 deletion src/v1/objects/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl super::Object {
///
/// [Complete API documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/patch)
pub fn patch<'a, OID>(
&self,
id: &OID,
metadata: &super::Metadata,
optional: Option<PatchObjectOptional<'_>>,
Expand All @@ -52,7 +53,7 @@ impl super::Object {
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri =
crate::__make_obj_url!("https://storage.googleapis.com/storage/v1/b/{}/o/{}", id);
crate::__make_obj_url!("https://{}/storage/v1/b/{}/o/{}", self.authority, id);

let query = optional.unwrap_or_default();
let query_params = serde_urlencoded::to_string(query)?;
Expand Down
4 changes: 3 additions & 1 deletion src/v1/objects/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl super::Object {
///
/// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite)
pub fn rewrite<'a, OID>(
&self,
source: &OID,
destination: &OID,
rewrite_token: Option<String>,
Expand All @@ -126,7 +127,8 @@ impl super::Object {
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri = format!(
"https://storage.googleapis.com/storage/v1/b/{}/o/{}/rewriteTo/b/{}/o/{}",
"https://{}/storage/v1/b/{}/o/{}/rewriteTo/b/{}/o/{}",
self.authority.as_str(),
percent_encoding::percent_encode(
source.bucket().as_ref(),
crate::util::PATH_ENCODE_SET
Expand Down
Loading

0 comments on commit 9aece41

Please sign in to comment.