Skip to content

Commit 972bba2

Browse files
authored
Merge pull request kayleg#10 from ruseinov/ru/feature/creds-from-string
Allow parsing credentials from string
2 parents 1dc79b1 + 46015fe commit 972bba2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/client.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use hyper::client::HttpConnector;
77
use hyper_tls::HttpsConnector;
88
use log::{error, info};
99
use smpl_jwt::Jwt;
10+
use std::fs;
11+
use std::str::FromStr;
1012
use std::sync::atomic::{AtomicBool, Ordering};
1113
use std::sync::{Arc, RwLock};
1214
use std::time::Duration;
@@ -17,7 +19,7 @@ type HyperClient = Arc<hyper::Client<HttpsConnector<HttpConnector>, hyper::Body>
1719

1820
pub struct State {
1921
token: Option<goauth::auth::Token>,
20-
credentials_path: String,
22+
credentials_string: String,
2123
project: Option<String>,
2224
hyper_client: HyperClient,
2325
running: Arc<AtomicBool>,
@@ -46,10 +48,10 @@ impl Clone for Client {
4648
}
4749

4850
impl Client {
49-
pub fn new(credentials_path: String) -> Result<Self, error::Error> {
51+
pub fn from_string(credentials_string: String) -> Result<Self, error::Error> {
5052
let mut client = Client(Arc::new(RwLock::new(State {
5153
token: None,
52-
credentials_path,
54+
credentials_string,
5355
project: None,
5456
hyper_client: setup_hyper(),
5557
running: Arc::new(AtomicBool::new(true)),
@@ -61,6 +63,11 @@ impl Client {
6163
}
6264
}
6365

66+
pub fn new(credentials_path: String) -> Result<Self, error::Error> {
67+
let credentials_string = fs::read_to_string(credentials_path).unwrap();
68+
Self::from_string(credentials_string)
69+
}
70+
6471
pub fn subscribe(&self, name: String) -> Subscription {
6572
Subscription {
6673
client: Some(self.clone()),
@@ -127,7 +134,7 @@ impl Client {
127134

128135
fn get_token(&mut self) -> Result<goauth::auth::Token, goauth::error::GOErr> {
129136
let credentials =
130-
goauth::credentials::Credentials::from_file(&self.0.read().unwrap().credentials_path)
137+
goauth::credentials::Credentials::from_str(&self.0.read().unwrap().credentials_string)
131138
.unwrap();
132139

133140
self.set_project(credentials.project());

0 commit comments

Comments
 (0)