/
germanubis
/
reqwest
Обзор
Документация
Войти
/
germanubis
/
reqwest
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/wasm_github_fetch/src/lib.rs
47 строк
1 KB
Yuri Astrakhan
docs: fix a few spelling issues (#2478)
23 дек 2024, 23:52
Не верифицирован
23 дек 2024, 23:52
ea48da7
Код
Авторство
О чём код?
use serde::{Deserialize, Serialize}; use wasm_bindgen::prelude::*; // NOTE: This test is a clone of https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs // but uses Reqwest instead of the web_sys fetch api directly /// A struct to hold some data from the GitHub Branch API. /// /// Note how we don't have to define every member -- serde will ignore extra /// data when deserializing #[derive(Debug, Serialize, Deserialize)] pub struct Branch { pub name: String, pub commit: Commit, } #[derive(Debug, Serialize, Deserialize)] pub struct Commit { pub sha: String, pub commit: CommitDetails, } #[derive(Debug, Serialize, Deserialize)] pub struct CommitDetails { pub author: Signature, pub committer: Signature, } #[derive(Debug, Serialize, Deserialize)] pub struct Signature { pub name: String, pub email: String, } #[wasm_bindgen] pub async fn run() -> Result<JsValue, JsValue> { let res = reqwest::Client::new() .get("https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master") .header("Accept", "application/vnd.github.v3+json") .send() .await?; let text = res.text().await?; let branch_info: Branch = serde_json::from_str(&text).unwrap(); Ok(JsValue::from_serde(&branch_info).unwrap()) }