1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use transport::{YarRequest};
use packager::MsgPackPackager;
use packager::JSONPackager;
use packager::Packager;
use packager::YarProtocol;
use snowflake_multi_threaded::SnowFlakeId;
use curl::easy::Easy;
use time::Duration;
use std::cell::RefCell;
use std::boxed::Box;
use std::io::Read;
use error::YarError;
use Result;
pub const YAR_OPT_PACKAGER:u8 = 1 << 0;
pub const YAR_OPT_PERSISTENT:u8 = 1 << 1;
pub const YAR_OPT_TIMEOUT:u8 = 1<< 2;
pub const YAR_OPT_CONNECT_TIMEOUT:u8 = 1<< 3;
pub struct Builder {
client_conf:YarClientConf
}
#[derive(Clone, Default)]
struct YarClientConf{
url:String,
timeout:i64,
connect_timeout:i64,
packager_name:String,
token:String,
provider:String,
}
pub struct YarClient{
curl_client:Easy,
packager:Box<Packager>,
snow_flake_id:SnowFlakeId,
provider:String,
token:String,
}
impl YarClient {
pub fn call(&mut self,fn_name:&str, parameters:Vec<String>)->Result<String> {
let req_id = self.snow_flake_id.generate_id().unwrap() & 0x0000FFFF ;
let request = YarRequest::new(req_id as u32, fn_name.to_string(), parameters.clone());
let post_raw:Vec<u8> = request.encode( &self.packager, self.token.as_ref(), self.provider.as_ref())?;
let resp_raw:RefCell<Vec<u8>> = RefCell::new(Vec::new());
self.curl_client.post(true)?;
self.curl_client.post_field_size(post_raw.len() as u64)?;
let mut transfer = self.curl_client.transfer();
transfer.read_function(|buf| {
let a = post_raw.as_slice().read(buf).unwrap_or(0);
Ok(a)
})?;
transfer.write_function( |data| {
resp_raw.borrow_mut().extend_from_slice(data);
Ok(data.len())
})?;
transfer.perform()?;
let protocol = YarProtocol::to_protocol(resp_raw.borrow().to_vec());
let resp = self.packager.unpack(protocol.body.unwrap());
String::from("111");
match resp.s {
0 => Ok(resp.r),
_ => {
Err(YarError::CallErr(format!("code: {}, msg: {}, output: {}", resp.s, resp.e, resp.o)))
}
}
}
}
impl <'a>Builder{
pub fn set_url(mut self,url:&str) -> Result<Builder>{
if url.is_empty() || (!url.starts_with("http") && !url.starts_with("https")){
return Err(YarError::URLError("url must be contains http or https"));
}
self.client_conf.url = url.to_string();
Ok(self)
}
pub fn set_opt<T>(mut self, name:u8, value:T) -> Result<Builder>
where T:Sized + ToString
{
if name.eq(&YAR_OPT_TIMEOUT){
self.client_conf.timeout = value.to_string().parse::<i64>()?;
}
if name.eq(&YAR_OPT_CONNECT_TIMEOUT){
self.client_conf.connect_timeout = value.to_string().parse::<i64>()?;
}
if name.eq(&YAR_OPT_PACKAGER){
self.client_conf.packager_name = value.to_string();
}
Ok(self)
}
pub fn set_provider(mut self, provider:&str) -> Builder{
self.client_conf.provider = provider.to_string();
self
}
pub fn set_token(mut self, token:&str) -> Builder{
self.client_conf.token = token.to_string();
self
}
pub fn build(self) -> Result<YarClient> {
let mut client = YarClient{
curl_client : Easy::new(),
packager : match self.client_conf.packager_name.as_str() {
"JSON" => Box::new(JSONPackager{}),
"MSGPACK" => Box::new( MsgPackPackager{}),
_ => Box::new(JSONPackager{})
},
snow_flake_id:SnowFlakeId::new(1,1),
token: self.client_conf.token,
provider:self.client_conf.provider,
};
client.curl_client.url(self.client_conf.url.as_ref())?;
if self.client_conf.timeout > 0 {
client.curl_client.timeout(Duration::seconds(self.client_conf.timeout).to_std().unwrap())?;
}
if self.client_conf.connect_timeout > 0 {
client.curl_client.connect_timeout(Duration::seconds(self.client_conf.connect_timeout).to_std().unwrap())?;
}
client.curl_client.url(self.client_conf.url.as_ref())?;
Ok(client)
}
}
impl <'a> Default for Builder{
fn default() -> Self {
Builder{
client_conf : YarClientConf{
timeout: 0,
connect_timeout: 0,
provider: "yar_client_rust".to_string(),
token:"yar_client_rust".to_string(),
..Default::default()
}
}
}
}