赞
踩
前面,我试了Julia调用 JoinQuant的web api 的数据。兴奋之余,也试了一下Rust。
Rust的 reqwest库不太熟悉,用了更长的一点时间。但总体上用法差不多。
一、相关的库
[dependencies]
reqwest = "0.9.10"
serde_json = "1.0"
关于reqwest库,除了github上的文档,建议参考,可以了解更清楚明了。
http://siciarz.net/24-days-rust-reqwest/
二、相关代码:
use std::time::Duration; use std::{thread, time}; extern crate reqwest; extern crate serde_json; use std::collections::HashMap; use std::io::Read; fn get_token()->String{ let mut data = HashMap::new(); data.insert("method", "get_token"); data.insert("mob", "13761618222"); data.insert("pwd", "618222"); let mut _res= reqwest::Client::new() .post("https://dataapi.joinquant.com/apis") .json(&data) .send() .expect("Failed to send request"); let mut buf = String::new(); _res.read_to_string(&mut buf).expect("Failed to read response"); buf } fn get_security_info(token:&str,code:&str,date:&str) -> String{ //"method" => "get_security_info","token" => token,"code" => "502050.XSHG","date" =>"2019-01-15" let mut data = HashMap::new(); data.insert("method","get_security_info"); data.insert("token",token); data.insert("code",code); data.insert("date",date); let mut _res= reqwest::Client::new() .post("https://dataapi.joinquant.com/apis") .json(&data) .send() .expect("Failed to send request"); let mut buf = String::new(); _res.read_to_string(&mut buf).expect("Failed to read response"); buf } fn get_bars(token:&str,code:&str,count:&str,unit_type:&str,end_date:&str,fq_ref_date:&str) -> String{ //"method" => "get_security_info","token" => token,"code" => "502050.XSHG","date" =>"2019-01-15" let mut data = HashMap::new(); data.insert("method","get_bars"); data.insert("count",count); data.insert("unit",unit_type); data.insert("token",token); data.insert("code",code); data.insert("enddate",end_date); data.insert("fq_ref_date",fq_ref_date); let mut _res= reqwest::Client::new() .post("https://dataapi.joinquant.com/apis") .json(&data) .send() .expect("Failed to send request"); let mut buf = String::new(); _res.read_to_string(&mut buf).expect("Failed to read response"); buf } //params=Dict("method" => "get_ticks","token" => token,"code"=>code,"count"=>count,"end_date"=>end_date); fn get_ticks(token:&str,code:&str,count:&str,end_date:&str) -> String{ let mut data = HashMap::new(); data.insert("method","get_ticks"); data.insert("count",count); data.insert("token",token); data.insert("code",code); data.insert("enddate",end_date); let mut _res= reqwest::Client::new() .post("https://dataapi.joinquant.com/apis") .json(&data) .send() .expect("Failed to send request"); let mut buf = String::new(); _res.read_to_string(&mut buf).expect("Failed to read response"); buf } fn main() { let _token = get_token(); println!("{:?}",_token); let code ="600000.XSHG"; let date = "2019-01-15"; let _data = get_bars(&_token,code,"10000","1m","2019-01-15","2019-02-28"); println!("{}",_data); thread::sleep(Duration::from_millis(500000)); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。