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

Add support for custom authority #59

Merged
merged 2 commits into from
Jun 22, 2022
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
24 changes: 22 additions & 2 deletions src/v1/objects.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! Types and APIs for interacting with GCS [Objects](https://cloud.google.com/storage/docs/json_api/v1/objects)

use crate::common::StorageClass;
use http::uri::Authority;
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 @@ -35,7 +37,25 @@ 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;
/// Additionally, it can also be used to specify a custom authority.
#[derive(Clone, Debug)]
pub struct Object {
authority: Authority,
}

impl Object {
pub fn with_authority(authority: Authority) -> Self {
Self { 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
4 changes: 3 additions & 1 deletion src/v1/objects/insert/resumable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,16 @@ 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
7 changes: 6 additions & 1 deletion src/v1/objects/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ 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
4 changes: 2 additions & 2 deletions src/v1/objects/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ 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<'_>>,
) -> Result<http::Request<std::io::Cursor<Vec<u8>>>, Error>
where
OID: ObjectIdentifier<'a> + ?Sized,
{
let mut uri =
crate::__make_obj_url!("https://storage.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/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