-
Notifications
You must be signed in to change notification settings - Fork 415
Added example: Using serde to populate structures from an API #926
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
7538383
a95d811
1b75516
3eafe7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| target | ||
| node_modules | ||
| .wrangler |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "serde-rust-structs" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [package.metadata.release] | ||
| release = false | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| worker = { version = "0.7", features = ['http'] } | ||
| worker-macros = { version = "0.7", features = ['http'] } | ||
| http = "1.3" | ||
| serde = "1.0.228" | ||
| serde_json = "1.0.149" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an unused dependency. This is important in so far as using
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for letting me know. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Using Serde to populate Rust structures | ||
| Demo of using ```serde::{Serialize, Deserialize}``` to populate structures from an API, and then return a JSON value from a structure. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| use worker::{ Context, Env, Fetch, Request, Response, Result, RouteContext, Router, event }; | ||
| use serde::{ Serialize, Deserialize }; | ||
|
|
||
| #[derive(Serialize, Deserialize, Clone)] | ||
| pub struct ImageURL { | ||
| pub image_url: String, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Clone)] | ||
| pub struct Images { | ||
| pub jpg: ImageURL, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Clone)] | ||
| pub struct Data { | ||
| pub mal_id: u32, | ||
| pub url: String, | ||
| pub title: String, | ||
| pub date: String, | ||
| pub author_username: String, | ||
| pub author_url: String, | ||
| pub forum_url: String, | ||
| pub images: Images, | ||
| pub comments: u32, | ||
| pub excerpt: String, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Clone)] | ||
| pub struct Pagination { | ||
| pub last_visible_page: u32, | ||
| pub has_next_page: bool, | ||
| } | ||
|
|
||
| // The main struct from the API | ||
| #[derive(Serialize, Deserialize, Clone)] | ||
| pub struct APIResult { | ||
| pub pagination: Pagination, | ||
| pub data: Vec<Data>, | ||
| } | ||
|
|
||
| // Handle route for /anime/:id | ||
| async fn get_anime_news(_req: Request, ctx: RouteContext<()>) -> Result<Response> { | ||
| let id = ctx.param("id").ok_or_else(|| worker::Error::RustError("Missing 'id' param".into()))?; | ||
|
|
||
| let url = format!("https://api.jikan.moe/v4/anime/{}/news", id); //Ex id: 38524 | ||
|
|
||
| let request = http::Request | ||
| ::builder() | ||
| .method("GET") | ||
| .uri(url.as_str()) | ||
| .header("Content-Type", "application/json") | ||
| .header("Accept", "*/*") | ||
| .body(String::new()) | ||
| .unwrap(); | ||
|
|
||
| let api_res: APIResult = Fetch::Request(worker::Request::try_from(request).unwrap()) | ||
| .send().await? | ||
| .json().await?; | ||
|
|
||
| Response::from_json(&api_res) | ||
| } | ||
|
|
||
| #[event(fetch)] | ||
| async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> { | ||
| Router::new().get_async("/anime/:id", get_anime_news).run(req, env).await | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| name = "serde-rust-structs" | ||
| main = "build/index.js" | ||
| compatibility_date = "2026-02-07" | ||
|
|
||
| [build] | ||
| command = "cargo install -q worker-build@^0.7 && worker-build --release" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unused dependency. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove it soon!