tmf-client 0.1.13

A Rust client library for TMF conformant APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! # TMF Client Library
//!
//! ## Description
//! Interact with TMF compliant APIs using an SDK
//!
//! ## Supported TMF APIs
//!
//! Currently supports:
//!
//! - TMF620 Product Catalog Management
//! - TMF622 Product Order Management
//! - TMF629 Customer Management
//! - TMF632 Party Management
//! - TMF633 Service Catalog Management
//! - TMF637 Product Inventory Management
//! - TMF638 Service Inventory Management
//! - TMF639 Resource Inventory Management
//! - TMF645 Service Qualification Management
//! - TMF648 Quote Management
//! - TMF663 Shopping Cart Management
//! - TMF674 Geographic Site Management
//!
//! ## Features
//! All TMF APIs can be conditionally compiled. Deafult includes all APIs using V4 specifications.

#![warn(missing_docs)]

pub mod common;
pub mod tmf;

use common::tmf_error::TMFError;
#[cfg(feature = "tmf620")]
use tmf::tmf620::TMF620;
#[cfg(feature = "tmf622")]
use tmf::tmf622::TMF622;
#[cfg(feature = "tmf629")]
use tmf::tmf629::TMF629;
#[cfg(feature = "tmf632")]
use tmf::tmf632::TMF632;
#[cfg(feature = "tmf633")]
use tmf::tmf633::TMF633;
#[cfg(feature = "tmf637")]
use tmf::tmf637::TMF637;
#[cfg(feature = "tmf638")]
use tmf::tmf638::TMF638;
#[cfg(feature = "tmf639")]
use tmf::tmf639::TMF639;
#[cfg(feature = "tmf645")]
use tmf::tmf645::TMF645;
#[cfg(feature = "tmf648")]
use tmf::tmf648::TMF648;
#[cfg(feature = "tmf663")]
use tmf::tmf663::TMF663;
#[cfg(feature = "tmf674")]
use tmf::tmf674::TMF674;

use tmflib::{HasId, Uri};

#[cfg(feature = "insecure")]
const INSECURE: bool = true;
#[cfg(not(feature = "insecure"))]
const INSECURE: bool = false;

/// Default port for the TMF API
pub const DEFAULT_PORT: u16 = 8001;

/// Configuration for the TMF Client
#[derive(Clone, Debug, Default)]
pub struct Config {
    /// The host URI for the TMF API  
    pub host: Uri,
    /// The port to use for the TMF API
    ///
    pub port: u16,
    /// The base path for the TMF API
    pub insecure: bool,
}

impl Config {
    /// Create a new configuration for the TMF Client
    /// ```
    /// # use tmf_client::Config;
    /// let config = Config::new("http://localhost", 8000);
    /// ```
    pub fn new(host: impl Into<String>, port: u16) -> Config {
        Config {
            host: Uri::from(host.into()),
            port,
            insecure: INSECURE,
        }
    }
}

/// Fields for filtering output
#[derive(Clone, Default, Debug)]
pub struct QueryOptions {
    /// Specific set of fields delimited by comma
    pub fields: Option<String>,
    /// Limit the number of results returned
    pub limit: Option<u16>,
    /// Offset the results returned
    pub offset: Option<u16>,
    /// Filter on name
    pub name: Option<String>,
}

impl QueryOptions {
    /// Setting the field on an existing QueryOptions
    /// ```
    /// # use tmf_client::QueryOptions;
    /// let opt = QueryOptions::default()
    ///     .fields("id,name,description".to_string());
    /// ```
    pub fn fields(mut self, fields: String) -> QueryOptions {
        self.fields = Some(fields);
        self
    }
    /// Set the limit on the number of results returned
    /// ```
    /// # use tmf_client::QueryOptions;
    /// let opt = QueryOptions::default()
    ///     .limit(10);
    /// ```
    pub fn limit(mut self, limit: u16) -> QueryOptions {
        self.limit = Some(limit);
        self
    }

    /// Set the offset on the number of results returned
    /// ```
    /// # use tmf_client::QueryOptions;
    /// let opt = QueryOptions::default()
    ///     .offset(5);
    /// ```
    pub fn offset(mut self, offset: u16) -> QueryOptions {
        self.offset = Some(offset);
        self
    }

    /// Set the name to filter on
    /// ```
    /// # use tmf_client::QueryOptions;
    /// let opt = QueryOptions::default()
    ///     .name("MyService".to_string());
    /// ```
    /// This will filter the results to only include those with the specified name.
    /// If the name is not set, it will not filter on name.
    ///
    pub fn name(mut self, name: impl Into<String>) -> QueryOptions {
        self.name = Some(name.into());
        self
    }
}

impl From<QueryOptions> for String {
    fn from(val: QueryOptions) -> Self {
        let limit = match val.limit {
            Some(l) => format!("limit={l}"),
            None => String::default(),
        };
        let offset = match val.offset {
            Some(o) => format!("offset={o}"),
            None => String::default(),
        };
        let name = match val.name {
            Some(n) => format!("name={n}"),
            None => String::default(),
        };
        format!("{limit}&{offset}&{name}")
    }
}

/// Standard set of operations for all TMF objects
#[cfg(feature = "blocking")]
pub trait BlockingOperations {
    /// The TMF object type that this trait operates on
    type TMF: HasId;

    /// Get a specific TMF object by Id
    /// ```
    /// # use tmf_client::{TMFClient,BlockingOperations};
    /// let categories = TMFClient::new("http://localhost",Some(8000))
    ///     .tmf620()
    ///     .category()
    ///     .get("ID123");
    /// ```
    fn get(&self, id: impl Into<String>) -> Result<Vec<Self::TMF>, TMFError>;
    /// Get a list of tmf objects applying optional filter
    /// ```
    /// # use tmf_client::{TMFClient,QueryOptions,BlockingOperations};
    /// let filter = QueryOptions::default()
    ///     .limit(15)
    ///     .offset(10);
    /// let categories = TMFClient::new("http://localhost",Some(8000))
    ///     .tmf620()
    ///     .category()
    ///     .list(Some(filter));
    /// ```
    fn list(&self, filter: Option<QueryOptions>) -> Result<Vec<Self::TMF>, TMFError>;
    /// Create a new instance of a TMF object
    fn create(&self, item: Self::TMF) -> Result<Self::TMF, TMFError>;
    /// Update an existing TMF Object using the provided patch object
    fn update(&self, id: impl Into<String>, patch: Self::TMF) -> Result<Self::TMF, TMFError>;
    /// Delete a specific tmf object by Id
    /// ```
    /// # use tmf_client::{TMFClient,BlockingOperations};
    /// let categories = TMFClient::new("http://localhost",None)
    ///     .tmf620()
    ///     .category()
    ///     .delete("ID123");
    /// ```
    fn delete(&self, id: impl Into<String>) -> Result<Self::TMF, TMFError>;
}

/// Standard set of operations for all TMF objects
#[cfg(not(feature = "blocking"))]
pub trait AsyncOperations {
    /// The TMF object type that this trait operates on
    type TMF: HasId;

    /// Get a specific TMF object by Id
    fn get(
        &self,
        id: impl Into<String>,
    ) -> impl std::future::Future<Output = Result<Vec<Self::TMF>, TMFError>>;
    /// Get a list of tmf objects applying optional filter
    fn list(
        &self,
        filter: Option<QueryOptions>,
    ) -> impl std::future::Future<Output = Result<Vec<Self::TMF>, TMFError>>;
    /// Create a new instance of a TMF object
    fn create(
        &self,
        item: Self::TMF,
    ) -> impl std::future::Future<Output = Result<Self::TMF, TMFError>>;
    /// Update an existing TMF Object using the provided patch object
    fn update(
        &self,
        id: impl Into<String>,
        patch: Self::TMF,
    ) -> impl std::future::Future<Output = Result<Self::TMF, TMFError>>;
    /// Delete a specific tmf object by Id
    fn delete(
        &self,
        id: impl Into<String>,
    ) -> impl std::future::Future<Output = Result<Self::TMF, TMFError>>;
}

/// Trait to create a new instance of a TMF object
#[allow(clippy::new_ret_no_self)]
pub trait HasNew<T: Clone> {
    /// Create a new instance of the TMF object passin in the destination host Uri
    fn new(config: Config) -> T;
}

/// TMF Client
pub struct TMFClient {
    config: Config,
    #[cfg(feature = "tmf620")]
    tmf620: Option<TMF620>,
    #[cfg(feature = "tmf622")]
    tmf622: Option<TMF622>,
    #[cfg(feature = "tmf629")]
    tmf629: Option<TMF629>,
    #[cfg(feature = "tmf632")]
    tmf632: Option<TMF632>,
    #[cfg(feature = "tmf633")]
    tmf633: Option<TMF633>,
    #[cfg(feature = "tmf637")]
    tmf637: Option<TMF637>,
    #[cfg(feature = "tmf638")]
    tmf638: Option<TMF638>,
    #[cfg(feature = "tmf639")]
    tmf639: Option<TMF639>,
    #[cfg(feature = "tmf645")]
    tmf645: Option<TMF645>,
    #[cfg(feature = "tmf648")]
    tmf648: Option<TMF648>,
    #[cfg(feature = "tmf663")]
    tmf663: Option<TMF663>,
    #[cfg(feature = "tmf674")]
    tmf674: Option<TMF674>,
}

// Create a new instance
fn instantiate<T: Clone + HasNew<T>>(api: &mut Option<T>, config: Config) -> T {
    match api {
        Some(instance) => instance.clone(),
        None => {
            let new_api = T::new(config);
            api.replace(new_api.clone());
            new_api
        }
    }
}

impl TMFClient {
    /// Create a new TMFClient instance
    /// ```
    /// # use tmf_client::TMFClient;
    /// let client = TMFClient::new("http://localhost",Some(8000));
    /// ```
    pub fn new(host: impl Into<String>, port: Option<u16>) -> TMFClient {
        TMFClient {
            config: Config::new(host, port.unwrap_or(DEFAULT_PORT)),
            #[cfg(feature = "tmf620")]
            tmf620: None,
            #[cfg(feature = "tmf622")]
            tmf622: None,
            #[cfg(feature = "tmf629")]
            tmf629: None,
            #[cfg(feature = "tmf632")]
            tmf632: None,
            #[cfg(feature = "tmf633")]
            tmf633: None,
            #[cfg(feature = "tmf637")]
            tmf637: None,
            #[cfg(feature = "tmf638")]
            tmf638: None,
            #[cfg(feature = "tmf639")]
            tmf639: None,
            #[cfg(feature = "tmf645")]
            tmf645: None,
            #[cfg(feature = "tmf648")]
            tmf648: None,
            #[cfg(feature = "tmf663")]
            tmf663: None,
            #[cfg(feature = "tmf674")]
            tmf674: None,
        }
    }

    /// Create access to TMF620 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf620 = TMFClient::new("http://localhost",None)
    ///     .tmf620();
    /// ```
    #[cfg(feature = "tmf620")]
    pub fn tmf620(&mut self) -> TMF620 {
        // // let instance =
        let config = self.config.clone();
        let instance: TMF620 = instantiate(&mut self.tmf620, config);
        self.tmf620.replace(instance.clone());
        instance
    }

    /// Create access to TMF622 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf620 = TMFClient::new("http://localhost",None)
    ///     .tmf622();
    /// ```
    #[cfg(feature = "tmf622")]
    pub fn tmf622(&mut self) -> TMF622 {
        instantiate(&mut self.tmf622, self.config.clone())
    }

    /// Create access to TMF632 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf632 = TMFClient::new("http://localhost",None)
    ///     .tmf629();
    /// ```
    #[cfg(feature = "tmf629")]
    pub fn tmf629(&mut self) -> TMF629 {
        instantiate(&mut self.tmf629, self.config.clone())
    }

    /// Create access to TMF632 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf632 = TMFClient::new("http://localhost",None)
    ///     .tmf632();
    /// ```
    #[cfg(feature = "tmf632")]
    pub fn tmf632(&mut self) -> TMF632 {
        instantiate(&mut self.tmf632, self.config.clone())
    }

    /// Create access to TMF633 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf633 = TMFClient::new("http://localhost",None)
    ///     .tmf633();
    /// ```
    #[cfg(feature = "tmf633")]
    pub fn tmf633(&mut self) -> TMF633 {
        instantiate(&mut self.tmf633, self.config.clone())
    }

    /// Create access to TMF637 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf637 = TMFClient::new("http://localhost",None)
    ///     .tmf637();
    /// ```
    #[cfg(feature = "tmf637")]
    pub fn tmf637(&mut self) -> TMF637 {
        instantiate(&mut self.tmf637, self.config.clone())
    }

    /// Create access to TMF638 API
    /// ```
    /// # use tmf_client::TMFClient;                
    /// let tmf638 = TMFClient::new("http://localhost",None)
    ///     .tmf638();
    /// ```
    #[cfg(feature = "tmf638")]
    pub fn tmf638(&mut self) -> TMF638 {
        instantiate(&mut self.tmf638, self.config.clone())
    }

    /// Create access to TMF639 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf639 = TMFClient::new("http://localhost",None)
    ///     .tmf639();
    /// ```
    #[cfg(feature = "tmf639")]
    pub fn tmf639(&mut self) -> TMF639 {
        instantiate(&mut self.tmf639, self.config.clone())
    }

    /// Create access to TMF645 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf645 = TMFClient::new("http://localhost",None)
    ///     .tmf645();
    /// ```
    #[cfg(feature = "tmf645")]
    pub fn tmf645(&mut self) -> TMF645 {
        instantiate(&mut self.tmf645, self.config.clone())
    }

    /// Create access to TMF648 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf648 = TMFClient::new("http://localhost",None)
    ///     .tmf648();
    /// ```
    #[cfg(feature = "tmf648")]
    pub fn tmf648(&mut self) -> TMF648 {
        instantiate(&mut self.tmf648, self.config.clone())
    }

    /// Create access to TMF663 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf663 = TMFClient::new("http://localhost",None)
    ///     .tmf663();
    /// ```
    #[cfg(feature = "tmf663")]
    pub fn tmf663(&mut self) -> TMF663 {
        instantiate(&mut self.tmf663, self.config.clone())
    }

    /// Create access to TMF674 API
    /// ```
    /// # use tmf_client::TMFClient;
    /// let tmf674 = TMFClient::new("http://localhost",None)
    ///     .tmf674();
    /// ```
    #[cfg(feature = "tmf674")]
    pub fn tmf674(&mut self) -> TMF674 {
        instantiate(&mut self.tmf674, self.config.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_filter_limit() {
        let filter = QueryOptions::default().limit(111);

        assert_eq!(filter.limit, Some(111));
    }

    #[test]
    fn test_filter_offset() {
        let filter = QueryOptions::default().offset(222);

        assert_eq!(filter.offset, Some(222));
    }
}