yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
216 stars 81 forks source link

[auth] Allow to construct `CredentialsFile` from plain `&str` #200

Closed benjaminch closed 9 months ago

benjaminch commented 9 months ago

Hi there! :)

Context

I would be super nice to be able to construct CredentialsFile from plain &str instead of having to pass a file path (eventually have to create a file) or set an environment variable. Let's say you have the credentials json content already, it's not very handy to put its content to a file or to set an environment variable in order to construct CredentialsFile.

Proposal

Having a new function in CredentialsFile taking a plain &str (meant to be credentials json content) and returning a CredentialsFile:

pub async fn new_from_str(str: &str) -> Result<Self, Error> {
    Ok(serde_json::from_str(str)?)
}

One can then use the API this way:

const CREDENTIALS_FILE_CONTENT: &str = "<CREDENTIALS_FILE_CONTENT_GOES_HERE>";

let credentials_file = CredentialsFile::new_from_str(CREDENTIALS_FILE_CONTENT).await;

Thanks a lot :)

benjaminch commented 9 months ago

Merged ! https://github.com/yoshidan/google-cloud-rust/pull/201