-
Notifications
You must be signed in to change notification settings - Fork 0
/
charge.rs
83 lines (73 loc) · 2.59 KB
/
charge.rs
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
use clap::{App, Arg, ArgMatches, SubCommand};
use opennode::charge;
use opennode_client::*;
use opennode_client::client::Client;
/// Create a new charge:
/// `cargo run --example charge -- --key=<KEY> create --amount=2000`
///
/// Get a charge with the given id:
/// `cargo run --example charge -- --key=<KEY> get <ID>`
///
/// List paid charges:
/// `cargo run --example charge -- --key=<KEY> list`
#[tokio::main]
async fn main() {
let app = App::new("charge")
.arg(
Arg::with_name("key")
.short("k")
.long("key")
.help("opennode api_key")
.value_name("KEY")
.required(true)
.takes_value(true),
)
.subcommand(
SubCommand::with_name("create")
.about("creates a new charge")
.arg(
Arg::with_name("amount")
.short("a")
.value_name("AMOUNT")
.help("charge amount in satoshis")
.required(true)
.takes_value(true),
),
)
.subcommand(
SubCommand::with_name("get")
.about("retrieve a charge with the given id")
.arg(
Arg::with_name("id")
.value_name("ID")
.help("id of the charge")
.required(true)
.takes_value(true),
),
)
.subcommand(SubCommand::with_name("list").about("retrieve paid charges"));
let matches = app.get_matches();
let api_key = matches.value_of("key").unwrap();
let client = Client::from_url("https://dev-api.opennode.co", api_key);
match matches.subcommand() {
("create", Some(m)) => create(m, &client).await,
("get", Some(m)) => get(m, &client).await,
("list", _) => list(&client).await,
_ => (),
}
}
async fn create(matches: &ArgMatches<'_>, client: &Client) {
let a = matches.value_of("amount").unwrap();
let amount = a.parse::<u64>().unwrap();
let charge: charge::Charge = create_charge(&client, charge::Payload::new(amount)).await.unwrap();
println!("{:?}", charge)
}
async fn get(matches: &ArgMatches<'_>, client: &Client) {
let id = matches.value_of("id").unwrap();
let charge: charge::Charge = get_charge(&client, id).await.unwrap();
println!("{:?}", charge)
}
async fn list(client: &Client) {
let charges: Vec<charge::Charge> = list_charges(&client).await.unwrap();
println!("{:?}", charges)
}