Skip to content

Commit babe01a

Browse files
committed
Auto-fill token labels
1 parent 34e87f1 commit babe01a

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

src/command/admin.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ use serde_json::json;
66
pub struct LoginArgs {
77
pub username: String,
88
pub password: String,
9-
pub label: String,
109
}
1110

1211
pub fn login(args: &LoginArgs) -> Result<()> {
1312
let res = rpc::call(
1413
"create_api_key",
15-
json!({"username": args.username, "password": args.password, "label": args.label}),
14+
json!({
15+
"username": args.username,
16+
"password": args.password,
17+
"label": "Created by btcmap-cli"
18+
}),
1619
)?;
1720
let res = res.result.unwrap();
1821
let api_key = res["token"].as_str().unwrap();

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn main() -> Result<()> {
136136
}
137137

138138
if settings::get_str("password")?.is_empty() {
139-
Err("you need to login first, run btcmap-cli login <password>")?;
139+
Err("you need to login first, run btcmap-cli login <username> <password>")?;
140140
}
141141

142142
let command = match &cli.command {

src/rpc.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use crate::verbosity;
33
use crate::Result;
44
use colored_json::ToColoredJson;
55
use serde::Deserialize;
6+
use serde::Serialize;
67
use serde_json::{json, Value};
8+
use std::i64;
79

8-
#[derive(Deserialize)]
10+
#[derive(Serialize, Deserialize)]
911
pub struct RpcResponse {
1012
pub result: Option<Value>,
1113
pub error: Option<Value>,
@@ -28,36 +30,67 @@ impl RpcResponse {
2830
}
2931

3032
pub fn call(method: &str, params: Value) -> Result<RpcResponse> {
31-
if verbosity() > 0 {
32-
println!(
33-
"{}",
34-
serde_json::to_string(&params)?.to_colored_json_auto()?
35-
);
33+
match verbosity() {
34+
i64::MIN..=0 => {}
35+
1..=i64::MAX => {
36+
println!("Calling method {method} with the following params:");
37+
println!(
38+
"{}",
39+
serde_json::to_string(&params)?.to_colored_json_auto()?
40+
);
41+
}
3642
}
3743
let params = params
3844
.as_object()
3945
.ok_or("params value is not a valid JSON object")?;
40-
let args = json!(
46+
let req_body = json!(
4147
{"jsonrpc": "2.0", "method": method, "params": params, "id": 1}
4248
);
4349
let mut api_url = settings::get_str("api_url")?;
4450
if api_url.trim().is_empty() {
4551
api_url = "https://api.btcmap.org/rpc".into();
4652
}
4753
if verbosity() >= 2 {
48-
println!("{}", serde_json::to_string(&args)?.to_colored_json_auto()?);
54+
println!("Full request body:");
55+
println!(
56+
"{}",
57+
serde_json::to_string(&req_body)?.to_colored_json_auto()?
58+
);
4959
}
50-
let response = ureq::post(api_url)
60+
let response: RpcResponse = ureq::post(api_url)
5161
.header("Content-Type", "application/json")
5262
.header(
5363
"Authorization",
5464
format!("Bearer {}", settings::get_str("password")?),
5565
)
56-
.send_json(args)?
66+
.send_json(req_body)?
5767
.body_mut()
58-
.read_to_string()?;
59-
if verbosity() >= 2 {
60-
println!("{}", response);
61-
}
62-
Ok(serde_json::from_str(&response)?)
68+
.read_json()?;
69+
match verbosity() {
70+
i64::MIN..=0 => {}
71+
1..=2 => {
72+
if response.result.is_some() {
73+
println!("RPC result:");
74+
println!(
75+
"{}",
76+
serde_json::to_string(&response.result)?.to_colored_json_auto()?
77+
);
78+
}
79+
if response.error.is_some() {
80+
println!("RPC error:");
81+
println!(
82+
"{}",
83+
serde_json::to_string(&response.error)?.to_colored_json_auto()?
84+
);
85+
}
86+
}
87+
3..=i64::MAX => {
88+
println!("RPC response:");
89+
println!(
90+
"{}",
91+
serde_json::to_string(&response)?.to_colored_json_auto()?
92+
);
93+
}
94+
};
95+
Ok(response)
6396
}

0 commit comments

Comments
 (0)