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"
|
||||
directories = "2.0"
|
||||
futures = "0.3"
|
||||
reqwest = "0.9"
|
||||
async-std = { version = "1.3", features = ["unstable"] }
|
||||
surf = { version = "1.0", git = "https://github.com/http-rs/surf.git", rev = "2ff0f95513e82bdb5ccc56767f9dd0985f2eb8fe" }
|
||||
rand = "0.7"
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||
|
@ -150,7 +150,6 @@ impl Pokemon {
|
||||
async fn search() -> Result<Pokemon, Error> {
|
||||
use rand::Rng;
|
||||
use serde::Deserialize;
|
||||
use std::io::Read;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Entry {
|
||||
@ -179,7 +178,11 @@ impl Pokemon {
|
||||
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 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
|
||||
.flavor_text_entries
|
||||
@ -188,13 +191,6 @@ impl Pokemon {
|
||||
.next()
|
||||
.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 {
|
||||
number: id,
|
||||
name: entry.name.to_uppercase(),
|
||||
@ -203,7 +199,7 @@ impl Pokemon {
|
||||
.chars()
|
||||
.map(|c| if c.is_control() { ' ' } else { c })
|
||||
.collect(),
|
||||
image: image::Handle::from_memory(bytes),
|
||||
image: image::Handle::from_memory(sprite),
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -211,13 +207,12 @@ impl Pokemon {
|
||||
#[derive(Debug, Clone)]
|
||||
enum Error {
|
||||
APIError,
|
||||
ImageError,
|
||||
LanguageError,
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(error: reqwest::Error) -> Error {
|
||||
dbg!(&error);
|
||||
impl From<surf::Exception> for Error {
|
||||
fn from(exception: surf::Exception) -> Error {
|
||||
dbg!(&exception);
|
||||
|
||||
Error::APIError
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ impl Application for Timer {
|
||||
let duration = Text::new(format!(
|
||||
"{:0>2}:{:0>2}:{:0>2}.{:0>2}",
|
||||
seconds / HOUR,
|
||||
seconds / MINUTE,
|
||||
(seconds % HOUR) / MINUTE,
|
||||
seconds % MINUTE,
|
||||
self.duration.subsec_millis() / 10,
|
||||
))
|
||||
|
Loading…
x
Reference in New Issue
Block a user