Skip to content

Commit a924f4c

Browse files
authored
fix(storage)!: add required parameters to builders (#2361)
1 parent 84d89ea commit a924f4c

File tree

2 files changed

+51
-74
lines changed

2 files changed

+51
-74
lines changed

src/integration-tests/src/storage.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,14 @@ pub async fn objects(builder: storage::client::ClientBuilder) -> Result<()> {
4747
tracing::info!("testing insert_object()");
4848
const CONTENTS: &str = "the quick brown fox jumps over the lazy dog";
4949
let insert = client
50-
.insert_object()
51-
.set_bucket(&bucket.name)
52-
.set_object("quick.text")
53-
.set_payload(CONTENTS)
50+
.insert_object(&bucket.name, "quick.text", CONTENTS)
5451
.send()
5552
.await?;
5653
tracing::info!("success with insert={insert:?}");
5754

5855
tracing::info!("testing read_object()");
5956
let contents = client
60-
.read_object()
61-
.set_bucket(&bucket.name)
62-
.set_object(&insert.name)
57+
.read_object(&bucket.name, &insert.name)
6358
.send()
6459
.await?;
6560
assert_eq!(contents, CONTENTS.as_bytes());

src/storage/src/client.rs

Lines changed: 49 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -106,43 +106,58 @@ impl Storage {
106106

107107
/// A simple upload from a buffer.
108108
///
109+
/// # Parameters
110+
/// * `bucket` - the bucket name containing the object. In
111+
/// `projects/_/buckets/{bucket_id}` format.
112+
/// * `object` - the object name.
113+
/// * `payload` - the object data.
114+
///
109115
/// # Example
110116
/// ```
111117
/// # use google_cloud_storage::client::Storage;
112118
/// async fn example(client: &Storage) -> gax::Result<()> {
113119
/// let response = client
114-
/// .insert_object()
115-
/// .set_bucket("projects/_/buckets/my-bucket")
116-
/// .set_object("my-object")
117-
/// .set_payload("the quick brown fox jumped over the lazy dog")
120+
/// .insert_object("projects/_/buckets/my-bucket", "my-object", "the quick brown fox jumped over the lazy dog")
118121
/// .send()
119122
/// .await?;
120123
/// println!("response details={response:?}");
121124
/// Ok(())
122125
/// }
123126
/// ```
124-
pub fn insert_object(&self) -> InsertObject {
125-
InsertObject::new(self.inner.clone())
127+
pub fn insert_object<B, O, P>(&self, bucket: B, object: O, payload: P) -> InsertObject
128+
where
129+
B: Into<String>,
130+
O: Into<String>,
131+
P: Into<bytes::Bytes>,
132+
{
133+
InsertObject::new(self.inner.clone(), bucket, object, payload)
126134
}
127135

128136
/// A simple download into a buffer.
129137
///
138+
/// # Parameters
139+
/// * `bucket` - the bucket name containing the object. In
140+
/// `projects/_/buckets/{bucket_id}` format.
141+
/// * `object` - the object name.
142+
///
130143
/// # Example
131144
/// ```
132145
/// # use google_cloud_storage::client::Storage;
133146
/// async fn example(client: &Storage) -> gax::Result<()> {
134147
/// let contents = client
135-
/// .read_object()
136-
/// .set_bucket("projects/_/buckets/my-bucket")
137-
/// .set_object("my-object")
148+
/// .read_object("projects/_/buckets/my-bucket", "my-object")
138149
/// .send()
139150
/// .await?;
140151
/// println!("object contents={contents:?}");
141152
/// Ok(())
142153
/// }
143154
/// ```
144-
pub fn read_object(&self) -> ReadObject {
145-
ReadObject::new(self.inner.clone())
155+
pub fn read_object<B, O>(&self, bucket: B, object: O) -> ReadObject
156+
where
157+
B: Into<String>,
158+
O: Into<String>,
159+
{
160+
ReadObject::new(self.inner.clone(), bucket, object)
146161
}
147162

148163
pub(crate) async fn new(
@@ -255,12 +270,17 @@ pub struct InsertObject {
255270
}
256271

257272
impl InsertObject {
258-
fn new(inner: std::sync::Arc<StorageInner>) -> Self {
273+
fn new<B, O, P>(inner: std::sync::Arc<StorageInner>, bucket: B, object: O, payload: P) -> Self
274+
where
275+
B: Into<String>,
276+
O: Into<String>,
277+
P: Into<bytes::Bytes>,
278+
{
259279
InsertObject {
260280
inner,
261-
bucket: Default::default(),
262-
object: Default::default(),
263-
payload: Default::default(),
281+
bucket: bucket.into(),
282+
object: object.into(),
283+
payload: payload.into(),
264284
}
265285
}
266286

@@ -271,10 +291,7 @@ impl InsertObject {
271291
/// # use google_cloud_storage::client::Storage;
272292
/// async fn example(client: &Storage) -> gax::Result<()> {
273293
/// let response = client
274-
/// .insert_object()
275-
/// .set_bucket("projects/_/buckets/my-bucket")
276-
/// .set_object("my-object")
277-
/// .set_payload("the quick brown fox jumped over the lazy dog")
294+
/// .insert_object("projects/_/buckets/my-bucket", "my-object", "the quick brown fox jumped over the lazy dog")
278295
/// .send()
279296
/// .await?;
280297
/// println!("response details={response:?}");
@@ -320,24 +337,6 @@ impl InsertObject {
320337

321338
Ok(Object::from(response))
322339
}
323-
324-
/// Sets the value of bucket.
325-
pub fn set_bucket<T: Into<String>>(mut self, v: T) -> Self {
326-
self.bucket = v.into();
327-
self
328-
}
329-
330-
/// Sets the value of object.
331-
pub fn set_object<T: Into<String>>(mut self, v: T) -> Self {
332-
self.object = v.into();
333-
self
334-
}
335-
336-
/// Sets the value of payload.
337-
pub fn set_payload<T: Into<bytes::Bytes>>(mut self, v: T) -> Self {
338-
self.payload = v.into();
339-
self
340-
}
341340
}
342341

343342
/// The request builder for [Storage::read_object][crate::client::Storage::read_object] calls.
@@ -350,10 +349,7 @@ impl InsertObject {
350349
/// # let client = Storage::builder()
351350
/// # .with_endpoint("https://storage.googleapis.com")
352351
/// # .build().await?;
353-
/// let builder: ReadObject = client
354-
/// .read_object()
355-
/// .set_bucket("projects/_/buckets/my-bucket")
356-
/// .set_object("my-object");
352+
/// let builder: ReadObject = client.read_object("projects/_/buckets/my-bucket", "my-object");
357353
/// let contents = builder.send().await?;
358354
/// println!("object contents={contents:?}");
359355
/// # Ok::<(), anyhow::Error>(()) });
@@ -364,25 +360,19 @@ pub struct ReadObject {
364360
}
365361

366362
impl ReadObject {
367-
fn new(inner: std::sync::Arc<StorageInner>) -> Self {
363+
fn new<B, O>(inner: std::sync::Arc<StorageInner>, bucket: B, object: O) -> Self
364+
where
365+
B: Into<String>,
366+
O: Into<String>,
367+
{
368368
ReadObject {
369369
inner,
370-
request: control::model::ReadObjectRequest::new(),
370+
request: control::model::ReadObjectRequest::new()
371+
.set_bucket(bucket)
372+
.set_object(object),
371373
}
372374
}
373375

374-
/// Sets the value of [bucket][control::model::ReadObjectRequest::bucket].
375-
pub fn set_bucket<T: Into<String>>(mut self, v: T) -> Self {
376-
self.request.bucket = v.into();
377-
self
378-
}
379-
380-
/// Sets the value of [object][control::model::ReadObjectRequest::object].
381-
pub fn set_object<T: Into<String>>(mut self, v: T) -> Self {
382-
self.request.object = v.into();
383-
self
384-
}
385-
386376
/// Sets the value of [generation][control::model::ReadObjectRequest::generation].
387377
pub fn set_generation<T: Into<i64>>(mut self, v: T) -> Self {
388378
self.request.generation = v.into();
@@ -576,9 +566,7 @@ mod tests {
576566
.await?;
577567

578568
let read_object_builder = client
579-
.read_object()
580-
.set_bucket("projects/_/buckets/bucket")
581-
.set_object("object")
569+
.read_object("projects/_/buckets/bucket", "object")
582570
.http_request_builder()
583571
.await?
584572
.build()?;
@@ -600,9 +588,7 @@ mod tests {
600588
.await?;
601589

602590
client
603-
.read_object()
604-
.set_bucket("projects/_/buckets/bucket")
605-
.set_object("object")
591+
.read_object("projects/_/buckets/bucket", "object")
606592
.http_request_builder()
607593
.await
608594
.inspect_err(|e| assert!(e.is_authentication()))
@@ -618,9 +604,7 @@ mod tests {
618604
.await?;
619605

620606
client
621-
.read_object()
622-
.set_bucket("malformed")
623-
.set_object("object")
607+
.read_object("malformed", "object")
624608
.http_request_builder()
625609
.await
626610
.expect_err("malformed bucket string should error");
@@ -635,9 +619,7 @@ mod tests {
635619
.await?;
636620

637621
let read_object_builder = client
638-
.read_object()
639-
.set_bucket("projects/_/buckets/bucket")
640-
.set_object("object")
622+
.read_object("projects/_/buckets/bucket", "object")
641623
.set_generation(5)
642624
.set_if_generation_match(10)
643625
.set_if_generation_not_match(20)

0 commit comments

Comments
 (0)