From d7a93d5a3f15b324e75860d1bc06813de54f5eca Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 25 Dec 2022 10:19:11 +0200 Subject: [PATCH] tactical nuke..no json :( --- frontend/src/fetching.rs | 23 ++++++----------------- frontend/src/main.rs | 12 ------------ src/api.rs | 15 ++++++++------- src/shadowrealmapi.rs | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 src/shadowrealmapi.rs diff --git a/frontend/src/fetching.rs b/frontend/src/fetching.rs index a827e6e..1a1ea37 100644 --- a/frontend/src/fetching.rs +++ b/frontend/src/fetching.rs @@ -228,28 +228,17 @@ async fn fetch_commit(url: &'static str) -> Result { Ok(text.as_string().unwrap()) } -async fn fetch_driver(url: &'static str) -> Result { +async fn fetch_driver(url: &'static str) -> Result { let mut opts = RequestInit::new(); opts.method("GET"); - //Available request modes: Cors, NoCors, SameOrigin - opts.mode(RequestMode::NoCors); //NoCors because cors simply doesn't work here - + opts.mode(RequestMode::Cors); //Cors is required for fetch to work + //initialise request let request = Request::new_with_str_and_init(url, &opts)?; - //api header for json - request.headers().set("Accept", "application/json")?; - - let window = web_sys::window().unwrap(); + let window = gloo::utils::window(); let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?; - assert!(resp_value.is_instance_of::()); let resp: Response = resp_value.dyn_into().unwrap(); - - //parsing Json response - let fetched_json = JsFuture::from(resp.json()?).await?; - log!(fetched_json.clone()); - let fetched_json: JsValue = fetched_json.into(); - //parsing into CommonDriver struct - let fetched_json: CommonDriver = fetched_json.into_serde().unwrap(); - Ok(fetched_json) + let text = JsFuture::from(resp.text()?).await?; + Ok(text.as_string().unwrap()) } //renders components above diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 1af79f8..7c10f73 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -51,18 +51,6 @@ struct Stdnt { gpa: f32, email: String, } -//testing this rn -//Component that returns HTML from json fetched from Api yes :D -// async fn fetch_person() -> Html { -// let resp = Request::get("http://libkyy.cf/api/driver/1234") -// .send() -// .await -// .unwrap(); -// assert_eq!(resp.status(), 200); -// let driver: CommonDriver = resp.json().await.unwrap(); -// let result = driver.use_it(); -// return result; -// } fn main() { yew::Renderer::::new().render(); diff --git a/src/api.rs b/src/api.rs index f5919a1..3d99df9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -7,25 +7,26 @@ use backend::{ admin_data::get_admin, driver_data::get_driver, establish_connection, ticket_data::get_ticket, }; use common::{CommonAdmin, CommonDriver, CommonTicket}; - +//Json goodies? NAHH //Ticket Table Services #[get("api/ticket/{id}")] -async fn api_ticket(id: web::Path) -> Json { +async fn api_ticket(id: web::Path) -> impl Responder { let fetched_ticket_data = get_ticket(&mut establish_connection(), *id); - Json(fetched_ticket_data) + //return struct as string + HttpResponse::Ok().body(format!("{:?}", fetched_ticket_data)) } // Getting admin data or smth idk #[get("api/admin/{id}")] -async fn api_admin(id: web::Path) -> Json { +async fn api_admin(id: web::Path) -> impl Responder { let fetched_admin_data = get_admin(&mut establish_connection(), *id); - Json(fetched_admin_data) + HttpResponse::Ok().body(format!("{:?}", fetched_admin_data)) } #[get("api/driver/{id}")] -async fn api_driver(id: web::Path) -> Json { +async fn api_driver(id: web::Path) -> impl Responder { let fetched_driver_data = get_driver(&mut establish_connection(), *id); - Json(fetched_driver_data) + HttpResponse::Ok().body(format!("{:?}", fetched_driver_data)) } #[get("api/test")] async fn api_test() -> impl Responder { diff --git a/src/shadowrealmapi.rs b/src/shadowrealmapi.rs new file mode 100644 index 0000000..fff5bd7 --- /dev/null +++ b/src/shadowrealmapi.rs @@ -0,0 +1,20 @@ +//Json goodies? NAHH +//Ticket Table Services +// #[get("api/ticket/{id}")] +// async fn api_ticket(id: web::Path) -> Json { +// let fetched_ticket_data = get_ticket(&mut establish_connection(), *id); +// Json(fetched_ticket_data) +// } +// +// // Getting admin data or smth idk +// #[get("api/admin/{id}")] +// async fn api_admin(id: web::Path) -> Json { +// let fetched_admin_data = get_admin(&mut establish_connection(), *id); +// Json(fetched_admin_data) +// } +// +// #[get("api/driver/{id}")] +// async fn api_driver(id: web::Path) -> Json { +// let fetched_driver_data = get_driver(&mut establish_connection(), *id); +// Json(fetched_driver_data) +// }