Skip to content

Commit

Permalink
Merge pull request #17 from YuukiToriyama/feature/migrate-gloo-to-req…
Browse files Browse the repository at this point in the history
…west/master

reqwestへの移行をmainにマージ
  • Loading branch information
YuukiToriyama authored Nov 28, 2023
2 parents 8d7e2ab + 3ee0107 commit dc8bfe0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ crate-type = ["cdylib"]

[dependencies]
console_error_panic_hook = "0.1.7"
gloo-net = "0.4.0"
nom = "7.1.3"
reqwest = { version = "0.11.22", features = ["json"] }
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
tokio = { version = "1.34.0", features = ["rt", "macros"] }
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38"

[dev-dependencies]
tokio = { version = "1.34.0", features = ["rt", "macros"] }
wasm-bindgen-test = "0.3.38"
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod mock;
pub mod wasm;
pub mod client;

use crate::entity::{City, Prefecture};
use crate::err::Error;
Expand Down
44 changes: 20 additions & 24 deletions src/api/wasm.rs → src/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use crate::api::Api;
use crate::entity::{City, Prefecture, Town};
use crate::err::{ApiErrorKind, Error};
use gloo_net::http::Request;

pub struct ApiImplForWasm {}
pub struct ApiImpl {}

impl Api for ApiImplForWasm {
impl Api for ApiImpl {
async fn get_prefecture_master(&self, prefecture_name: &str) -> Result<Prefecture, Error> {
let endpoint = format!(
"https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist/{}/master.json",
prefecture_name
);
let response = match Request::get(&endpoint).send().await {
let response = match reqwest::get(&endpoint).await {
Ok(result) => result,
Err(_) => return Err(Error::new_api_error(ApiErrorKind::FETCH(endpoint))),
};
if response.ok() {
if response.status() == 200 {
match response.json::<Prefecture>().await {
Ok(result) => Ok(result),
Err(_) => Err(Error::new_api_error(ApiErrorKind::DESERIALIZE(endpoint))),
Expand All @@ -30,15 +29,15 @@ impl Api for ApiImplForWasm {
"https://geolonia.github.io/japanese-addresses/api/ja/{}/{}.json",
prefecture_name, city_name
);
let response = match Request::get(&endpoint).send().await {
let response = match reqwest::get(&endpoint).await {
Ok(result) => result,
Err(_) => return Err(Error::new_api_error(ApiErrorKind::FETCH(endpoint))),
Err(_) => return Err(Error::new_api_error(ApiErrorKind::DESERIALIZE(endpoint))),
};
if response.ok() {
if response.status() == 200 {
match response.json::<Vec<Town>>().await {
Ok(towns) => Ok(City {
Ok(result) => Ok(City {
name: city_name.to_string(),
towns,
towns: result,
}),
Err(_) => Err(Error::new_api_error(ApiErrorKind::DESERIALIZE(endpoint))),
}
Expand All @@ -48,18 +47,15 @@ impl Api for ApiImplForWasm {
}
}

#[cfg(all(test, target_arch = "wasm32"))]
#[cfg(test)]
mod api_tests {
use crate::api::wasm::ApiImplForWasm;
use crate::api::client::ApiImpl;
use crate::api::Api;
use crate::entity::Town;
use wasm_bindgen_test::wasm_bindgen_test;

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
#[tokio::test]
async fn get_prefecture_master_success() {
let api = ApiImplForWasm {};
let api = ApiImpl {};
let prefecture = api.get_prefecture_master("富山県").await.unwrap();
assert_eq!(prefecture.name, "富山県".to_string());
let cities = vec![
Expand All @@ -84,20 +80,20 @@ mod api_tests {
}
}

#[wasm_bindgen_test]
#[tokio::test]
async fn get_prefecture_master_fail() {
let api = ApiImplForWasm {};
let api = ApiImpl {};
let result = api.get_prefecture_master("大阪都").await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().error_message,
"https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist/大阪都/master.jsonを取得できませんでした".to_string()
)
);
}

#[wasm_bindgen_test]
#[tokio::test]
async fn get_city_master_success() {
let api = ApiImplForWasm {};
let api = ApiImpl {};
let city = api.get_city_master("石川県", "羽咋郡志賀町").await.unwrap();
assert_eq!(city.name, "羽咋郡志賀町".to_string());
let town = Town {
Expand All @@ -109,9 +105,9 @@ mod api_tests {
assert!(city.towns.contains(&town));
}

#[wasm_bindgen_test]
#[tokio::test]
async fn get_city_master_fail() {
let api = ApiImplForWasm {};
let api = ApiImpl {};
let result = api.get_city_master("石川県", "敦賀市").await;
assert!(result.is_err());
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod entity;
mod err;
mod parser;

use crate::api::wasm::ApiImplForWasm;
use crate::api::client::ApiImpl;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
Expand All @@ -20,7 +20,7 @@ impl Parser {

pub async fn parse(&self, address: &str) -> String {
console_error_panic_hook::set_once();
let api = ApiImplForWasm {};
let api = ApiImpl {};
let result = parser::parse(api, address).await;
serde_json::to_string(&result).unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub async fn parse<T: Api>(api: T, input: &str) -> ParseResult {
#[cfg(test)]
mod parser_tests {
use crate::api::mock::ApiMock;
use crate::api::wasm::ApiImplForWasm;
use crate::api::client::ApiImpl;
use crate::err::ParseErrorKind;
use crate::parser::parse;
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
Expand Down Expand Up @@ -159,7 +159,7 @@ mod parser_tests {

#[wasm_bindgen_test]
async fn parse_wasm_success() {
let api = ApiImplForWasm {};
let api = ApiImpl {};
let result = parse(api, "兵庫県淡路市生穂新島8番地").await;
assert_eq!(result.address.prefecture, "兵庫県".to_string());
assert_eq!(result.address.city, "淡路市".to_string());
Expand Down

0 comments on commit dc8bfe0

Please sign in to comment.