refactor: do not get api secret at compile time

This commit is contained in:
Youwen Wu 2024-09-11 02:09:50 -07:00
parent 2f3e5244ff
commit 0c770db006
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3

View file

@ -1,22 +1,26 @@
use reqwest::header; use reqwest::header;
use reqwest::Error; use reqwest::{Client, Error};
use std::env; use std::env;
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); 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<Client, Error> {
let token =
env::var("CANVAS_SECRET").expect("Canvas API key is not defined in the environment.");
let mut headers = header::HeaderMap::new(); let mut headers = header::HeaderMap::new();
headers.insert( headers.insert(
header::AUTHORIZATION, 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) .user_agent(APP_USER_AGENT)
.default_headers(headers) .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") .get("https://ucsb.instructure.com/api/v1/courses")
.send() .send()
.await?; .await?;