use crate::error::Error;

use super::types::Role;

/// Get list of Roles
pub async fn get(
  shasta_token: &str,
  shasta_base_url: &str,
  shasta_root_cert: &[u8],
  socks5_proxy: Option<&str>,
) -> Result<Vec<String>, Error> {
  let client_builder = reqwest::Client::builder()
    .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

  let client = match socks5_proxy {
    Some(proxy) => client_builder.proxy(reqwest::Proxy::all(proxy)?).build()?,
    None => client_builder.build()?,
  };

  let api_url: String =
    shasta_base_url.to_owned() + "/smd/hsm/v2/service/values/role";

  let payload = client
    .get(api_url)
    .bearer_auth(shasta_token)
    .send()
    .await
    .map_err(|error| Error::NetError(error))?
    .json::<Role>()
    .await;

  payload
    .map(|role| role.role)
    .map_err(|error| Error::NetError(error))
}
