-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace panics with results & better option types #437
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ pub use aw_models::{Bucket, BucketMetadata, Event}; | |
|
||
pub struct AwClient { | ||
client: reqwest::Client, | ||
pub baseurl: String, | ||
pub baseurl: reqwest::Url, | ||
pub name: String, | ||
pub hostname: String, | ||
} | ||
|
@@ -29,13 +29,11 @@ impl std::fmt::Debug for AwClient { | |
} | ||
|
||
impl AwClient { | ||
pub fn new(ip: &str, port: &str, name: &str) -> AwClient { | ||
let baseurl = format!("http://{ip}:{port}"); | ||
pub fn new(baseurl: reqwest::Url, name: &str, hostname: String) -> AwClient { | ||
let client = reqwest::Client::builder() | ||
.timeout(std::time::Duration::from_secs(120)) | ||
.build() | ||
.unwrap(); | ||
let hostname = gethostname::gethostname().into_string().unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This unwrap is extremely unlikely, you would need to essentially have invalid utf-8 in your hostname to fail here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, very unlikely, but in that unlikely event, panic-ing doesn't make sense, I think. Maybe I should just strip out UTF-8 characters instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! |
||
AwClient { | ||
client, | ||
baseurl, | ||
|
@@ -47,7 +45,7 @@ impl AwClient { | |
pub async fn get_bucket(&self, bucketname: &str) -> Result<Bucket, reqwest::Error> { | ||
let url = format!("{}/api/0/buckets/{}", self.baseurl, bucketname); | ||
let bucket = self | ||
.client | ||
.client | ||
.get(url) | ||
.send() | ||
.await? | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to expose our reqwest dependency in the API? I think we should avoid doing that.
Wouldn't it be better to have a custom error type that the Url is invalid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really an API? While I found a old crates.io package, this project really does not feel like a library.
That said, if you'd like to keep that distinction, then I agree, but then I think new() should return a Result, which is fine, but a bit odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is an API, just not a very mature one :)
As you saw, it's used by aw-sync, but its also used by aw-watcher-window-wayland.