Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed May 19, 2024
1 parent 49c6b54 commit d0c932f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ pub static AUTH_URL: &str =
"https://tdx.transportdata.tw/auth/realms/TDXConnect/protocol/openid-connect/token";
pub static URL_HEAD: &str = "https://tdx.transportdata.tw/api/basic";

pub async fn get_token_header(client_id: &str, client_secret: &str) -> Result<String, Box<dyn std::error::Error>> {
pub async fn get_token_header(
client_id: &str,
client_secret: &str,
) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::Client::new();

let auth_header = json!({
Expand All @@ -23,10 +26,10 @@ pub async fn get_token_header(client_id: &str, client_secret: &str) -> Result<St
.text()
.await?;

let data_header = auth_response.split_once("\":\"").unwrap().1;
let data_header = auth_response.split_once("\":\"").unwrap().1;

match data_header.split_once("\",") {
Some((token, _)) => Ok(format!("Bearer {}", token)),
None => Err("Token not found".into()),
}
}
match data_header.split_once("\",") {
Some((token, _)) => Ok(format!("Bearer {}", token)),
None => Err("Token not found".into()),
}
}
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use reqwest::{header::AUTHORIZATION, header::CONTENT_TYPE, *};
use serde_json::*;
use std::{collections::HashMap, env, error::Error, fs::File, path::Path};
//use std::io::Write;
use catenary_tdx_data::auth::{AUTH_URL, URL_HEAD};
use std::{thread, time::Duration};
use catenary_tdx_data::auth::{URL_HEAD,AUTH_URL};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand Down Expand Up @@ -58,7 +58,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let rail = [
//static rail
"/v2/Rail/Operator", //also for metro
"/v2/Rail/THSR/Station", //theres only one line so they dont have routes
"/v2/Rail/THSR/Station", //theres only one line so they dont have routes
"/v2/Rail/THSR/GeneralTimetable", //calender, trips, stop times
"/v2/Rail/THSR/Shape",
"/v2/Rail/THSR/ODFare",
Expand Down Expand Up @@ -107,11 +107,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
"TRTC", //has live
"KRTC", //has live
"KLRT", //has live
"TYMC",
"TRTCMG", //gondola :D
"TMRT",
"NTMC",
"NTALRT",
"TYMC", "TRTCMG", //gondola :D
"TMRT", "NTMC", "NTALRT",
];

let raw_path = match env::consts::ARCH {
Expand All @@ -127,10 +124,13 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let secret: HashMap<String, String> =
serde_json::from_reader(file).expect("error while reading");

let client = Client::new();

let client = Client::new();

let token = catenary_tdx_data::auth::get_token_header(secret.get("client_id").unwrap(), secret.get("client_secret").unwrap()).await?;
let token = catenary_tdx_data::auth::get_token_header(
secret.get("client_id").unwrap(),
secret.get("client_secret").unwrap(),
)
.await?;

for loc in city.iter() {
fetch(&bus[0].replace("city", loc), &token, &client)
Expand Down

0 comments on commit d0c932f

Please sign in to comment.