Use surf
in pokedex
example
This commit is contained in:
parent
ffa46898d9
commit
f0381a7fb3
@ -42,8 +42,8 @@ serde = { version = "1.0", features = ["derive"] }
|
|||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
directories = "2.0"
|
directories = "2.0"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
reqwest = "0.9"
|
|
||||||
async-std = { version = "1.3", features = ["unstable"] }
|
async-std = { version = "1.3", features = ["unstable"] }
|
||||||
|
surf = { version = "1.0", git = "https://github.com/http-rs/surf.git", rev = "2ff0f95513e82bdb5ccc56767f9dd0985f2eb8fe" }
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||||
|
@ -150,7 +150,6 @@ impl Pokemon {
|
|||||||
async fn search() -> Result<Pokemon, Error> {
|
async fn search() -> Result<Pokemon, Error> {
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Entry {
|
struct Entry {
|
||||||
@ -179,7 +178,11 @@ impl Pokemon {
|
|||||||
let url = format!("https://pokeapi.co/api/v2/pokemon-species/{}", id);
|
let url = format!("https://pokeapi.co/api/v2/pokemon-species/{}", id);
|
||||||
let sprite = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id);
|
let sprite = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id);
|
||||||
|
|
||||||
let entry: Entry = reqwest::get(&url)?.json()?;
|
let (entry, sprite): (Entry, _) = futures::future::try_join(
|
||||||
|
surf::get(&url).recv_json(),
|
||||||
|
surf::get(&sprite).recv_bytes(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let description = entry
|
let description = entry
|
||||||
.flavor_text_entries
|
.flavor_text_entries
|
||||||
@ -188,13 +191,6 @@ impl Pokemon {
|
|||||||
.next()
|
.next()
|
||||||
.ok_or(Error::LanguageError)?;
|
.ok_or(Error::LanguageError)?;
|
||||||
|
|
||||||
let mut sprite = reqwest::get(&sprite)?;
|
|
||||||
let mut bytes = Vec::new();
|
|
||||||
|
|
||||||
sprite
|
|
||||||
.read_to_end(&mut bytes)
|
|
||||||
.map_err(|_| Error::ImageError)?;
|
|
||||||
|
|
||||||
Ok(Pokemon {
|
Ok(Pokemon {
|
||||||
number: id,
|
number: id,
|
||||||
name: entry.name.to_uppercase(),
|
name: entry.name.to_uppercase(),
|
||||||
@ -203,7 +199,7 @@ impl Pokemon {
|
|||||||
.chars()
|
.chars()
|
||||||
.map(|c| if c.is_control() { ' ' } else { c })
|
.map(|c| if c.is_control() { ' ' } else { c })
|
||||||
.collect(),
|
.collect(),
|
||||||
image: image::Handle::from_memory(bytes),
|
image: image::Handle::from_memory(sprite),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -211,13 +207,12 @@ impl Pokemon {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Error {
|
enum Error {
|
||||||
APIError,
|
APIError,
|
||||||
ImageError,
|
|
||||||
LanguageError,
|
LanguageError,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<reqwest::Error> for Error {
|
impl From<surf::Exception> for Error {
|
||||||
fn from(error: reqwest::Error) -> Error {
|
fn from(exception: surf::Exception) -> Error {
|
||||||
dbg!(&error);
|
dbg!(&exception);
|
||||||
|
|
||||||
Error::APIError
|
Error::APIError
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ impl Application for Timer {
|
|||||||
let duration = Text::new(format!(
|
let duration = Text::new(format!(
|
||||||
"{:0>2}:{:0>2}:{:0>2}.{:0>2}",
|
"{:0>2}:{:0>2}:{:0>2}.{:0>2}",
|
||||||
seconds / HOUR,
|
seconds / HOUR,
|
||||||
seconds / MINUTE,
|
(seconds % HOUR) / MINUTE,
|
||||||
seconds % MINUTE,
|
seconds % MINUTE,
|
||||||
self.duration.subsec_millis() / 10,
|
self.duration.subsec_millis() / 10,
|
||||||
))
|
))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user