Skip to content

Commit

Permalink
Refactor HttpBuilder to avoid unwrap calls (#1727)
Browse files Browse the repository at this point in the history
This refactors the HTTP builder to hold `ratelimiter_disabled` and `token`
without `Option`s, removing the need to use `unwrap` in the `build` method.
  • Loading branch information
vaporoxx authored and arqunis committed Mar 15, 2022
1 parent 3aca5ca commit 89499b2
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/http/client.rs
Expand Up @@ -56,32 +56,28 @@ use crate::utils;
pub struct HttpBuilder {
client: Option<Client>,
ratelimiter: Option<Ratelimiter>,
ratelimiter_disabled: Option<bool>,
token: Option<String>,
ratelimiter_disabled: bool,
token: String,
proxy: Option<Url>,
#[cfg(feature = "unstable_discord_api")]
application_id: Option<u64>,
}

impl HttpBuilder {
fn _new() -> Self {
/// Construct a new builder to call methods on for the HTTP construction.
/// The `token` will automatically be prefixed "Bot " if not already.
pub fn new(token: impl AsRef<str>) -> Self {
Self {
client: None,
ratelimiter: None,
ratelimiter_disabled: Some(false),
token: None,
ratelimiter_disabled: false,
token: parse_token(token),
proxy: None,
#[cfg(feature = "unstable_discord_api")]
application_id: None,
}
}

/// Construct a new builder to call methods on for the HTTP construction.
/// The `token` will automatically be prefixed "Bot " if not already.
pub fn new(token: impl AsRef<str>) -> Self {
Self::_new().token(token)
}

/// Sets the application_id to use interactions.
#[cfg(feature = "unstable_discord_api")]
pub fn application_id(mut self, application_id: u64) -> Self {
Expand All @@ -93,12 +89,7 @@ impl HttpBuilder {
/// Sets a token for the bot. If the token is not prefixed "Bot ", this
/// method will automatically do so.
pub fn token(mut self, token: impl AsRef<str>) -> Self {
let token = token.as_ref().trim();

let token =
if token.starts_with("Bot ") { token.to_string() } else { format!("Bot {}", token) };

self.token = Some(token);
self.token = parse_token(token);

self
}
Expand Down Expand Up @@ -128,7 +119,7 @@ impl HttpBuilder {
/// purpose of delegating rate limiting to an API proxy via [`Self::proxy`]
/// instead of the current process.
pub fn ratelimiter_disabled(mut self, ratelimiter_disabled: bool) -> Self {
self.ratelimiter_disabled = Some(ratelimiter_disabled);
self.ratelimiter_disabled = ratelimiter_disabled;

self
}
Expand Down Expand Up @@ -157,9 +148,8 @@ impl HttpBuilder {
}

/// Use the given configuration to build the `Http` client.
#[allow(clippy::unwrap_used)]
pub fn build(self) -> Http {
let token = self.token.unwrap();
let token = self.token;

#[cfg(feature = "unstable_discord_api")]
let application_id = self
Expand All @@ -176,7 +166,7 @@ impl HttpBuilder {
Ratelimiter::new(client, token.to_string())
});

let ratelimiter_disabled = self.ratelimiter_disabled.unwrap();
let ratelimiter_disabled = self.ratelimiter_disabled;

Http {
client,
Expand All @@ -190,6 +180,16 @@ impl HttpBuilder {
}
}

fn parse_token(token: impl AsRef<str>) -> String {
let token = token.as_ref().trim();

if token.starts_with("Bot ") {
token.to_string()
} else {
format!("Bot {}", token)
}
}

fn reason_into_header(reason: &str) -> Headers {
let mut headers = Headers::new();
headers.insert(
Expand Down

0 comments on commit 89499b2

Please sign in to comment.