diff --git a/Cargo.toml b/Cargo.toml index 271454c..fb80d6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ chrono = {version = "0.4", features = ["serde"]} rand = "0.8.5" http = "0.2" serde = { version = "1.0", features = ["derive"] } -spin-sdk = { git = "https://github.com/fermyon/spin", rev = "139c40967a75dbdd5d4da2e626d24e68f54c0a5a" } +spin-sdk = "3" diff --git a/src/tweet.rs b/src/tweet.rs index 2c2f3e6..5da9c8c 100644 --- a/src/tweet.rs +++ b/src/tweet.rs @@ -10,14 +10,15 @@ struct Publish { pub fn addhelpers(x: &mut Handlebars) { handlebars_helper!(tweet: |user: String, id: String| { let url = format!("https://publish.twitter.com/oembed?url=https://twitter.com/{}/status/{}", user, id); - let req = http::request::Builder::new().method("GET").uri(&url).body(None).unwrap(); - let res = spinhttp::send(req).unwrap(); + let req = spinhttp::Request::get(&url); + let res_fut = spinhttp::send(req); + let res: spinhttp::Response = spinhttp::run(res_fut).unwrap(); let mut html = "".to_string(); - let body = res.body().as_ref().map(|bytes| bytes.as_ref()); - let str = std::str::from_utf8(body.unwrap()).unwrap().to_string(); + let body = res.body(); + let str = std::str::from_utf8(body).unwrap().to_string(); - if res.status().is_success() { + if is_success_status(res.status()) { let deserialized: Publish = serde_json::from_str(&str).unwrap(); html = deserialized.html.to_string() } else { @@ -29,3 +30,7 @@ pub fn addhelpers(x: &mut Handlebars) { x.register_helper("tweet", Box::new(tweet)); } + +fn is_success_status(status: &spinhttp::StatusCode) -> bool { + http::StatusCode::from_u16(*status).is_ok_and(|s| s.is_success()) +}