Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Spin SDK 3.x #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update to Spin SDK 3.x
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
itowlson committed Dec 18, 2024
commit 60e5aa66bc0926aae96f1a93297e549c126bcc02
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 10 additions & 5 deletions src/tweet.rs
Original file line number Diff line number Diff line change
@@ -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())
}