use std::future::Future;
use http::Method;
use serde::{Deserialize, Serialize};
use crate::body::NoneBody;
use crate::error::Result;
use crate::response::HeaderResponseProcessor;
use crate::ser::OnlyKeyField;
use crate::{Client, Ops, Prepared, Request};
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetObjectMetaParams {
pub(crate) object_meta: OnlyKeyField,
#[serde(skip_serializing_if = "Option::is_none")]
pub version_id: Option<String>,
}
impl GetObjectMetaParams {
pub fn new() -> Self {
Self {
object_meta: OnlyKeyField,
version_id: None,
}
}
pub fn version_id(mut self, version_id: impl Into<String>) -> Self {
self.version_id = Some(version_id.into());
self
}
}
impl Default for GetObjectMetaParams {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct GetObjectMetaResponse {
#[serde(rename = "content-length")]
pub content_length: Option<String>,
#[serde(rename = "etag")]
pub etag: Option<String>,
#[serde(rename = "last-modified")]
pub last_modified: Option<String>,
#[serde(rename = "x-oss-version-id")]
pub version_id: Option<String>,
#[serde(rename = "x-oss-last-access-time")]
pub last_access_time: Option<String>,
#[serde(rename = "x-oss-transition-time")]
pub transition_time: Option<String>,
#[serde(rename = "x-oss-sealed-time")]
pub sealed_time: Option<String>,
}
pub struct GetObjectMeta {
pub object_key: String,
pub params: GetObjectMetaParams,
}
impl Ops for GetObjectMeta {
type Response = HeaderResponseProcessor<GetObjectMetaResponse>;
type Body = NoneBody;
type Query = GetObjectMetaParams;
fn prepare(self) -> Result<Prepared<GetObjectMetaParams>> {
Ok(Prepared {
method: Method::HEAD,
key: Some(self.object_key),
query: Some(self.params),
..Default::default()
})
}
}
pub trait GetObjectMetaOperations {
fn get_object_meta(
&self,
object_key: impl Into<String>,
params: Option<GetObjectMetaParams>,
) -> impl Future<Output = Result<GetObjectMetaResponse>>;
}
impl GetObjectMetaOperations for Client {
async fn get_object_meta(
&self,
object_key: impl Into<String>,
params: Option<GetObjectMetaParams>,
) -> Result<GetObjectMetaResponse> {
let ops = GetObjectMeta {
object_key: object_key.into(),
params: params.unwrap_or_default(),
};
self.request(ops).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize_params_default() {
let q = crate::ser::to_string(&GetObjectMetaParams::new()).unwrap();
assert_eq!(q, "objectMeta");
}
#[test]
fn test_serialize_params_with_version() {
let q = crate::ser::to_string(&GetObjectMetaParams::new().version_id("v1")).unwrap();
assert_eq!(q, "objectMeta&versionId=v1");
}
#[test]
fn test_deserialize_response() {
let json = serde_json::json!({
"content-length": "344606",
"etag": "\"5B3C1A2E\"",
"last-modified": "Fri, 24 Feb 2012 06:07:48 GMT",
"x-oss-version-id": "v1",
});
let resp: GetObjectMetaResponse = serde_json::from_value(json).unwrap();
assert_eq!(resp.content_length.as_deref(), Some("344606"));
assert_eq!(resp.version_id.as_deref(), Some("v1"));
}
}