use crate::Result;
use crate::appendable_object_writer::AppendableObjectWriter;
use crate::model_ext::ReopenAppendableObjectRequest;
use crate::request_options::RequestOptions;
use std::sync::Arc;
use std::time::Duration;
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-stream")))]
#[derive(Clone, Debug)]
pub struct ReopenAppendableObject<S = crate::storage::transport::Storage> {
stub: Arc<S>,
request: ReopenAppendableObjectRequest,
options: RequestOptions,
}
impl<S> ReopenAppendableObject<S>
where
S: crate::storage::stub::Storage + 'static,
{
pub async fn send(self) -> Result<AppendableObjectWriter> {
self.stub
.reopen_appendable_object(self.request, self.options)
.await
}
}
impl<S> ReopenAppendableObject<S> {
pub(crate) fn new<B, O>(
stub: std::sync::Arc<S>,
bucket: B,
object: O,
generation: i64,
options: RequestOptions,
) -> Self
where
B: Into<String>,
O: Into<String>,
{
let request = ReopenAppendableObjectRequest {
bucket: bucket.into(),
object: object.into(),
generation,
if_metageneration_match: None,
if_metageneration_not_match: None,
routing_token: None,
write_handle: None,
params: None,
};
Self {
request,
options,
stub,
}
}
pub fn set_if_metageneration_match<V>(mut self, v: V) -> Self
where
V: Into<i64>,
{
self.request.if_metageneration_match = Some(v.into());
self
}
pub fn set_if_metageneration_not_match<V>(mut self, v: V) -> Self
where
V: Into<i64>,
{
self.request.if_metageneration_not_match = Some(v.into());
self
}
pub fn set_routing_token<V>(mut self, token: V) -> Self
where
V: Into<String>,
{
self.request.routing_token = Some(token.into());
self
}
pub fn set_write_handle<V>(mut self, handle: V) -> Self
where
V: Into<bytes::Bytes>,
{
self.request.write_handle = Some(handle.into());
self
}
pub fn with_retry_policy<V: Into<google_cloud_gax::retry_policy::RetryPolicyArg>>(
mut self,
v: V,
) -> Self {
self.options.retry_policy = v.into().into();
self
}
pub fn with_backoff_policy<V: Into<google_cloud_gax::backoff_policy::BackoffPolicyArg>>(
mut self,
v: V,
) -> Self {
self.options.backoff_policy = v.into().into();
self
}
pub fn with_retry_throttler<V: Into<google_cloud_gax::retry_throttler::RetryThrottlerArg>>(
mut self,
v: V,
) -> Self {
self.options.retry_throttler = v.into().into();
self
}
pub fn with_attempt_timeout(mut self, v: Duration) -> Self {
self.options.set_bidi_attempt_timeout(v);
self
}
pub fn with_user_agent<V>(mut self, user_agent: V) -> Self
where
V: Into<String>,
{
self.options.user_agent = Some(user_agent.into());
self
}
pub fn with_quota_project<V>(mut self, project: V) -> Self
where
V: Into<String>,
{
self.options.set_quota_project(project);
self
}
}
#[cfg(test)]
#[cfg(google_cloud_unstable_storage_bidi)]
mod tests {
use super::*;
use crate::client::Storage;
use crate::request_options::RequestOptions;
use anyhow::Result;
use google_cloud_auth::credentials::anonymous::Builder as Anonymous;
use static_assertions::assert_impl_all;
use std::sync::Arc;
#[derive(Debug)]
struct StorageStub;
impl crate::stub::Storage for StorageStub {}
const BUCKET_NAME: &str = "projects/_/buckets/test-bucket";
const OBJECT_NAME: &str = "test-object";
#[tokio::test]
async fn traits() -> Result<()> {
assert_impl_all!(ReopenAppendableObject: Clone, std::fmt::Debug, Send, Sync);
let client = Storage::builder()
.with_credentials(Anonymous::new().build())
.build()
.await?;
fn need_send<T: Send>(_val: &T) {}
fn need_static<T: 'static>(_val: &T) {}
let open = client.reopen_appendable_object(BUCKET_NAME, OBJECT_NAME, 123456);
need_static(&open);
let fut = client
.reopen_appendable_object(BUCKET_NAME, OBJECT_NAME, 123456)
.send();
need_send(&fut);
need_static(&fut);
Ok(())
}
#[test]
fn attributes() {
let options = RequestOptions::new();
let builder = ReopenAppendableObject::new(
Arc::new(StorageStub),
BUCKET_NAME.to_string(),
OBJECT_NAME.to_string(),
123456,
options,
)
.set_if_metageneration_match(456)
.set_if_metageneration_not_match(567)
.set_routing_token("token")
.set_write_handle(bytes::Bytes::from("handle"));
assert_eq!(builder.request.if_metageneration_match, Some(456));
assert_eq!(builder.request.if_metageneration_not_match, Some(567));
assert_eq!(builder.request.routing_token.as_deref(), Some("token"));
assert_eq!(
builder.request.write_handle,
Some(bytes::Bytes::from("handle"))
);
}
#[tokio::test]
async fn options() -> Result<()> {
use crate::retry_policy::RetryableErrors;
use google_cloud_gax::exponential_backoff::ExponentialBackoff;
let client = crate::client::Storage::builder()
.with_credentials(Anonymous::new().build())
.build()
.await?;
let builder = client
.reopen_appendable_object(BUCKET_NAME, OBJECT_NAME, 123456)
.with_attempt_timeout(std::time::Duration::from_secs(12))
.with_user_agent("test-agent")
.with_quota_project("test-project")
.with_retry_policy(RetryableErrors)
.with_backoff_policy(ExponentialBackoff::default())
.with_retry_throttler(google_cloud_gax::retry_throttler::CircuitBreaker::default());
assert_eq!(
builder.options.bidi_attempt_timeout,
std::time::Duration::from_secs(12)
);
assert_eq!(builder.options.user_agent.as_deref(), Some("test-agent"));
assert_eq!(
builder.options.quota_project.as_deref(),
Some("test-project")
);
Ok(())
}
}