Skip to content

Commit d46e8ca

Browse files
committed
Implement builder pattern for AppConfigurationClient
Signed-off-by: Javier G. Sogo <[email protected]>
1 parent df53044 commit d46e8ca

File tree

5 files changed

+90
-21
lines changed

5 files changed

+90
-21
lines changed

examples/demo.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use std::{collections::HashMap, env, thread, time::Duration};
1616

1717
use appconfiguration_rust_sdk::{
18-
AppConfigurationClient, AttrValue, Entity, Feature, Property, Value,
18+
AppConfigClientBuilder, AppConfigIBMCloudBuilder, AttrValue, Entity, Feature, Property, Value,
1919
};
2020
use dotenvy::dotenv;
2121
use std::error::Error;
@@ -42,16 +42,17 @@ impl Entity for CustomerEntity {
4242

4343
fn main() -> std::result::Result<(), Box<dyn Error>> {
4444
dotenv().ok();
45-
let region = env::var("REGION").expect("REGION should be set.");
46-
let guid = env::var("GUID").expect("GUID should be set.");
47-
let apikey = env::var("APIKEY").expect("APIKEY should be set.");
48-
let collection_id = env::var("COLLECTION_ID").expect("COLLECTION_ID should be set.");
49-
let environment_id = env::var("ENVIRONMENT_ID").expect("ENVIRONMENT_ID should be set.");
50-
let feature_id = env::var("FEATURE_ID").expect("FEATURE_ID should be set.");
51-
let property_id = env::var("PROPERTY_ID").expect("PROPERTY_ID should be set.");
45+
let region = env::var("REGION").or(Err("'REGION' envvar is required."))?;
46+
let guid = env::var("GUID").or(Err("'GUID' envvar should be set."))?;
47+
let apikey = env::var("APIKEY").or(Err("'APIKEY' envvar should be set."))?;
48+
let collection_id = env::var("COLLECTION_ID").or(Err("'COLLECTION_ID' envvar should be set."))?;
49+
let environment_id = env::var("ENVIRONMENT_ID").or(Err("'ENVIRONMENT_ID' envvar should be set."))?;
50+
let feature_id = env::var("FEATURE_ID").or(Err("'FEATURE_ID' envvar should be set."))?;
51+
let property_id = env::var("PROPERTY_ID").or(Err("'PROPERTY_ID' envvar should be set."))?;
5252

5353
let client =
54-
AppConfigurationClient::new(&apikey, &region, &guid, &environment_id, &collection_id)?;
54+
AppConfigIBMCloudBuilder::new(&region, &apikey, &guid, &environment_id, &collection_id)
55+
.build()?;
5556

5657
let entity = CustomerEntity {
5758
id: "user123".to_string(),

src/builders/mod.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// (C) Copyright IBM Corp. 2024.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::AppConfigurationClient;
16+
use crate::Result;
17+
18+
pub trait AppConfigClientBuilder {
19+
/// Creates and returns the [`AppConfigurationClient`]
20+
fn build(self) -> Result<AppConfigurationClient>;
21+
}
22+
23+
/// An [`AppConfigurationClient`] builder to create a client connecting to IBM Cloud
24+
pub struct AppConfigIBMCloudBuilder<'a> {
25+
region: &'a str,
26+
apikey: &'a str,
27+
guid: &'a str,
28+
environment_id: &'a str,
29+
collection_id: &'a str,
30+
}
31+
32+
impl<'a> AppConfigIBMCloudBuilder<'a> {
33+
/// Creates a builder with the data required to instantiate a new [`AppConfigClientBuilder`]
34+
/// connecting to IBM Cloud
35+
///
36+
/// # Arguments
37+
///
38+
/// * `region` - Region name where the App Configuration service instance is created
39+
/// * `apikey` - The encrypted API key.
40+
/// * `guid` - Instance ID of the App Configuration service. Obtain it from the service credentials section of the App Configuration dashboard
41+
/// * `collection_id` - ID of the collection created in App Configuration service instance under the Collections section
42+
/// * `environment_id` - ID of the environment created in App Configuration service instance under the Environments section.
43+
pub fn new(
44+
region: &'a str,
45+
apikey: &'a str,
46+
guid: &'a str,
47+
environment_id: &'a str,
48+
collection_id: &'a str,
49+
) -> Self {
50+
Self {
51+
region,
52+
apikey,
53+
guid,
54+
environment_id,
55+
collection_id,
56+
}
57+
}
58+
}
59+
60+
impl<'a> AppConfigClientBuilder for AppConfigIBMCloudBuilder<'a> {
61+
fn build(self) -> Result<AppConfigurationClient> {
62+
AppConfigurationClient::new(
63+
self.apikey,
64+
self.region,
65+
self.guid,
66+
self.environment_id,
67+
self.collection_id,
68+
)
69+
}
70+
}

src/client/app_configuration_client.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,7 @@ pub struct AppConfigurationClient {
3838
}
3939

4040
impl AppConfigurationClient {
41-
/// Creates a client to retrieve configurations for a specific collection.
42-
/// To uniquely address a collection the following is required:
43-
/// - `region`
44-
/// - `guid`: Identifies an instance
45-
/// - `environment_id`
46-
/// - `collection_id`
47-
/// In addition `api_key` is required for authentication
48-
pub fn new(
41+
pub(crate) fn new(
4942
apikey: &str,
5043
region: &str,
5144
guid: &str,

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
mod builders;
1516
mod client;
1617
mod entity;
1718
mod errors;
@@ -21,12 +22,13 @@ mod property;
2122
mod segment_evaluation;
2223
mod value;
2324

25+
pub use builders::{AppConfigClientBuilder, AppConfigIBMCloudBuilder};
2426
pub use client::AppConfigurationClient;
25-
pub use entity::{Entity, AttrValue};
27+
pub use entity::{AttrValue, Entity};
28+
pub use errors::{Error, Result};
2629
pub use feature::Feature;
2730
pub use property::Property;
2831
pub use value::Value;
29-
pub use errors::{Result, Error};
3032

3133
#[cfg(test)]
3234
mod tests;

tests/test_app_config.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use dotenvy::dotenv;
1616
use rstest::*;
1717

1818
use appconfiguration_rust_sdk::{
19-
AppConfigurationClient, AttrValue, Entity, Feature, Property, Value,
19+
AppConfigClientBuilder, AppConfigIBMCloudBuilder, AppConfigurationClient, AttrValue, Entity,
20+
Feature, Property, Value,
2021
};
2122
use std::collections::HashMap;
2223
use std::env;
@@ -44,7 +45,9 @@ fn setup_client() -> AppConfigurationClient {
4445

4546
//TODO: Our current pricing plan doesn't allow more than 1 collection, so we are using
4647
// car-rentals so far.
47-
AppConfigurationClient::new(&apikey, &region, &guid, "testing", "car-rentals").unwrap()
48+
AppConfigIBMCloudBuilder::new(&region, &apikey, &guid, "testing", "car-rentals")
49+
.build()
50+
.unwrap()
4851
}
4952

5053
#[rstest]

0 commit comments

Comments
 (0)