ruma_client_api/authenticated_media/
get_media_preview.rs
1pub mod v1 {
6 use ruma_common::{
11 api::{request, response, Metadata},
12 metadata, MilliSecondsSinceUnixEpoch,
13 };
14 use serde::Serialize;
15 use serde_json::value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue};
16
17 const METADATA: Metadata = metadata! {
18 method: GET,
19 rate_limited: true,
20 authentication: AccessToken,
21 history: {
22 unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/preview_url",
23 1.11 => "/_matrix/client/v1/media/preview_url",
24 }
25 };
26
27 #[request(error = crate::Error)]
29 pub struct Request {
30 #[ruma_api(query)]
32 pub url: String,
33
34 #[ruma_api(query)]
36 #[serde(skip_serializing_if = "Option::is_none")]
37 pub ts: Option<MilliSecondsSinceUnixEpoch>,
38 }
39
40 #[response(error = crate::Error)]
42 #[derive(Default)]
43 pub struct Response {
44 #[ruma_api(body)]
49 pub data: Option<Box<RawJsonValue>>,
50 }
51
52 impl Request {
53 pub fn new(url: String) -> Self {
55 Self { url, ts: None }
56 }
57 }
58
59 impl Response {
60 pub fn new() -> Self {
62 Self { data: None }
63 }
64
65 pub fn from_raw_value(data: Box<RawJsonValue>) -> Self {
68 Self { data: Some(data) }
69 }
70
71 pub fn from_serialize<T: Serialize>(data: &T) -> serde_json::Result<Self> {
74 Ok(Self { data: Some(to_raw_json_value(data)?) })
75 }
76 }
77
78 #[cfg(test)]
79 mod tests {
80 use assert_matches2::assert_matches;
81 use serde_json::{
82 from_value as from_json_value, json,
83 value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue},
84 };
85
86 #[test]
89 fn raw_json_deserialize() {
90 type OptRawJson = Option<Box<RawJsonValue>>;
91
92 assert_matches!(from_json_value::<OptRawJson>(json!(null)).unwrap(), None);
93 from_json_value::<OptRawJson>(json!("test")).unwrap().unwrap();
94 from_json_value::<OptRawJson>(json!({ "a": "b" })).unwrap().unwrap();
95 }
96
97 #[test]
99 fn raw_json_serialize() {
100 to_raw_json_value(&json!(null)).unwrap();
101 to_raw_json_value(&json!("string")).unwrap();
102 to_raw_json_value(&json!({})).unwrap();
103 to_raw_json_value(&json!({ "a": "b" })).unwrap();
104 }
105 }
106}