From 0c770db0066bd2dde7ed7c7e77dc9f10e9a41222 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Wed, 11 Sep 2024 02:09:50 -0700 Subject: [PATCH] refactor: do not get api secret at compile time --- src/main.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3aef641..619c5c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,26 @@ use reqwest::header; -use reqwest::Error; +use reqwest::{Client, Error}; use std::env; static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); -static TOKEN: &str = env!("CANVAS_SECRET"); -async fn get_request() -> Result<(), reqwest::Error> { +fn create_client() -> Result { + let token = + env::var("CANVAS_SECRET").expect("Canvas API key is not defined in the environment."); + let mut headers = header::HeaderMap::new(); headers.insert( header::AUTHORIZATION, - format!("Bearer {}", TOKEN).parse().unwrap(), + format!("Bearer {}", token).parse().unwrap(), ); - let client = reqwest::Client::builder() + reqwest::Client::builder() .user_agent(APP_USER_AGENT) .default_headers(headers) - .build()?; + .build() +} - let response = client +async fn get_request() -> Result<(), reqwest::Error> { + let response = create_client()? .get("https://ucsb.instructure.com/api/v1/courses") .send() .await?;