|
| 1 | +use crate::{blob::BlobProperties, prelude::*}; |
| 2 | +use azure_core::prelude::*; |
| 3 | +use azure_core::{ |
| 4 | + headers::{ |
| 5 | + date_from_headers, etag_from_headers, request_id_from_headers, server_from_headers, Headers, |
| 6 | + }, |
| 7 | + Method, RequestId, |
| 8 | +}; |
| 9 | +use chrono::{DateTime, Utc}; |
| 10 | +use std::convert::{TryFrom, TryInto}; |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct SetPropertiesBuilder { |
| 14 | + blob_client: BlobClient, |
| 15 | + lease_id: Option<LeaseId>, |
| 16 | + timeout: Option<Timeout>, |
| 17 | + cache_control: Option<BlobCacheControl>, |
| 18 | + content_type: Option<BlobContentType>, |
| 19 | + content_encoding: Option<BlobContentEncoding>, |
| 20 | + content_language: Option<BlobContentLanguage>, |
| 21 | + content_disposition: Option<BlobContentDisposition>, |
| 22 | + content_md5: Option<BlobContentMD5>, |
| 23 | + context: Context, |
| 24 | +} |
| 25 | + |
| 26 | +impl SetPropertiesBuilder { |
| 27 | + pub(crate) fn new(blob_client: BlobClient) -> Self { |
| 28 | + Self { |
| 29 | + blob_client, |
| 30 | + lease_id: None, |
| 31 | + timeout: None, |
| 32 | + cache_control: None, |
| 33 | + content_type: None, |
| 34 | + content_encoding: None, |
| 35 | + content_language: None, |
| 36 | + content_disposition: None, |
| 37 | + content_md5: None, |
| 38 | + context: Context::new(), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn set_from_blob_properties(self, blob_properties: BlobProperties) -> Self { |
| 43 | + let mut s = self; |
| 44 | + |
| 45 | + if let Some(cc) = blob_properties.cache_control { |
| 46 | + s = s.cache_control(cc); |
| 47 | + } |
| 48 | + if !blob_properties.content_type.is_empty() { |
| 49 | + s = s.content_type(blob_properties.content_type); |
| 50 | + } |
| 51 | + if let Some(ce) = blob_properties.content_encoding { |
| 52 | + s = s.content_encoding(ce); |
| 53 | + } |
| 54 | + if let Some(cl) = blob_properties.content_language { |
| 55 | + s = s.content_language(cl); |
| 56 | + } |
| 57 | + if let Some(cd) = blob_properties.content_disposition { |
| 58 | + s = s.content_disposition(cd); |
| 59 | + } |
| 60 | + if let Some(cmd5) = blob_properties.content_md5 { |
| 61 | + s = s.content_md5(cmd5); |
| 62 | + } |
| 63 | + s |
| 64 | + } |
| 65 | + |
| 66 | + setters! { |
| 67 | + lease_id: LeaseId => Some(lease_id), |
| 68 | + timeout: Timeout => Some(timeout), |
| 69 | + cache_control: BlobCacheControl => Some(cache_control), |
| 70 | + content_type: BlobContentType => Some(content_type), |
| 71 | + content_encoding: BlobContentEncoding => Some(content_encoding), |
| 72 | + content_language: BlobContentLanguage => Some(content_language), |
| 73 | + content_disposition: BlobContentDisposition => Some(content_disposition), |
| 74 | + content_md5: BlobContentMD5 => Some(content_md5), |
| 75 | + context: Context => context, |
| 76 | + } |
| 77 | + |
| 78 | + pub fn into_future(mut self) -> Response { |
| 79 | + Box::pin(async move { |
| 80 | + let mut url = self.blob_client.url_with_segments(None)?; |
| 81 | + |
| 82 | + url.query_pairs_mut().append_pair("comp", "properties"); |
| 83 | + self.timeout.append_to_url_query(&mut url); |
| 84 | + |
| 85 | + let mut request = self.blob_client.prepare_request(url, Method::Put, None)?; |
| 86 | + request.add_optional_header(&self.lease_id); |
| 87 | + request.add_optional_header(&self.cache_control); |
| 88 | + request.add_optional_header(&self.content_type); |
| 89 | + request.add_optional_header(&self.content_encoding); |
| 90 | + request.add_optional_header(&self.content_language); |
| 91 | + request.add_optional_header(&self.content_disposition); |
| 92 | + request.add_optional_header(&self.content_md5); |
| 93 | + |
| 94 | + let response = self |
| 95 | + .blob_client |
| 96 | + .send(&mut self.context, &mut request) |
| 97 | + .await?; |
| 98 | + response.headers().try_into() |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[derive(Debug, Clone)] |
| 104 | +pub struct SetPropertiesResponse { |
| 105 | + pub request_id: RequestId, |
| 106 | + pub etag: String, |
| 107 | + pub server: String, |
| 108 | + pub date: DateTime<Utc>, |
| 109 | +} |
| 110 | + |
| 111 | +impl TryFrom<&Headers> for SetPropertiesResponse { |
| 112 | + type Error = crate::Error; |
| 113 | + |
| 114 | + fn try_from(headers: &Headers) -> Result<Self, Self::Error> { |
| 115 | + Ok(SetPropertiesResponse { |
| 116 | + request_id: request_id_from_headers(headers)?, |
| 117 | + etag: etag_from_headers(headers)?, |
| 118 | + server: server_from_headers(headers)?.to_owned(), |
| 119 | + date: date_from_headers(headers)?, |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | +pub type Response = futures::future::BoxFuture<'static, azure_core::Result<SetPropertiesResponse>>; |
| 124 | + |
| 125 | +#[cfg(feature = "into_future")] |
| 126 | +impl std::future::IntoFuture for SetPropertiesBuilder { |
| 127 | + type IntoFuture = Response; |
| 128 | + type Output = <Response as std::future::Future>::Output; |
| 129 | + fn into_future(self) -> Self::IntoFuture { |
| 130 | + Self::into_future(self) |
| 131 | + } |
| 132 | +} |
0 commit comments