Skip to content

Commit eb6adaa

Browse files
committed
feat: add bearer token authentication to GraphQL requests
1 parent 07ee922 commit eb6adaa

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ ROOT_URL=https://root.amfoss.in/
33
OWNER_ID=
44
DEBUG=true
55
ENABLE_DEBUG_LIBRARIES=false
6+
AMD_API_KEY=

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct Config {
3434
pub owner_id: Option<UserId>,
3535
pub prefix_string: String,
3636
pub root_url: String,
37+
pub api_key: String,
3738
}
3839

3940
impl Default for Config {
@@ -46,6 +47,8 @@ impl Default for Config {
4647
owner_id: parse_owner_id_env("OWNER_ID"),
4748
prefix_string: String::from("$"),
4849
root_url: std::env::var("ROOT_URL").expect("ROOT_URL was not found in env"),
50+
api_key: std::env::var("AMD_API_KEY")
51+
.expect("AMD_API_KEY was not found in env"),
4952
}
5053
}
5154
}

src/graphql/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,26 @@ use reqwest::Client;
2626
pub struct GraphQLClient {
2727
http: Client,
2828
root_url: Arc<String>,
29+
api_key: Arc<String>,
2930
}
3031

3132
impl GraphQLClient {
32-
pub fn new(root_url: String) -> Self {
33+
pub fn new(root_url: String, api_key: String) -> Self {
3334
Self {
3435
http: Client::new(),
3536
root_url: Arc::new(root_url),
37+
api_key: Arc::new(api_key),
3638
}
3739
}
3840

3941
pub fn root_url(&self) -> &str {
4042
&self.root_url
4143
}
4244

45+
pub fn api_key(&self) -> &str {
46+
&self.api_key
47+
}
48+
4349
pub fn http(&self) -> Client {
4450
self.http.clone()
4551
}

src/graphql/queries.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl GraphQLClient {
6060
let response = self
6161
.http()
6262
.post(self.root_url())
63+
.bearer_auth(self.api_key())
6364
.json(&serde_json::json!({"query": query, "variables":variables}))
6465
.send()
6566
.await
@@ -115,6 +116,7 @@ impl GraphQLClient {
115116
let response = self
116117
.http()
117118
.post(self.root_url())
119+
.bearer_auth(self.api_key())
118120
.json(&serde_json::json!({ "query": query }))
119121
.send()
120122
.await

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ struct Data {
5555

5656
impl Data {
5757
/// Returns a new [`Data`] with an empty `reaction_roles` field and the passed-in `reload_handle`.
58-
fn new(reload_handle: ReloadHandle, root_url: String) -> Self {
58+
fn new(reload_handle: ReloadHandle, root_url: String, api_key: String) -> Self {
5959
Data {
6060
reaction_roles: HashMap::new(),
6161
log_reload_handle: reload_handle,
62-
graphql_client: GraphQLClient::new(root_url),
62+
graphql_client: GraphQLClient::new(root_url, api_key),
6363
}
6464
}
6565
}
@@ -95,7 +95,11 @@ fn build_framework(
9595
}
9696

9797
fn prepare_data(config: &Config, reload_handle: ReloadHandle) -> Data {
98-
let mut data = Data::new(reload_handle, config.root_url.clone());
98+
let mut data = Data::new(
99+
reload_handle,
100+
config.root_url.clone(),
101+
config.api_key.clone(),
102+
);
99103
data.populate_with_reaction_roles();
100104
data
101105
}

0 commit comments

Comments
 (0)