Skip to content

Commit e1f2fe1

Browse files
committed
Support for Async Actions
1 parent f4c9181 commit e1f2fe1

File tree

6 files changed

+252
-0
lines changed

6 files changed

+252
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,31 @@ let was_voucher_enabled: bool = voucherify.voucher_enable("D1dsWQVE").send().unw
101101
let was_voucher_disabled: bool = voucherify.voucher_disable("D1dsWQVE").send().unwrap();
102102
```
103103

104+
## Async Actions API
105+
106+
Provided methods:
107+
- [Get Async Actions](#get-async-action)
108+
- [List Async Actions](#list-async-actions)
109+
110+
#### [Get Async Action]
111+
112+
``` rust
113+
let async_action: AsyncAction = voucherify.async_action_get("id").send().unwrap();
114+
```
115+
116+
#### [List Async Actions]
117+
118+
``` rust
119+
let async_action_list: Vec<AsyncAction> = voucherify.async_action_list().limit(5).end_date("2021-07-16T16:10:28Z").send().unwrap();
120+
```
121+
104122
## License
105123

106124
Licensed under MIT license ([LICENSE](LICENSE) or http://opensource.org/licenses/MIT)
107125

126+
[Get Async Action]: https://docs.voucherify.io/reference?utm_source=github&utm_medium=sdk&utm_campaign=acq#get-async-actions-1
127+
[List Async Actions]: https://docs.voucherify.io/reference?utm_source=github&utm_medium=sdk&utm_campaign=acq#list-async-actions
128+
108129
[Create Voucher]: https://docs.voucherify.io/reference?utm_source=github&utm_medium=sdk&utm_campaign=acq#create-voucher
109130
[Get Voucher]: https://docs.voucherify.io/reference?utm_source=github&utm_medium=sdk&utm_campaign=acq#vouchers-get
110131
[Update Voucher]: https://docs.voucherify.io/reference?utm_source=github&utm_medium=sdk&utm_campaign=acq#update-voucher

examples/async_actions.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extern crate voucherify_rs;
2+
3+
use voucherify_rs::Voucherify;
4+
use voucherify_rs::async_action::AsyncAction;
5+
use voucherify_rs::async_action::AsyncActionList;
6+
7+
fn main() {
8+
9+
//
10+
// Setup voucherify api
11+
//
12+
let voucherify = Voucherify::new("c70a6f00-cf91-4756-9df5-47628850002b",
13+
"3266b9f8-e246-4f79-bdf0-833929b1380c");
14+
15+
//
16+
// List Async Actions
17+
//
18+
let async_actions: AsyncActionList = voucherify.async_action_list().limit(3).end_date("2021-07-16T16:10:28Z").send().unwrap();
19+
println!("Async Actions: {:?}", async_actions);
20+
21+
//
22+
// Get Async Action
23+
//
24+
let async_action: AsyncAction = voucherify.async_action_get("id").send().unwrap();
25+
println!("Fetched Async Action: {:?}", async_action);
26+
27+
}

src/async_action/get.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::io::Read;
2+
use hyper::Url;
3+
use hyper::method::Method;
4+
use serde_json;
5+
6+
use request::VoucherifyRequest;
7+
use utils::error::VoucherifyError;
8+
use async_action::AsyncAction;
9+
10+
pub struct AsyncActionGetRequest {
11+
request: VoucherifyRequest,
12+
app_url: String,
13+
14+
id: String,
15+
}
16+
17+
impl AsyncActionGetRequest {
18+
pub fn new(request: VoucherifyRequest, id: &str, app_url: String) -> AsyncActionGetRequest {
19+
AsyncActionGetRequest {
20+
request,
21+
app_url,
22+
23+
id: id.to_string(),
24+
}
25+
}
26+
27+
pub fn send(&mut self) -> Result<AsyncAction, VoucherifyError> {
28+
let url = try!(Url::parse(format!("{}/v1/async-actions/{}",
29+
self.app_url,
30+
self.id)
31+
.as_str()));
32+
33+
let mut response = try!(self.request.execute(Method::Get, url));
34+
35+
let mut json = String::new();
36+
let _ = try!(response.read_to_string(&mut json));
37+
38+
if !response.status.is_success() {
39+
return Err(VoucherifyError::ResponseError(json))
40+
}
41+
42+
match serde_json::from_str(json.as_str()) {
43+
Ok(async_action) => Ok(async_action),
44+
Err(err) => Err(VoucherifyError::JsonParse(err)),
45+
}
46+
}
47+
}

src/async_action/list.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::io::Read;
2+
use hyper::Url;
3+
use hyper::method::Method;
4+
5+
use request::VoucherifyRequest;
6+
use utils::error::VoucherifyError;
7+
use async_action::AsyncActionList;
8+
9+
pub struct AsyncActionListRequest {
10+
request: VoucherifyRequest,
11+
app_url: String,
12+
13+
limit: u32,
14+
end_date: String,
15+
}
16+
17+
impl AsyncActionListRequest {
18+
pub fn new(request: VoucherifyRequest, app_url: String) -> AsyncActionListRequest {
19+
AsyncActionListRequest {
20+
request,
21+
app_url,
22+
23+
limit: 100,
24+
end_date: String::new(),
25+
}
26+
}
27+
28+
pub fn limit(&mut self, limit: u32) -> &mut AsyncActionListRequest {
29+
self.limit = limit;
30+
self
31+
}
32+
33+
pub fn end_date(&mut self, end_date: &str) -> &mut AsyncActionListRequest {
34+
self.end_date = end_date.to_string();
35+
self
36+
}
37+
38+
pub fn send(&mut self) -> Result<AsyncActionList, VoucherifyError> {
39+
let mut url = try!(Url::parse(format!("{}/v1/async-actions", self.app_url).as_str()));
40+
url.query_pairs_mut()
41+
.clear()
42+
.append_pair("limit", format!("{}", self.limit).as_str());
43+
44+
if !self.end_date.is_empty() {
45+
url.query_pairs_mut().append_pair("end_date", self.end_date.as_str());
46+
}
47+
48+
let mut response = try!(self.request.execute(Method::Get, url));
49+
50+
let mut json = String::new();
51+
let _ = try!(response.read_to_string(&mut json));
52+
53+
match serde_json::from_str(json.as_str()) {
54+
Ok(async_action_list) => Ok(async_action_list),
55+
Err(err) => Err(VoucherifyError::JsonParse(err)),
56+
}
57+
}
58+
}

src/async_action/mod.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
pub mod get;
2+
pub mod list;
3+
4+
use std::collections::BTreeMap;
5+
use serde_json::Value;
6+
7+
#[derive(Default, PartialEq, Debug, Serialize, Deserialize)]
8+
pub struct AsyncActionList {
9+
pub async_actions: Vec<AsyncAction>
10+
}
11+
12+
#[derive(Default, PartialEq, Debug, Serialize, Deserialize)]
13+
pub struct AsyncAction {
14+
pub id: Option<String>,
15+
#[serde(rename = "type")]
16+
pub async_action_type: Option<String>,
17+
pub status: Option<String>,
18+
pub result: Option<BTreeMap<String, Value>>,
19+
pub created_at: Option<String>,
20+
}
21+
22+
impl AsyncAction {
23+
pub fn new() -> AsyncAction {
24+
AsyncAction::default()
25+
}
26+
27+
pub fn id(mut self, id: String) -> AsyncAction {
28+
self.id = Some(id);
29+
self
30+
}
31+
32+
pub fn async_action_type(mut self, async_action_type: String) -> AsyncAction {
33+
self.async_action_type = Some(async_action_type);
34+
self
35+
}
36+
37+
pub fn status(mut self, status: String) -> AsyncAction {
38+
self.status = Some(status);
39+
self
40+
}
41+
42+
pub fn result(mut self, result: BTreeMap<String, Value>) -> AsyncAction {
43+
self.result = Some(result);
44+
self
45+
}
46+
47+
pub fn created_at(mut self, created_at: String) -> AsyncAction {
48+
self.created_at = Some(created_at);
49+
self
50+
}
51+
52+
pub fn build(self) -> AsyncAction {
53+
self
54+
}
55+
}

src/lib.rs

+44
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ extern crate hyper;
66
extern crate hyper_native_tls;
77

88
pub mod request;
9+
pub mod async_action;
910
pub mod voucher;
1011
pub mod utils;
1112

13+
use async_action::get::AsyncActionGetRequest;
14+
use async_action::list::AsyncActionListRequest;
1215
use request::VoucherifyRequest;
1316
use voucher::create::VoucherCreateRequest;
1417
use voucher::get::VoucherGetRequest;
@@ -212,4 +215,45 @@ impl Voucherify {
212215
let new_request = VoucherifyRequest::new(&self.app_id, &self.app_token);
213216
VoucherDisableRequest::new(new_request, voucher_id, self.app_url.to_string())
214217
}
218+
219+
/// Gets single Async Action by id.
220+
///
221+
/// # Example
222+
///
223+
/// ```
224+
/// use voucherify_rs::Voucherify;
225+
/// use voucherify_rs::voucher::AsyncAction;
226+
///
227+
/// let voucherify = Voucherify::new("c70a6f00-cf91-4756-9df5-47628850002b",
228+
/// "3266b9f8-e246-4f79-bdf0-833929b1380c");
229+
///
230+
/// let async_action: AsyncAction = voucherify.async_action_get("id").send().unwrap();
231+
///
232+
/// assert_eq!(async_action.id.unwrap(), "id");
233+
/// ```
234+
pub fn async_action_get(&self, id: &str) -> AsyncActionGetRequest {
235+
let new_request = VoucherifyRequest::new(&self.app_id, &self.app_token);
236+
AsyncActionGetRequest::new(new_request, id, self.app_url.to_string())
237+
}
238+
239+
/// Gets a list of Async Actions.
240+
///
241+
/// # Example
242+
///
243+
/// ```
244+
/// use voucherify_rs::Voucherify;
245+
/// use voucherify_rs::voucher::AsyncAction;
246+
///
247+
/// let voucherify = Voucherify::new("c70a6f00-cf91-4756-9df5-47628850002b",
248+
/// "3266b9f8-e246-4f79-bdf0-833929b1380c");
249+
///
250+
/// let async_action_list: Vec<AsyncAction> = voucherify.async_action_list().limit(5).endDate("2021-07-16T16:10:28Z").send().unwrap();
251+
///
252+
/// assert!(async_action_list.len() <= 5);
253+
/// ```
254+
pub fn async_action_list(&self) -> AsyncActionListRequest {
255+
let new_request = VoucherifyRequest::new(&self.app_id, &self.app_token);
256+
AsyncActionListRequest::new(new_request, self.app_url.to_string())
257+
}
258+
215259
}

0 commit comments

Comments
 (0)