Skip to content

Commit

Permalink
Parses response json into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
holtkampjs committed Mar 12, 2023
1 parent edd335d commit 4898674
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 65 deletions.
127 changes: 69 additions & 58 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11", features = ["blocking", "json", "gzip"] }
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0.94"
tokio = { version = "1.0", features = ["full"] }
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#![allow(unused)]
use reqwest::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug, Deserialize, Serialize)]
struct WordOfTheDay {
#[serde(rename(deserialize = "contentProvider"))]
content_provider: HashMap<String, Value>,
definitions: Vec<HashMap<String, Value>>,
examples: Vec<HashMap<String, Value>>,
#[serde(rename(deserialize = "htmlExtra"))]
html_extra: Option<String>,
note: String,
word: String,
category: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
#[tokio::main]
async fn main() -> Result<()> {
let api_key = std::env::var("WORDNIK_API_KEY").unwrap();
let url = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key=".to_owned() + &api_key;
let response = reqwest::blocking::get(url)?.text()?;
let json: serde_json::Value = serde_json::from_str(&response)?;
println!("{:#?}", json);
let word_of_the_day: WordOfTheDay =
reqwest::Client::new().get(url).send().await?.json().await?;
println!("{:#?}", word_of_the_day);
Ok(())
}

0 comments on commit 4898674

Please sign in to comment.