1use std::io::Read;
5
6use crate::QueryOptions;
7use crate::common::tmf_error::TMFError;
8use tmflib::{HasId,Uri};
9use serde::{de::DeserializeOwned, Serialize};
10#[cfg(feature = "tmf620")]
13pub mod tmf620;
14#[cfg(feature = "tmf622")]
15pub mod tmf622;
16#[cfg(feature = "tmf629")]
17pub mod tmf629;
18#[cfg(feature = "tmf632")]
19pub mod tmf632;
20#[cfg(feature = "tmf633")]
21pub mod tmf633;
22#[cfg(feature = "tmf637")]
23pub mod tmf637;
24#[cfg(feature = "tmf638")]
25pub mod tmf638;
26#[cfg(feature = "tmf639")]
27pub mod tmf639;
28#[cfg(feature = "tmf645")]
29pub mod tmf645;
30#[cfg(feature = "tmf648")]
31pub mod tmf648;
32#[cfg(feature = "tmf663")]
33pub mod tmf663;
34#[cfg(feature = "tmf674")]
35pub mod tmf674;
36
37static USER_AGENT : &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
38
39pub fn get_tmf<T : HasId + DeserializeOwned>(host: Uri, id : String) -> Result<Vec<T>,TMFError> {
41 let url = format!("{}{}/{}",host,T::get_class_href(),id);
43 let insecure = false;
44 #[cfg(feature = "insecure")]
45 let insecure = true; let client = reqwest::blocking::Client::builder()
48 .danger_accept_invalid_certs(insecure).user_agent(USER_AGENT)
50 .use_rustls_tls()
51 .build()?;
52
53 let objects = client
54 .get(url)
55 .send()?
56 .text()?;
57 let output : T = serde_json::from_str(objects.as_str())?;
58 Ok(vec![output])
59}
60
61pub fn list_tmf<T : HasId + DeserializeOwned>(host: Uri, filter : Option<QueryOptions>) -> Result<Vec<T>,TMFError> {
63 let filter = match filter {
65 Some(f) => f.into(),
66 None => String::default(),
67 };
68 let url = format!("{}{}?{}",host,T::get_class_href(),filter);
69 let insecure = false;
70 #[cfg(feature = "insecure")]
71 let insecure = true; let client = reqwest::blocking::Client::builder()
73 .danger_accept_invalid_certs(insecure) .user_agent(USER_AGENT)
75 .use_rustls_tls()
76 .build()?;
77
78 let objects = client
79 .get(url)
80 .send()?
81 .text()?;
82 let output : Vec<T> = serde_json::from_str(objects.as_str())?;
83 Ok(output)
84}
85
86pub fn create_tmf<T : HasId + Serialize + DeserializeOwned>(host : Uri, item : T) -> Result<T,TMFError> {
88 let url = format!("{}{}",host,T::get_class_href());
89 let insecure = false;
90 #[cfg(feature = "insecure")]
91 let insecure = true; let client = reqwest::blocking::Client::builder()
93 .danger_accept_invalid_certs(insecure) .use_rustls_tls()
95 .user_agent(USER_AGENT)
96 .build()?;
97 let body_str = serde_json::to_string(&item)?;
98 let mut res = client.post(url)
99 .body(body_str)
100 .send()?;
101 let mut output = String::default();
102 let _count = res.read_to_string(&mut output)?;
103 match res.status() {
104 reqwest::StatusCode::CREATED | reqwest::StatusCode::OK => {
105 let item : T = serde_json::from_str(output.as_str())?;
106 Ok(item)
107 },
108 _ => Err(TMFError::Unknown(format!("Failed to create TMF object: {output}"))),
109 }
110
111}
112
113pub fn update_tmf<T : HasId + Serialize + DeserializeOwned>(host : Uri, id : impl Into<String>, patch : T) -> Result<T,TMFError> {
115 let url = format!("{}{}/{}",host,T::get_class_href(),id.into());
116 let insecure = false;
117 #[cfg(feature = "insecure")]
118 let insecure = true; let client = reqwest::blocking::Client::builder()
120 .danger_accept_invalid_certs(insecure) .use_rustls_tls()
122 .user_agent(USER_AGENT)
123 .build()?;
124
125 let body_str = serde_json::to_string(&patch)?;
126 let mut res = client.patch(url)
127 .body(body_str)
128 .send()?;
129 let mut output = String::default();
130 let _ = res.read_to_string(&mut output);
131 let item : T = serde_json::from_str(output.as_str())?;
132 Ok(item)
133}
134
135pub fn delete_tmf<T : HasId>(host : Uri, id : impl Into<String>) -> Result<T,TMFError> {
137 let url = format!("{}{}/{}",host,T::get_class_href(),id.into().clone());
138 let insecure = false;
139 #[cfg(feature = "insecure")]
140 let insecure = true; let client = reqwest::blocking::Client::builder()
142 .danger_accept_invalid_certs(insecure) .use_rustls_tls()
144 .user_agent(USER_AGENT)
145 .build()?;
146
147 let mut _res = client.delete(url)
148 .send()?;
149 let out = T::default();
152 Ok(out)
154}