Skip to main content

rutmf/api/
tmf666.rs

1//! **TMF666 Account Management v5.0.0** — the client.
2//!
3//! Covers the four account collections — `billingAccount`, `financialAccount`,
4//! `partyAccount`, `settlementAccount` — plus `billFormat`,
5//! `billPresentationMedia` and `billingCycleSpecification`, and the `hub`.
6//!
7//! All four account collections serve the same Rust type. TMF666 models
8//! `Account` as one family with four `@type`-discriminated subclasses and then
9//! gives each its own path, so the methods differ while the shape does not —
10//! ask an [`Account`] which subclass it is with
11//! [`kind`](crate::account::Account::kind).
12
13use crate::account::{
14    Account, AccountCreate, AccountUpdate, BillFormat, BillFormatCreate, BillFormatUpdate,
15    BillPresentationMedia, BillPresentationMediaCreate, BillPresentationMediaUpdate,
16    BillingCycleSpecification, BillingCycleSpecificationCreate, BillingCycleSpecificationUpdate,
17};
18
19use super::client::TmfClient;
20use super::error::Result;
21use super::hub::HubOps;
22use super::page::{Page, PageRequest, PageStream, paginate};
23use super::patch::Patch;
24use super::query::Query;
25use super::resource_ops;
26use super::transport::Transport;
27use super::{Api, Collection, Operation};
28
29/// The conventional API root, appended to a host to form the base URL.
30pub const API_PATH: &str = "tmf-api/accountManagement/v5";
31
32/// The specification version this client was modelled from.
33///
34/// The covered APIs are not all on the same patch release, so this is per-API
35/// rather than a single crate-wide constant. Asserted against the vendored
36/// corpus by `every_client_reports_the_version_it_was_modelled_from`.
37pub const SPEC_VERSION: &str = "5.0.0";
38
39/// What TMF666 declares: every collection, including `hub`, with the
40/// operations on each.
41///
42/// [`TmfHandler`](crate::server::TmfHandler) refuses anything else, and
43/// `tests/coverage.rs` holds this equal to the specification document.
44pub const API: Api = Api::new(
45    "TMF666",
46    API_PATH,
47    SPEC_VERSION,
48    &[
49        Collection::crud("billFormat"),
50        Collection::crud("billPresentationMedia"),
51        Collection::crud("billingAccount"),
52        Collection::crud("billingCycleSpecification"),
53        Collection::crud("financialAccount"),
54        Collection::crud("partyAccount"),
55        Collection::crud("settlementAccount"),
56        Collection::new("hub", &[Operation::Create, Operation::Delete]),
57    ],
58);
59
60const BILLING: &str = "billingAccount";
61const FINANCIAL: &str = "financialAccount";
62const PARTY: &str = "partyAccount";
63const SETTLEMENT: &str = "settlementAccount";
64const FORMATS: &str = "billFormat";
65const MEDIA: &str = "billPresentationMedia";
66const CYCLES: &str = "billingCycleSpecification";
67
68/// A client for TMF666 Account Management.
69///
70/// ```no_run
71/// use rutmf::account::AccountKind;
72/// use rutmf::api::{Query, Transport, tmf666::AccountClient};
73///
74/// # async fn demo(transport: impl Transport + 'static) -> rutmf::api::Result<()> {
75/// let client = AccountClient::new("https://mycsp.com/tmf-api/accountManagement/v5", transport)?;
76///
77/// let page = client.list_billing_accounts(&Query::new().limit(20)).await?;
78/// for account in page {
79///     assert_eq!(account.kind(), AccountKind::Billing);
80///     println!("{:?}", account.name);
81/// }
82/// # Ok(())
83/// # }
84/// ```
85#[derive(Debug, Clone)]
86pub struct AccountClient {
87    inner: TmfClient,
88}
89
90impl AccountClient {
91    /// Creates a client for `base_url`, dispatching through `transport`.
92    ///
93    /// # Errors
94    ///
95    /// Returns [`Error::InvalidBaseUrl`] if the URL is empty.
96    ///
97    /// [`Error::InvalidBaseUrl`]: super::Error::InvalidBaseUrl
98    pub fn new(base_url: impl Into<String>, transport: impl Transport + 'static) -> Result<Self> {
99        Ok(Self {
100            inner: TmfClient::new(base_url, transport)?,
101        })
102    }
103
104    /// Creates a client for `host`, appending the conventional [`API_PATH`].
105    ///
106    /// # Errors
107    ///
108    /// Returns [`Error::InvalidBaseUrl`] if the host is empty.
109    ///
110    /// [`Error::InvalidBaseUrl`]: super::Error::InvalidBaseUrl
111    pub fn from_host(host: &str, transport: impl Transport + 'static) -> Result<Self> {
112        Self::new(
113            format!("{}/{API_PATH}", host.trim_end_matches('/')),
114            transport,
115        )
116    }
117
118    /// The underlying generic client, for operations this API does not wrap.
119    #[must_use]
120    pub fn inner(&self) -> &TmfClient {
121        &self.inner
122    }
123
124    resource_ops!(
125        BILLING,
126        read = Account,
127        create = AccountCreate,
128        update = AccountUpdate,
129        list = list_billing_accounts,
130        stream = stream_billing_accounts,
131        get = get_billing_account,
132        new = create_billing_account,
133        patch = update_billing_account,
134        delete = delete_billing_account,
135        doc = "a billing account"
136    );
137
138    resource_ops!(
139        FINANCIAL,
140        read = Account,
141        create = AccountCreate,
142        update = AccountUpdate,
143        list = list_financial_accounts,
144        stream = stream_financial_accounts,
145        get = get_financial_account,
146        new = create_financial_account,
147        patch = update_financial_account,
148        delete = delete_financial_account,
149        doc = "a financial account"
150    );
151
152    resource_ops!(
153        PARTY,
154        read = Account,
155        create = AccountCreate,
156        update = AccountUpdate,
157        list = list_party_accounts,
158        stream = stream_party_accounts,
159        get = get_party_account,
160        new = create_party_account,
161        patch = update_party_account,
162        delete = delete_party_account,
163        doc = "a party account"
164    );
165
166    resource_ops!(
167        SETTLEMENT,
168        read = Account,
169        create = AccountCreate,
170        update = AccountUpdate,
171        list = list_settlement_accounts,
172        stream = stream_settlement_accounts,
173        get = get_settlement_account,
174        new = create_settlement_account,
175        patch = update_settlement_account,
176        delete = delete_settlement_account,
177        doc = "a settlement account"
178    );
179
180    resource_ops!(
181        FORMATS,
182        read = BillFormat,
183        create = BillFormatCreate,
184        update = BillFormatUpdate,
185        list = list_bill_formats,
186        stream = stream_bill_formats,
187        get = get_bill_format,
188        new = create_bill_format,
189        patch = update_bill_format,
190        delete = delete_bill_format,
191        doc = "a bill format"
192    );
193
194    resource_ops!(
195        MEDIA,
196        read = BillPresentationMedia,
197        create = BillPresentationMediaCreate,
198        update = BillPresentationMediaUpdate,
199        list = list_presentation_media,
200        stream = stream_presentation_media,
201        get = get_presentation_medium,
202        new = create_presentation_medium,
203        patch = update_presentation_medium,
204        delete = delete_presentation_medium,
205        doc = "a bill presentation medium"
206    );
207
208    resource_ops!(
209        CYCLES,
210        read = BillingCycleSpecification,
211        create = BillingCycleSpecificationCreate,
212        update = BillingCycleSpecificationUpdate,
213        list = list_billing_cycle_specifications,
214        stream = stream_billing_cycle_specifications,
215        get = get_billing_cycle_specification,
216        new = create_billing_cycle_specification,
217        patch = update_billing_cycle_specification,
218        delete = delete_billing_cycle_specification,
219        doc = "a billing cycle specification"
220    );
221}
222
223impl HubOps for AccountClient {
224    fn hub_client(&self) -> &TmfClient {
225        &self.inner
226    }
227}