Crates.io | spotify_confidence_openfeature_provider |
lib.rs | spotify_confidence_openfeature_provider |
version | 0.1.2 |
source | src |
created_at | 2024-06-05 12:25:31.016383 |
updated_at | 2024-07-08 12:26:58.945253 |
description | Openfeature provider for the Confidence SDK for Rust |
homepage | https://confidence.spotify.com/ |
repository | https://github.com/spotify/confidence-sdk-rust |
max_upload_size | |
id | 1262708 |
size | 36,469 |
Rust implementation of the Confidence feature provider, to be used in conjunction with the OpenFeature SDK.
setProvider
makes the openfeature api to be able to use confidence as the provider for flags.
first, we need to setup the api config which contains a client_secret
and a a region
, then we can create a provider like following:
let api_config = APIConfig {
api_key: "API_KEY".to_string(),
region: Region::Global,
};
let confidence = Confidence::new(api_config);
let provider = ConfidenceProvider::new(confidence);
at this point we can set the provider for the OpenFeatureAPI
as following:
let mut api = OpenFeature::singleton_mut().await;
api.set_provider(provider).await;
after this initial setup we can start accessing the flags. Every time any of flags are asked, the provider fetches them from the network and resolve the asked property.
The schema of the property plays a crucial role in resolving the property, if the schema type matches the asked type, the value will be returned otherwise
we expect an MismatchType
error from the EvaluationError
.
// wrong type, should return error
let details_string = api
.create_client()
.get_bool_details("test.struct-key.string-key", Some(&context), None)
.await;
// correct type, should return value
let details_boolean = api
.create_client()
.get_bool_details("my-test.struct-key.boolean-key", Some(&context), None)
.await;
println!("details string -> {:?}", details_string);
println!("details boolean -> {:?}", details_boolean.unwrap().value);
The result of the above code would be:
details string -> Err(EvaluationError { code: TypeMismatch, message: Some("schema type is different for my-test.struct-key.string-key") })
details boolean -> true
It’s also possible to read the whole flag and get the whole flag as a struct like following
let details_flag = api
.create_client()
.get_struct_details::<MyStructValue>("my-test.struct-key", Some(&context), None)
.await
.unwrap();
println!("details boolean struct -> {:?}", details_flag.value.my_boolean);
println!("details string struct -> {:?}", details_flag.value.my_string);
}
struct MyStructValue {
my_boolean: bool,
my_string: String
}
impl FromStructValue for MyStructValue {
fn from_struct_value(value: &StructValue) -> anyhow::Result<Self> {
return Ok(MyStructValue {
my_boolean: value.fields.get("boolean-key").unwrap().as_bool().unwrap_or_default(),
my_string: value.fields.get("string-key").unwrap().as_str().unwrap_or_default().to_string(),
})
}
}