1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use super::{Client, State};
use crate::{
error::{Error, Result},
ratelimiting::Ratelimiter,
request::channel::message::allowed_mentions::AllowedMentions,
};
use reqwest::{Client as ReqwestClient, ClientBuilder as ReqwestClientBuilder, Proxy};
use std::{sync::Arc, time::Duration};
#[derive(Clone, Debug)]
pub struct ClientBuilder {
pub(crate) default_allowed_mentions: Option<AllowedMentions>,
pub(crate) proxy: Option<Proxy>,
pub(crate) proxy_http: bool,
pub(crate) ratelimiter: Option<Ratelimiter>,
pub(crate) reqwest_client: Option<ReqwestClient>,
pub(crate) timeout: Duration,
pub(crate) token: Option<String>,
}
impl ClientBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn build(self) -> Result<Client> {
let mut builder = ReqwestClientBuilder::new().timeout(self.timeout);
if let Some(proxy) = self.proxy {
builder = builder.proxy(proxy)
}
Ok(Client {
state: Arc::new(State {
http: builder
.build()
.map_err(|source| Error::BuildingClient { source })?,
ratelimiter: self.ratelimiter,
token: self.token,
use_http: self.proxy_http,
default_allowed_mentions: self.default_allowed_mentions,
}),
})
}
pub fn default_allowed_mentions(&mut self, allowed_mentions: AllowedMentions) -> &mut Self {
self.default_allowed_mentions.replace(allowed_mentions);
self
}
pub fn proxy(&mut self, proxy: Proxy) -> &mut Self {
self.proxy.replace(proxy);
self
}
pub fn proxy_http(&mut self, proxy_http: bool) -> &mut Self {
self.proxy_http = proxy_http;
self
}
pub fn reqwest_client(&mut self, client: ReqwestClient) -> &mut Self {
self.reqwest_client.replace(client);
self
}
pub fn ratelimiter(&mut self, ratelimiter: impl Into<Option<Ratelimiter>>) -> &mut Self {
self.ratelimiter = ratelimiter.into();
self
}
pub fn timeout(&mut self, duration: Duration) -> &mut Self {
self.timeout = duration;
self
}
pub fn token(&mut self, token: impl Into<String>) -> &mut Self {
let mut token = token.into();
let is_bot = token.starts_with("Bot ");
let is_bearer = token.starts_with("Bearer ");
if !is_bot && !is_bearer {
token.insert_str(0, "Bot ");
}
self.token.replace(token);
self
}
}
impl Default for ClientBuilder {
fn default() -> Self {
Self {
default_allowed_mentions: None,
proxy: None,
proxy_http: false,
reqwest_client: None,
ratelimiter: Some(Ratelimiter::new()),
timeout: Duration::from_secs(10),
token: None,
}
}
}