bahnapi.rs (2136B)
1 use graphql_client::*; 2 use std::fs; 3 4 #[derive(GraphQLQuery)] 5 #[graphql( 6 schema_path = "bahn_definitions/schema.graphql", 7 query_path = "bahn_definitions/search.graphql", 8 response_derives = "Debug" 9 )] 10 pub struct Search; 11 12 #[derive(GraphQLQuery)] 13 #[graphql( 14 schema_path = "bahn_definitions/schema.graphql", 15 query_path = "bahn_definitions/nearby.graphql", 16 response_derives = "Debug" 17 )] 18 pub struct Nearby; 19 20 pub fn search(search_term: String) -> Result<(), failure::Error> { 21 let q = Search::build_query(search::Variables { 22 search_term: Some(search_term), 23 }); 24 25 let client = reqwest::Client::new(); 26 27 let mut res = client 28 .post("https://api.deutschebahn.com/free1bahnql/v1/graphql") 29 .bearer_auth("6fc6827d4628e9568210eed4a22fa8b5") 30 .json(&q) 31 .send()?; 32 let response_body: Response<search::ResponseData> = res.json()?; 33 //println!("{:?}", response_body); 34 fs::write("./search.json", format!("{:?}", response_body)).expect("Unable to write file"); 35 if let Some(errors) = response_body.errors { 36 println!("there are errors:"); 37 38 for error in &errors { 39 println!("{:?}", error); 40 } 41 } 42 Ok(()) 43 } 44 45 pub fn nearby( 46 lat: f64, 47 long: f64, 48 radius: Option<i64>, 49 count: Option<i64>, 50 ) -> Result<Response<nearby::ResponseData>, failure::Error> { 51 let q = Nearby::build_query(nearby::Variables { 52 latitude: lat, 53 longitude: long, 54 radius, 55 count, 56 }); 57 58 let client = reqwest::Client::new(); 59 60 let mut res = client 61 .post("https://api.deutschebahn.com/free1bahnql/v1/graphql") 62 .bearer_auth("6fc6827d4628e9568210eed4a22fa8b5") 63 .json(&q) 64 .send()?; 65 let response_body: Response<nearby::ResponseData> = res.json()?; 66 //println!("{:?}", response_body); 67 fs::write("./nearby.json", format!("{:?}", response_body)).expect("Unable to write file"); 68 if let Some(errors) = &response_body.errors { 69 println!("there are errors:"); 70 71 for error in errors { 72 println!("{:?}", error); 73 } 74 } 75 Ok(response_body) 76 }