tmf_client/tmf/
mod.rs

1//! TMF Modules
2//! 
3
4use std::io::Read;
5
6use crate::QueryOptions;
7use crate::common::tmf_error::TMFError;
8use tmflib::{HasId,Uri};
9use serde::{de::DeserializeOwned, Serialize};
10// use log::info;
11
12#[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
39/// Make API call to retrieve a single TMF object
40pub fn get_tmf<T : HasId + DeserializeOwned>(host: Uri, id : String) -> Result<Vec<T>,TMFError> {
41    // Return results
42    let url = format!("{}{}/{}",host,T::get_class_href(),id);
43    let insecure = false;
44    #[cfg(feature = "insecure")]
45    let insecure = true; // For testing purposes only, do not use in production
46    
47    let client = reqwest::blocking::Client::builder()
48        .danger_accept_invalid_certs(insecure)// For testing purposes only, do not use in production
49        .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
61/// Make API call to retrieve a set of TMF objects according to filter
62pub fn list_tmf<T : HasId + DeserializeOwned>(host: Uri, filter : Option<QueryOptions>) -> Result<Vec<T>,TMFError> {
63    // Return results
64    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; // For testing purposes only, do not use in production
72    let client = reqwest::blocking::Client::builder()
73        .danger_accept_invalid_certs(insecure) // For testing purposes only, do not use in production
74        .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
86/// Create a new TMF object
87pub 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; // For testing purposes only, do not use in production
92    let client = reqwest::blocking::Client::builder()
93        .danger_accept_invalid_certs(insecure) // For testing purposes only, do not use in production
94        .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
113/// Update an existing TMF object
114pub 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; // For testing purposes only, do not use in production
119        let client = reqwest::blocking::Client::builder()
120        .danger_accept_invalid_certs(insecure) // For testing purposes only, do not use in production
121        .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
135/// Delete an existing TMF object
136pub 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; // For testing purposes only, do not use in production
141    let client = reqwest::blocking::Client::builder()
142        .danger_accept_invalid_certs(insecure) // For testing purposes only, do not use in production
143        .use_rustls_tls()
144        .user_agent(USER_AGENT)
145        .build()?;
146    
147    let mut _res = client.delete(url)
148        .send()?;
149    // Return empty object for now to avoid
150    // round trip to retrieve object
151    let out = T::default();
152    // out.set_id(id);
153    Ok(out)
154}