Skip to content
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

Refactor builder pattern for ClientBuilder #3

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/customise_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use lastfm::ClientBuilder;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("LASTFM_API_KEY")?;

let client = ClientBuilder::new(api_key, "loige")
.reqwest_client(reqwest::Client::new())
.base_url("http://localhost:8080".parse().unwrap())
.build();

// do something with client...
dbg!(client);

Ok(())
}
37 changes: 23 additions & 14 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,46 @@ lazy_static! {
.expect("Cannot initialize HTTP client");
}

pub struct ClientBuilder<A: AsRef<str>, U: AsRef<str>> {
api_key: A,
username: U,
pub struct ClientBuilder {
api_key: String,
username: String,
reqwest_client: Option<reqwest::Client>,
base_url: Option<Url>,
}

impl<A: AsRef<str>, U: AsRef<str>> ClientBuilder<A, U> {
pub fn new(api_key: A, username: U) -> Self {
impl ClientBuilder {
pub fn new<A: AsRef<str>, U: AsRef<str>>(api_key: A, username: U) -> Self {
Self {
api_key,
username,
api_key: api_key.as_ref().to_string(),
username: username.as_ref().to_string(),
reqwest_client: None,
base_url: None,
}
}

pub fn reqwest_client(&mut self, client: reqwest::Client) -> &mut Self {
pub fn from_env<U: AsRef<str>>(username: U) -> Self {
Self::try_from_env(username).unwrap()
}

pub fn try_from_env<U: AsRef<str>>(username: U) -> Result<Self, VarError> {
let api_key = env::var("LASTFM_API_KEY")?;
Ok(ClientBuilder::new(api_key, username))
}

pub fn reqwest_client(mut self, client: reqwest::Client) -> Self {
self.reqwest_client = Some(client);
self
}

pub fn base_url(&mut self, base_url: Url) -> &mut Self {
pub fn base_url(mut self, base_url: Url) -> Self {
self.base_url = Some(base_url);
self
}

pub fn build(self) -> Client {
Client {
api_key: self.api_key.as_ref().to_string(),
username: self.username.as_ref().to_string(),
api_key: self.api_key,
username: self.username,
reqwest_client: self
.reqwest_client
.unwrap_or_else(|| DEFAULT_CLIENT.clone()),
Expand All @@ -65,6 +74,7 @@ impl<A: AsRef<str>, U: AsRef<str>> ClientBuilder<A, U> {
}
}

#[derive(Debug, Clone)]
pub struct Client {
api_key: String,
username: String,
Expand Down Expand Up @@ -182,12 +192,11 @@ async fn get_page<A: AsRef<str>, U: AsRef<str>>(

impl Client {
pub fn from_env<U: AsRef<str>>(username: U) -> Self {
Self::try_from_env(username).unwrap()
ClientBuilder::try_from_env(username).unwrap().build()
}

pub fn try_from_env<U: AsRef<str>>(username: U) -> Result<Self, VarError> {
let api_key = env::var("LASTFM_API_KEY")?;
Ok(ClientBuilder::new(api_key, username).build())
Ok(ClientBuilder::try_from_env(username)?.build())
}

pub async fn now_playing(&self) -> Result<Option<NowPlayingTrack>, Error> {
Expand Down