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?;