Skip to content

Commit

Permalink
Switch to reqwest (#44)
Browse files Browse the repository at this point in the history
Back then `reqwest` wasn't supporting WebAssembly particularly well, or
even at all initially. Nowadays it does not have any of these problems
anymore and significantly simplifies our codebase.
  • Loading branch information
CryZe committed Jan 18, 2024
1 parent 778e60f commit 81840c6
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 651 deletions.
26 changes: 6 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "splits-io-api"
version = "0.3.2"
version = "0.4.0"
authors = ["Christopher Serr <christopher.serr@gmail.com>"]
edition = "2021"
documentation = "https://docs.rs/splits-io-api/"
Expand All @@ -20,28 +20,14 @@ include = [
]

[dependencies]
http = "0.2.0"
serde = "1.0.189"
serde_derive = "1.0.189"
serde_json = "1.0.107"
snafu = { version = "0.7.1", default-features = false, features = ["std"] }
url = "2.1.0"
uuid = { version = "1.1.2", default-features = false, features = ["serde"] }
uuid = { version = "1.6.1", default-features = false, features = ["serde"] }
reqwest = { version = "0.11.23", default-features = false, features = ["json", "multipart"] }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
hyper = { version = "0.14.1", default-features = false, features = ["tcp", "client", "http2"] }

[target.'cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64", target_arch = "loongarch64", all(target_arch = "mips", target_endian = "little"), all(target_arch = "mips64", target_endian = "little"), all(target_arch = "powerpc", target_endian = "big"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "s390x"))'.dependencies]
hyper-rustls = { version = "0.24.1", default-features = false, features = ["native-tokio", "http2", "tls12"] }

[target.'cfg(all(not(target_family = "wasm"), not(any(target_arch = "arm", target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64", target_arch = "loongarch64", all(target_arch = "mips", target_endian = "little"), all(target_arch = "mips64", target_endian = "little"), all(target_arch = "powerpc", target_endian = "big"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "s390x"))))'.dependencies]
hyper-tls = "0.5.0"

[target.'cfg(target_family = "wasm")'.dependencies]
js-sys = "0.3.31"
wasm-bindgen = "0.2.51"
wasm-bindgen-futures = "0.4.4"
web-sys = { version = "0.3.32", features = ["Response", "Window", "RequestInit", "AbortSignal", "ObserverCallback", "ReferrerPolicy", "RequestCache", "RequestCredentials", "RequestInit", "RequestMode", "RequestRedirect", "Headers"] }
[features]
default = ["rustls"]
rustls = ["reqwest/rustls-tls-webpki-roots"]

[dev-dependencies]
tokio = { version = "1.0.1", features = ["io-std", "macros"] }
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# <img src="https://raw.githubusercontent.com/glacials/splits-io/dec549110968c5a02df87cddff49a43549cceb92/public/logo.png" alt="Splits.io" height="42" width="42" align="top"/> splits-io-api
# <img src="https://raw.githubusercontent.com/glacials/splits-io/dec549110968c5a02df87cddff49a43549cceb92/public/logo.png" alt="splits.io" height="42" width="42" align="top"/> splits-io-api

[![Build Status](https://github.com/LiveSplit/splits-io-api/workflows/Rust/badge.svg)](https://github.com/LiveSplit/splits-io-api/actions)
[![crates.io](https://img.shields.io/crates/v/splits-io-api.svg)](https://crates.io/crates/splits-io-api)
[![docs.rs](https://docs.rs/splits-io-api/badge.svg)](https://docs.rs/splits-io-api/)
[![dependency status](https://deps.rs/repo/github/LiveSplit/splits-io-api/status.svg)](https://deps.rs/repo/github/LiveSplit/splits-io-api)

Bindings to the Splits.io API for Rust. Both native platforms and the web are
Bindings to the splits.io API for Rust. Both native platforms and the web are
supported.

## Example Usage

```rust
// Create a Splits.io API client.
// Create a splits.io API client.
let client = Client::new();

// Search for a runner.
Expand Down
22 changes: 4 additions & 18 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

use crate::{
get_json,
platform::Body,
wrapper::{ContainsCategory, ContainsRunners, ContainsRuns},
Category, Client, Error, Run, Runner,
};
use http::Request;
use url::Url;
use reqwest::Url;

impl Category {
/// Gets a Category.
Expand All @@ -34,11 +32,7 @@ pub async fn get(client: &Client, id: &str) -> Result<Category, Error> {
let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
url.path_segments_mut().unwrap().push(id);

let ContainsCategory { category } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsCategory { category } = get_json(client, client.client.get(url)).await?;

Ok(category)
}
Expand All @@ -48,11 +42,7 @@ pub async fn get_runners(client: &Client, id: &str) -> Result<Vec<Runner>, Error
let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
url.path_segments_mut().unwrap().extend(&[id, "runners"]);

let ContainsRunners { runners } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsRunners { runners } = get_json(client, client.client.get(url)).await?;

Ok(runners)
}
Expand All @@ -62,11 +52,7 @@ pub async fn get_runs(client: &Client, id: &str) -> Result<Vec<Run>, Error> {
let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
url.path_segments_mut().unwrap().extend(&[id, "runs"]);

let ContainsRuns { runs } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsRuns { runs } = get_json(client, client.client.get(url)).await?;

Ok(runs)
}
49 changes: 13 additions & 36 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

use crate::{
get_json,
platform::Body,
wrapper::{ContainsCategories, ContainsGame, ContainsGames, ContainsRunners, ContainsRuns},
Category, Client, Error, Game, Run, Runner, UnidentifiableResourceSnafu,
Category, Client, Error, Game, Run, Runner,
};
use http::Request;
use snafu::OptionExt;
use url::Url;
use reqwest::Url;

impl Game {
/// Searches for a Game based on the name of the game.
Expand All @@ -29,8 +26,8 @@ impl Game {
get_categories(
client,
self.shortname
.as_ref()
.context(UnidentifiableResourceSnafu)?,
.as_deref()
.ok_or(Error::UnidentifiableResource)?,
)
.await
}
Expand All @@ -40,8 +37,8 @@ impl Game {
get_runs(
client,
self.shortname
.as_ref()
.context(UnidentifiableResourceSnafu)?,
.as_deref()
.ok_or(Error::UnidentifiableResource)?,
)
.await
}
Expand All @@ -51,8 +48,8 @@ impl Game {
get_runners(
client,
self.shortname
.as_ref()
.context(UnidentifiableResourceSnafu)?,
.as_deref()
.ok_or(Error::UnidentifiableResource)?,
)
.await
}
Expand All @@ -63,11 +60,7 @@ pub async fn search(client: &Client, name: &str) -> Result<Vec<Game>, Error> {
let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
url.query_pairs_mut().append_pair("search", name);

let ContainsGames { games } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsGames { games } = get_json(client, client.client.get(url)).await?;

Ok(games)
}
Expand All @@ -77,11 +70,7 @@ pub async fn get(client: &Client, shortname: &str) -> Result<Game, Error> {
let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
url.path_segments_mut().unwrap().push(shortname);

let ContainsGame { game } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsGame { game } = get_json(client, client.client.get(url)).await?;

Ok(game)
}
Expand All @@ -93,11 +82,7 @@ pub async fn get_categories(client: &Client, shortname: &str) -> Result<Vec<Cate
.unwrap()
.extend(&[shortname, "categories"]);

let ContainsCategories { categories } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsCategories { categories } = get_json(client, client.client.get(url)).await?;

Ok(categories)
}
Expand All @@ -109,11 +94,7 @@ pub async fn get_runs(client: &Client, shortname: &str) -> Result<Vec<Run>, Erro
.unwrap()
.extend(&[shortname, "runs"]);

let ContainsRuns { runs } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsRuns { runs } = get_json(client, client.client.get(url)).await?;

Ok(runs)
}
Expand All @@ -125,11 +106,7 @@ pub async fn get_runners(client: &Client, shortname: &str) -> Result<Vec<Runner>
.unwrap()
.extend(&[shortname, "runners"]);

let ContainsRunners { runners } = get_json(
client,
Request::get(url.as_str()).body(Body::empty()).unwrap(),
)
.await?;
let ContainsRunners { runners } = get_json(client, client.client.get(url)).await?;

Ok(runners)
}

0 comments on commit 81840c6

Please sign in to comment.