Skip to content

Commit

Permalink
Simplify constructor functions for Http (#1840)
Browse files Browse the repository at this point in the history
This commit aims to clean up the different methods for creating a new `Http`
instance.

Removals:
- `Http::new()`: Providing your own `reqwest` client seems like a niche use
  case where you can use `HttpBuilder` instead
- `Http::new_with_application_id()`: Creates an HTTP client without a token, so
  you won't be able to use it anyway
- `Http::default()`: Same as above

Renames:
- `Http::new_with_token()` -> `Http::new()`
- `Http::new_with_token_application_id()` -> `Http::new_with_application_id()`

Co-authored-by: Constantin Nickel <constantin.nickel@gmail.com>
  • Loading branch information
2 people authored and arqunis committed Apr 18, 2022
1 parent a36353b commit f8bc937
Show file tree
Hide file tree
Showing 25 changed files with 84 additions and 123 deletions.
2 changes: 1 addition & 1 deletion examples/e05_command_framework/src/main.rs
Expand Up @@ -221,7 +221,7 @@ async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

let http = Http::new_with_token(&token);
let http = Http::new(&token);

// We will fetch your bot's owners and id
let (owners, bot_id) = match http.get_current_application_info().await {
Expand Down
2 changes: 1 addition & 1 deletion examples/e06_sample_bot_structure/src/main.rs
Expand Up @@ -65,7 +65,7 @@ async fn main() {

let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

let http = Http::new_with_token(&token);
let http = Http::new(&token);

// We will fetch your bot's owners and id
let (owners, _bot_id) = match http.get_current_application_info().await {
Expand Down
2 changes: 1 addition & 1 deletion examples/e10_collectors/src/main.rs
Expand Up @@ -53,7 +53,7 @@ async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

let http = Http::new_with_token(&token);
let http = Http::new(&token);

// We will fetch your bot's id.
let bot_id = match http.get_current_user().await {
Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_channel.rs
Expand Up @@ -100,7 +100,7 @@ impl CreateChannel {
/// # use std::sync::Arc;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Arc::new(Http::default());
/// # let http = Arc::new(Http::new("token"));
/// # let mut guild = GuildId(0).to_partial_guild(&http).await?;
/// use serenity::model::channel::{PermissionOverwrite, PermissionOverwriteType};
/// use serenity::model::id::UserId;
Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_message.rs
Expand Up @@ -35,7 +35,7 @@ use crate::model::id::StickerId;
/// # use serenity::http::Http;
/// # use std::sync::Arc;
/// #
/// # let http = Arc::new(Http::default());
/// # let http = Arc::new(Http::new("token"));
///
/// let channel_id = ChannelId(7);
///
Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_channel.rs
Expand Up @@ -16,7 +16,7 @@ use crate::model::id::ChannelId;
/// # use serenity::{http::Http, model::id::ChannelId};
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let mut channel = ChannelId(0);
/// // assuming a channel has already been bound
/// if let Err(why) = channel.edit(&http, |c| c.name("new name").topic("a test topic")).await {
Expand Down Expand Up @@ -160,7 +160,7 @@ impl EditChannel {
/// # use std::sync::Arc;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Arc::new(Http::default());
/// # let http = Arc::new(Http::new("token"));
/// # let mut channel = ChannelId(0);
/// use serenity::model::channel::{PermissionOverwrite, PermissionOverwriteType};
/// use serenity::model::id::UserId;
Expand Down
6 changes: 3 additions & 3 deletions src/builder/edit_guild.rs
Expand Up @@ -54,7 +54,7 @@ impl EditGuild {
/// # use serenity::{http::Http, model::id::GuildId};
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let mut guild = GuildId(0).to_partial_guild(&http).await?;
/// use serenity::utils;
///
Expand Down Expand Up @@ -239,7 +239,7 @@ impl EditGuild {
/// # use serenity::{http::Http, model::id::GuildId};
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let mut guild = GuildId(0).to_partial_guild(&http).await?;
/// use serenity::model::guild::VerificationLevel;
///
Expand Down Expand Up @@ -273,7 +273,7 @@ impl EditGuild {
/// # use serenity::{http::Http, model::id::GuildId};
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let mut guild = GuildId(0).to_partial_guild(&http).await?;
/// use serenity::model::guild::SystemChannelFlags;
///
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_role.rs
Expand Up @@ -35,7 +35,7 @@ use crate::model::Permissions;
/// # use serenity::{model::id::{ChannelId, GuildId}, http::Http};
/// # use std::sync::Arc;
/// #
/// # let http = Arc::new(Http::default());
/// # let http = Arc::new(Http::new("token"));
/// # let (channel_id, guild_id) = (ChannelId(1), GuildId(2));
/// #
/// // assuming a `channel_id` and `guild_id` has been bound
Expand Down
12 changes: 6 additions & 6 deletions src/builder/execute_webhook.rs
Expand Up @@ -28,7 +28,7 @@ use crate::model::channel::MessageFlags;
/// use serenity::utils::Colour;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
/// let webhook = http.get_webhook_from_url(url).await?;
///
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> ExecuteWebhook<'a> {
/// # use serenity::http::Http;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let webhook = http.get_webhook_with_token(0, "").await?;
/// #
/// let avatar_url = "https://i.imgur.com/KTs6whd.jpg";
Expand All @@ -103,7 +103,7 @@ impl<'a> ExecuteWebhook<'a> {
/// # use serenity::http::Http;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let webhook = http.get_webhook_with_token(0, "").await?;
/// #
/// let execution = webhook.execute(&http, false, |w| w.content("foo")).await;
Expand Down Expand Up @@ -217,7 +217,7 @@ impl<'a> ExecuteWebhook<'a> {
/// # use serenity::http::Http;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let webhook = http.get_webhook_with_token(0, "").await?;
/// #
/// let execution = webhook.execute(&http, false, |w| w.content("hello").tts(true)).await;
Expand All @@ -243,7 +243,7 @@ impl<'a> ExecuteWebhook<'a> {
/// # use serenity::http::Http;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let webhook = http.get_webhook_with_token(0, "").await?;
/// #
/// let execution = webhook.execute(&http, false, |w| w.content("hello").username("hakase")).await;
Expand All @@ -270,7 +270,7 @@ impl<'a> ExecuteWebhook<'a> {
/// # use serenity::model::channel::MessageFlags;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// # let webhook = http.get_webhook_with_token(0, "").await?;
/// #
/// let execution = webhook
Expand Down
2 changes: 1 addition & 1 deletion src/builder/get_messages.rs
Expand Up @@ -29,7 +29,7 @@ use crate::model::id::MessageId;
/// # use serenity::http::Http;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Http::default();
/// # let http = Http::new("token");
/// use serenity::model::id::{ChannelId, MessageId};
///
/// // you can then pass it into a function which retrieves messages:
Expand Down
2 changes: 1 addition & 1 deletion src/client/bridge/gateway/shard_manager.rs
Expand Up @@ -65,7 +65,7 @@ use crate::CacheAndHttp;
/// impl EventHandler for Handler {}
/// impl RawEventHandler for Handler {}
///
/// # let cache_and_http = Arc::new(CacheAndHttp::default());
/// # let cache_and_http: Arc<CacheAndHttp> = unimplemented!();
/// # let http = &cache_and_http.http;
/// let gateway_url = Arc::new(Mutex::new(http.get_gateway().await?.url));
/// let data = Arc::new(RwLock::new(TypeMap::new()));
Expand Down
4 changes: 2 additions & 2 deletions src/client/mod.rs
Expand Up @@ -109,7 +109,7 @@ impl ClientBuilder {
/// a framework via the [`Self::framework`] or [`Self::framework_arc`] method,
/// otherwise awaiting the builder will cause a panic.
pub fn new(token: impl AsRef<str>) -> Self {
Self::_new(Http::new_with_token(token.as_ref()))
Self::_new(Http::new(token.as_ref()))
}

/// Construct a new builder with a [`Http`] instance to calls methods on
Expand All @@ -126,7 +126,7 @@ impl ClientBuilder {
/// 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 {
self.http = Some(Http::new_with_token(token.as_ref()));
self.http = Some(Http::new(token.as_ref()));

self
}
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/shard.rs
Expand Up @@ -113,7 +113,7 @@ impl Shard {
/// # use serenity::model::gateway::GatewayIntents;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http = Arc::new(Http::default());
/// # let http = Arc::new(Http::new("token"));
/// let token = std::env::var("DISCORD_BOT_TOKEN")?;
/// // retrieve the gateway response, which contains the URL to connect to
/// let gateway = Arc::new(Mutex::new(http.get_gateway().await?.url));
Expand Down

0 comments on commit f8bc937

Please sign in to comment.