Skip to content

Commit cd632d6

Browse files
authored
Merge pull request #27 from yajamon/response_structure
Public Apiのレスポンスを共通のTraitで実装したし、結果を構造体にパースして返すようにした
2 parents 61d593b + 2d2fd18 commit cd632d6

File tree

8 files changed

+150
-48
lines changed

8 files changed

+150
-48
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ hyper-tls = "0.1.2"
1919
chrono = "0.4"
2020
openssl = "0.9"
2121
serde_json = "1.0.8"
22+
serde = "1.0"
23+
serde_derive = "1.0"

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[macro_use]
2+
extern crate serde_derive;
3+
14
#[macro_use]
25
mod builder;
36
mod core;

src/main.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,33 @@ use zaif_api::trade_api::*;
1010

1111
fn main() {
1212
let api = CurrenciesBuilder::new().name("btc".to_string()).finalize();
13-
println!("{}", api.exec().unwrap());
13+
for currency in api.exec().unwrap() {
14+
println!("name: {} is_token: {}", currency.name, currency.is_token);
15+
}
1416

1517
let api = CurrencyPairsBuilder::new().finalize();
16-
println!("{}", api.exec().unwrap());
18+
for currency_pair in api.exec().unwrap() {
19+
println!(
20+
"name: {} description: {}",
21+
currency_pair.name,
22+
currency_pair.description
23+
);
24+
}
1725

18-
let api = LastPriceBuilder::new().currency_pair("btc_jpy".to_string()).finalize();
19-
println!("{}", api.exec().unwrap());
26+
let api = LastPriceBuilder::new()
27+
.currency_pair("btc_jpy".to_string())
28+
.finalize();
29+
println!("last_price: {}", api.exec().unwrap().last_price);
2030

21-
let api = DepthBuilder::new().currency_pair("btc_jpy".to_string()).finalize();
22-
println!("{}", api.exec().unwrap());
31+
let api = DepthBuilder::new()
32+
.currency_pair("btc_jpy".to_string())
33+
.finalize();
34+
for ask in api.exec().unwrap().asks {
35+
println!("ask price: {} amount: {}", ask.price(), ask.amount());
36+
}
37+
for bid in api.exec().unwrap().bids {
38+
println!("bid price: {} amount: {}", bid.price(), bid.amount());
39+
}
2340

2441
let access_key = AccessKey::new("YOUR_API_KEY", "YOUR_API_SECRET");
2542
let api = GetInfo2Builder::new()

src/public_api/currencies.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
extern crate reqwest;
1+
extern crate serde;
2+
extern crate serde_json;
23

3-
use core::*;
4+
use public_api::PublicApi;
45

56
builder!(CurrenciesBuilder => Currencies {
67
name: String = "all".to_string()
78
});
89

910
impl Currencies {
10-
pub fn exec(&self) -> reqwest::Result<String> {
11-
let api = ApiBuilder::new()
12-
.uri(
13-
format!("https://api.zaif.jp/api/1/currencies/{}", self.name).as_str(),
14-
)
15-
.finalize();
11+
pub fn exec(&self) -> serde_json::Result<Vec<CurrenciesResponse>> {
12+
serde_json::from_value(<Self as PublicApi>::exec(&self)?)
13+
}
14+
}
1615

17-
api.exec()
16+
impl PublicApi for Currencies {
17+
fn action(&self) -> &str {
18+
"currencies"
19+
}
20+
fn parameter(&self) -> &str {
21+
self.name.as_str()
1822
}
1923
}
24+
25+
#[derive(Deserialize)]
26+
pub struct CurrenciesResponse {
27+
pub name: String,
28+
pub is_token: bool,
29+
}

src/public_api/currency_pairs.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1-
extern crate reqwest;
1+
extern crate serde;
2+
extern crate serde_json;
23

3-
use core::*;
4+
use public_api::PublicApi;
45

56
builder!(CurrencyPairsBuilder => CurrencyPairs {
67
currency_pair: String = "all".to_string()
78
});
89

910
impl CurrencyPairs {
10-
pub fn exec(&self) -> reqwest::Result<String> {
11-
let api = ApiBuilder::new()
12-
.uri(
13-
format!(
14-
"https://api.zaif.jp/api/1/currency_pairs/{}",
15-
self.currency_pair
16-
).as_str(),
17-
)
18-
.finalize();
11+
pub fn exec(&self) -> serde_json::Result<Vec<CurrencyPairsResponse>> {
12+
serde_json::from_value(<Self as PublicApi>::exec(&self)?)
13+
}
14+
}
1915

20-
api.exec()
16+
impl PublicApi for CurrencyPairs {
17+
fn action(&self) -> &str {
18+
"currency_pairs"
19+
}
20+
fn parameter(&self) -> &str {
21+
self.currency_pair.as_str()
2122
}
2223
}
2324

25+
#[derive(Deserialize)]
26+
pub struct CurrencyPairsResponse {
27+
pub name: String,
28+
pub title: String,
29+
pub currency_pair: String,
30+
pub description: String,
31+
pub is_token: bool,
32+
pub event_number: i64,
33+
pub seq: i64,
34+
pub item_unit_min: f64,
35+
pub item_unit_step: f64,
36+
pub item_japanese: String,
37+
pub aux_unit_min: f64,
38+
pub aux_unit_step: f64,
39+
pub aux_unit_point: i64,
40+
pub aux_japanese: String,
41+
}

src/public_api/depth.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
1-
extern crate reqwest;
1+
extern crate serde;
2+
extern crate serde_json;
23

3-
use core::*;
4+
use public_api::PublicApi;
45

56
builder!(DepthBuilder => Depth {
67
currency_pair: String = "btc_jpy".to_string()
78
});
89

910
impl Depth {
10-
pub fn exec(&self) -> reqwest::Result<String> {
11-
let api = ApiBuilder::new()
12-
.uri(
13-
format!("https://api.zaif.jp/api/1/depth/{}", self.currency_pair).as_str(),
14-
)
15-
.finalize();
11+
pub fn exec(&self) -> serde_json::Result<DepthResponse> {
12+
serde_json::from_value(<Self as PublicApi>::exec(&self)?)
13+
}
14+
}
15+
16+
impl PublicApi for Depth {
17+
fn action(&self) -> &str {
18+
"depth"
19+
}
20+
fn parameter(&self) -> &str {
21+
self.currency_pair.as_str()
22+
}
23+
}
1624

17-
api.exec()
25+
#[derive(Deserialize)]
26+
pub struct DepthItem(f64, f64);
27+
impl DepthItem {
28+
pub fn price(&self) -> f64 {
29+
self.0
30+
}
31+
pub fn amount(&self) -> f64 {
32+
self.1
1833
}
1934
}
2035

36+
#[derive(Deserialize)]
37+
pub struct DepthResponse {
38+
pub asks: Vec<DepthItem>,
39+
pub bids: Vec<DepthItem>,
40+
}

src/public_api/last_price.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
extern crate reqwest;
1+
extern crate serde;
2+
extern crate serde_json;
23

3-
use core::*;
4+
use public_api::PublicApi;
45

56
builder!(LastPriceBuilder => LastPrice {
67
currency_pair: String = "btc_jpy".to_string()
78
});
89

910
impl LastPrice {
10-
pub fn exec(&self) -> reqwest::Result<String> {
11-
let api = ApiBuilder::new()
12-
.uri(
13-
format!(
14-
"https://api.zaif.jp/api/1/last_price/{}",
15-
self.currency_pair
16-
).as_str(),
17-
)
18-
.finalize();
11+
pub fn exec(&self) -> serde_json::Result<LastPriceResponse> {
12+
serde_json::from_value(<Self as PublicApi>::exec(&self)?)
13+
}
14+
}
1915

20-
api.exec()
16+
impl PublicApi for LastPrice {
17+
fn action(&self) -> &str {
18+
"last_price"
19+
}
20+
fn parameter(&self) -> &str {
21+
self.currency_pair.as_str()
2122
}
2223
}
2324

25+
#[derive(Deserialize)]
26+
pub struct LastPriceResponse {
27+
pub last_price: f64,
28+
}

src/public_api/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
extern crate reqwest;
2+
extern crate serde_json;
3+
4+
use self::serde_json::Value;
5+
6+
use core::*;
7+
18
pub use self::currencies::*;
29
pub use self::currency_pairs::*;
310
pub use self::last_price::*;
@@ -7,3 +14,23 @@ mod currencies;
714
mod currency_pairs;
815
mod last_price;
916
mod depth;
17+
18+
trait PublicApi {
19+
fn action(&self) -> &str;
20+
fn parameter(&self) -> &str;
21+
fn exec(&self) -> serde_json::Result<Value> {
22+
let endpoint = "https://api.zaif.jp/api/1";
23+
let api = ApiBuilder::new()
24+
.uri(
25+
format!("{}/{}/{}", endpoint, self.action(), self.parameter()).as_str(),
26+
)
27+
.finalize();
28+
29+
let res = match api.exec() {
30+
Ok(res) => res,
31+
Err(e) => panic!("reqwest Error: {}", e),
32+
};
33+
serde_json::from_str(res.as_str())
34+
}
35+
}
36+

0 commit comments

Comments
 (0)