2024-09-11 01:34:47 -07:00
|
|
|
use reqwest::header;
|
2024-09-11 02:09:50 -07:00
|
|
|
use reqwest::{Client, Error};
|
2024-09-11 01:34:47 -07:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
|
|
|
|
|
2024-09-11 02:09:50 -07:00
|
|
|
fn create_client() -> Result<Client, Error> {
|
|
|
|
let token =
|
|
|
|
env::var("CANVAS_SECRET").expect("Canvas API key is not defined in the environment.");
|
|
|
|
|
2024-09-11 01:34:47 -07:00
|
|
|
let mut headers = header::HeaderMap::new();
|
|
|
|
headers.insert(
|
|
|
|
header::AUTHORIZATION,
|
2024-09-11 02:09:50 -07:00
|
|
|
format!("Bearer {}", token).parse().unwrap(),
|
2024-09-11 01:34:47 -07:00
|
|
|
);
|
2024-09-11 02:09:50 -07:00
|
|
|
reqwest::Client::builder()
|
2024-09-11 01:34:47 -07:00
|
|
|
.user_agent(APP_USER_AGENT)
|
|
|
|
.default_headers(headers)
|
2024-09-11 02:09:50 -07:00
|
|
|
.build()
|
|
|
|
}
|
2024-09-11 01:34:47 -07:00
|
|
|
|
2024-09-11 02:09:50 -07:00
|
|
|
async fn get_request() -> Result<(), reqwest::Error> {
|
|
|
|
let response = create_client()?
|
2024-09-11 01:34:47 -07:00
|
|
|
.get("https://ucsb.instructure.com/api/v1/courses")
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
println!("Status: {}", response.status());
|
|
|
|
|
|
|
|
let body = response.text().await?;
|
|
|
|
println!("Body:\n{}", body);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Error> {
|
|
|
|
get_request().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|