Wrote program for Day 20
This commit is contained in:
parent
4a71624acc
commit
8aa13b7c38
@ -66,7 +66,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
||||
| Day #17 | [Prison Break](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-17_Prison-Break) | :white_check_mark: |
|
||||
| Day #18 | [Unique Paths](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-18_Unique-Paths) | :white_check_mark: |
|
||||
| Day #19 | [URL Shortener](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-19_URL-Shortener) | :white_check_mark: |
|
||||
| Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_large_square: |
|
||||
| Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_check_mark: |
|
||||
| Day #21 | [Random Maze Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-21_Random-Maze-Generator) | :white_large_square: |
|
||||
| Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_large_square: |
|
||||
| Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_large_square: |
|
||||
|
11
Week-03/Day-20_API-Challenge/day20/Cargo.toml
Normal file
11
Week-03/Day-20_API-Challenge/day20/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "day20"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.5"
|
||||
reqwest = "0.12.5"
|
||||
serde = { version = "1.0.207", features = ["derive"] }
|
||||
serde_json = "1.0.124"
|
||||
tokio = { version = "1.39.2", features = ["full"] }
|
56
Week-03/Day-20_API-Challenge/day20/src/lib.rs
Normal file
56
Week-03/Day-20_API-Challenge/day20/src/lib.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use axum::{extract::Query, Json};
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{from_str, json, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub async fn search(params: Params) -> Result<Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent("JustLearningRust/1.0")
|
||||
.build()?;
|
||||
|
||||
let mut query = String::new();
|
||||
|
||||
if let Some(q) = params.q {
|
||||
query.push_str(&format!("&q={}", q));
|
||||
}
|
||||
if let Some(latitude) = params.latitude {
|
||||
query.push_str(&format!("&lat={}", latitude));
|
||||
}
|
||||
if let Some(longitude) = params.longitude {
|
||||
query.push_str(&format!("&lon={}", longitude));
|
||||
}
|
||||
|
||||
let url = format!(
|
||||
"https://nominatim.openstreetmap.org/search?format=json{}",
|
||||
query
|
||||
);
|
||||
let res = client.get(url).send().await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Params {
|
||||
q: Option<String>,
|
||||
latitude: Option<f32>,
|
||||
longitude: Option<f32>,
|
||||
}
|
||||
|
||||
pub async fn search_handler(Query(params): Query<Params>) -> Json<Value> {
|
||||
let res = search(params).await.expect("Failed to get response body");
|
||||
let str = &res.text().await.expect("Failed to get response body");
|
||||
let json: Vec<HashMap<String, Value>> = from_str(str).unwrap();
|
||||
let mut ret = Vec::new();
|
||||
for el in json.iter() {
|
||||
let display_name = el.get("display_name").unwrap().as_str().unwrap();
|
||||
if display_name.contains("United States")
|
||||
|| display_name.contains("Canada")
|
||||
|| display_name.contains("Brasil")
|
||||
// there's no way to filter by population
|
||||
{
|
||||
ret.push(json!({"name": el.get("display_name"), "latitude": el.get("lat"), "longitude": el.get("lon"), "score": el.get("importance")}));
|
||||
}
|
||||
}
|
||||
Json(json!({ "suggestions": ret }))
|
||||
}
|
11
Week-03/Day-20_API-Challenge/day20/src/main.rs
Normal file
11
Week-03/Day-20_API-Challenge/day20/src/main.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use axum::routing::{get, Router};
|
||||
use day20::search_handler;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new().route("/suggestions", get(search_handler));
|
||||
let listener = tokio::net::TcpListener::bind(&"0.0.0.0:3000")
|
||||
.await
|
||||
.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
Loading…
Reference in New Issue
Block a user