Skip to content

Commit

Permalink
Merge pull request #2 from rruckley/CommonTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
rruckley authored Mar 30, 2024
2 parents 7721b43 + dbdb460 commit 0074662
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 207 deletions.
Binary file added .DS_Store
Binary file not shown.
399 changes: 237 additions & 162 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "tmf-client"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Ryan Ruckley <[email protected]"]
description = "A Rust client library for TMF conformant APIs"

[dependencies]
env_logger = "0.11.1"
reqwest = {version = "0.11.24", features = ["blocking"]}
serde = "1.0.196"
serde_json = "1.0.113"
env_logger = "0.11.3"
reqwest = {version = "0.12.2", features = ["blocking"]}
serde = "1.0"
serde_json = "1.0"
tmflib = { git = "https://github.com/rruckley/tmflib.git"}
2 changes: 1 addition & 1 deletion examples/get_catalog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Get Catalog Example
use tmf_client::{TMFClient,QueryOptions};
use tmf_client::{TMFClient,QueryOptions,Operations};
use tmflib::HasName;

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/get_product_offering.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Product Offering Example
use tmf_client::{TMFClient,QueryOptions};
use tmf_client::{TMFClient,QueryOptions,Operations};
use tmflib::{HasId,HasName};

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/get_product_offering_price.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Product Offering Example
use tmf_client::{TMFClient,QueryOptions};
use tmf_client::{TMFClient,QueryOptions,Operations};
use tmflib::{HasId,HasName};

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/get_product_specification.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Product Offering Example
use tmf_client::{TMFClient,QueryOptions};
use tmf_client::{TMFClient,QueryOptions,Operations};
use tmflib::{HasId,HasName};

fn main() {
Expand Down
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
pub mod tmf;
pub mod common;

use common::tmf_error::TMFError;
use tmf::tmf620::TMF620;
use tmf::tmf622::TMF622;

use tmflib::HasId;

/// Fields for filtering output
#[derive(Clone, Default, Debug)]
pub struct QueryOptions {
Expand Down Expand Up @@ -57,6 +60,40 @@ impl From<QueryOptions> for String {
}
}

pub trait Operations {
type TMF : HasId;

/// Get a specific tmf object by Id
/// ```
/// # use tmf_client::TMFClient;
/// let categories = TMFClient::new("http://localhost: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;
/// let categories = TMFClient::new("http://localhost:8000")
/// .tmf620()
/// .category()
/// .list(None);
/// ```
fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Self::TMF>,TMFError>;
fn create(&self, item : Self::TMF) -> Result<Self::TMF,TMFError>;
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;
/// let categories = TMFClient::new("http://localhost:8000")
/// .tmf620()
/// .category()
/// .get("ID123");
/// ```
fn delete(&self, id : impl Into<String>) -> Result<Self::TMF,TMFError>;
}

pub struct TMFClient {
host : String,
tmf620 : Option<TMF620>,
Expand Down
130 changes: 94 additions & 36 deletions src/tmf/tmf620.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! TMF620 Product Catalogue

use tmflib::tmf620::catalog::Catalog;
use tmflib::tmf620::category::Category;
use tmflib::tmf620::product_offering::ProductOffering;
Expand All @@ -9,7 +10,7 @@ use tmflib::Uri;
use super::{get_tmf,list_tmf};
use crate::common::tmf_error::TMFError;

use crate::QueryOptions;
use crate::{QueryOptions,Operations};

/// TMF620 Category API calls
#[derive(Clone,Default,Debug)]
Expand All @@ -26,19 +27,32 @@ impl TMF620Category {
pub fn get(&self, _id : impl Into<String>) -> Result<Vec<Category>,TMFError> {
get_tmf(self.host.clone(),_id.into())
}
/// Get a list of catalogs applying optional filter
/// ```
/// # use tmf_client::TMFClient;
/// let categories = TMFClient::new("http://localhost:8000")
/// .tmf620()
/// .category()
/// .list(None);
/// ```

pub fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Category>,TMFError> {
list_tmf(self.host.clone(),filter)
}
}

impl Operations for TMF620Category {
type TMF = Category;

fn create(&self, _item : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn delete(&self, _id : impl Into<String>) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn get(&self, id : impl Into<String>) -> Result<Vec<Self::TMF>,TMFError> {
get_tmf(self.host.clone(),id.into())
}
fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Self::TMF>,TMFError> {
list_tmf(self.host.clone(),filter)
}
fn update(&self, _id : impl Into<String>, _patch : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
}

/// TMF620 Catalog API calls
#[derive(Clone,Default,Debug)]
pub struct TMF620Catalog {
Expand All @@ -50,16 +64,30 @@ impl TMF620Catalog {
pub fn new(host : Uri) -> TMF620Catalog {
TMF620Catalog { host }
}
/// Get a single catalog entry
pub fn get(&self, _id : impl Into<String>) -> Result<Vec<Catalog>,TMFError> {
get_tmf(self.host.clone(),_id.into())
}

impl Operations for TMF620Catalog {
type TMF = Catalog;

fn create(&self, _item : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
/// Get a list of catalogs applying optional filter
pub fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Catalog>,TMFError> {
list_tmf(self.host.clone(),filter)
fn delete(&self, _id : impl Into<String>) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn get(&self, id : impl Into<String>) -> Result<Vec<Self::TMF>,TMFError> {
get_tmf(self.host.clone(),id.into())
}
fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Self::TMF>,TMFError> {
list_tmf(self.host.clone(),filter)
}
fn update(&self, _id : impl Into<String>, _patch : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
}



/// TMF620 ProductOffering API calls
#[derive(Clone,Default,Debug)]
pub struct TMF620ProductOffering {
Expand All @@ -71,15 +99,25 @@ impl TMF620ProductOffering {
pub fn new(host : Uri) -> TMF620ProductOffering {
TMF620ProductOffering { host }
}
}

/// Get a single product offering
pub fn get(&self, _id : impl Into<String>) -> Result<Vec<ProductOffering>,TMFError> {
get_tmf(self.host.clone(),_id.into())
}
impl Operations for TMF620ProductOffering {
type TMF = ProductOffering;

/// Get a list of catalogs applying optional filter
pub fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<ProductOffering>,TMFError> {
list_tmf(self.host.clone(),filter)
fn create(&self, _item : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn delete(&self, _id : impl Into<String>) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn get(&self, id : impl Into<String>) -> Result<Vec<Self::TMF>,TMFError> {
get_tmf(self.host.clone(),id.into())
}
fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Self::TMF>,TMFError> {
list_tmf(self.host.clone(),filter)
}
fn update(&self, _id : impl Into<String>, _patch : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
}

Expand All @@ -94,15 +132,25 @@ impl TMF620ProductOfferingPrice {
pub fn new(host : Uri) -> TMF620ProductOfferingPrice {
TMF620ProductOfferingPrice { host }
}
}

/// Get a single product offering
pub fn get(&self, _id : impl Into<String>) -> Result<Vec<ProductOfferingPrice>,TMFError> {
get_tmf(self.host.clone(),_id.into())
}
impl Operations for TMF620ProductOfferingPrice {
type TMF = ProductOfferingPrice;

/// Get a list of catalogs applying optional filter
pub fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<ProductOfferingPrice>,TMFError> {
list_tmf(self.host.clone(),filter)
fn create(&self, _item : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn delete(&self, _id : impl Into<String>) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn get(&self, id : impl Into<String>) -> Result<Vec<Self::TMF>,TMFError> {
get_tmf(self.host.clone(),id.into())
}
fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Self::TMF>,TMFError> {
list_tmf(self.host.clone(),filter)
}
fn update(&self, _id : impl Into<String>, _patch : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
}

Expand All @@ -117,15 +165,25 @@ impl TMF620ProductSpecification {
pub fn new(host : Uri) -> TMF620ProductSpecification {
TMF620ProductSpecification { host }
}
}

/// Get a single product offering
pub fn get(&self, _id : impl Into<String>) -> Result<Vec<ProductSpecification>,TMFError> {
get_tmf(self.host.clone(),_id.into())
}
impl Operations for TMF620ProductSpecification {
type TMF = ProductSpecification;

/// Get a list of catalogs applying optional filter
pub fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<ProductSpecification>,TMFError> {
list_tmf(self.host.clone(),filter)
fn create(&self, _item : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn delete(&self, _id : impl Into<String>) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
fn get(&self, id : impl Into<String>) -> Result<Vec<Self::TMF>,TMFError> {
get_tmf(self.host.clone(),id.into())
}
fn list(&self, filter : Option<QueryOptions>) -> Result<Vec<Self::TMF>,TMFError> {
list_tmf(self.host.clone(),filter)
}
fn update(&self, _id : impl Into<String>, _patch : Self::TMF) -> Result<Self::TMF,TMFError> {
Err(TMFError::from("Not implemented"))
}
}

Expand Down

0 comments on commit 0074662

Please sign in to comment.