get_services_by_type

Function get_services_by_type 

Source
pub fn get_services_by_type<T>(
    type_name: &str,
) -> Result<Vec<Service<T>>, Error<'_>>
Expand description

Get’s you a a list services fromVCAP_SERVICES by their type

This allows you to get the services with their credentials. The type T can be used to have a typed credentials struct. As the format of credentials is up to your provider it defaults to a generic Value from serde_json.

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct CustomCredentials {
    pub password: String,
    pub username: String,
    pub host: String,
    pub port: u16,
}

// After that you can use it

let services = cf_env::get_services_by_type::<CustomCredentials>("a_type_of_service").unwrap();

There is no need for typed credentials if you would like to parse it anyway

use serde_json::Value;
use cf_env::Service;

let services: Vec<Service<Value>>  = cf_env::get_services_by_type("a_type_of_service").unwrap();

let uri = services[0].credentials["uri"].as_str().unwrap();