|
| 1 | +use arboard::Clipboard; |
| 2 | +use console::Term; |
| 3 | +use dialoguer::{FuzzySelect, theme::ColorfulTheme}; |
| 4 | +use reqwest::Client; |
| 5 | + |
| 6 | +use crate::types::{CHAINS, Key, RedactedKey}; |
| 7 | + |
| 8 | +pub async fn get_keys_interactive( |
| 9 | + client: &Client, |
| 10 | + term: &mut Term, |
| 11 | + _unsafe_op: bool, |
| 12 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 13 | + let mut clipboard = Clipboard::new()?; |
| 14 | + |
| 15 | + let chain = FuzzySelect::with_theme(&ColorfulTheme::default()) |
| 16 | + .with_prompt("Which chain would you like to use?") |
| 17 | + .items(&CHAINS) |
| 18 | + .interact()?; |
| 19 | + |
| 20 | + let api_keys: Vec<Key> = client |
| 21 | + .get("https://api.cloud.developerdao.com/api/keys") |
| 22 | + .send() |
| 23 | + .await? |
| 24 | + .error_for_status()? |
| 25 | + .json::<Vec<Key>>() |
| 26 | + .await?; |
| 27 | + |
| 28 | + term.flush()?; |
| 29 | + |
| 30 | + if api_keys.is_empty() { |
| 31 | + let new_key = Key { |
| 32 | + apikey: client |
| 33 | + .post("https://api.cloud.developerdao.com/api/keys") |
| 34 | + .send() |
| 35 | + .await? |
| 36 | + .error_for_status()? |
| 37 | + .text() |
| 38 | + .await?, |
| 39 | + }; |
| 40 | + |
| 41 | + let redacted_key = new_key.as_redacted(); |
| 42 | + let redacted_ep = format!( |
| 43 | + "https://api.cloud.developerdao.com/rpc/{}/{}", |
| 44 | + CHAINS[chain].id(), |
| 45 | + redacted_key |
| 46 | + ); |
| 47 | + |
| 48 | + let endpoint = format!( |
| 49 | + "https://api.cloud.developerdao.com/rpc/{}/{}", |
| 50 | + CHAINS[chain].id(), |
| 51 | + new_key |
| 52 | + ); |
| 53 | + |
| 54 | + println!("\nYour RPC Endpoint for {}:\n", CHAINS[chain]); |
| 55 | + println!("\n{redacted_ep}\n",); |
| 56 | + |
| 57 | + match _unsafe_op { |
| 58 | + true => { |
| 59 | + term.clear_screen()?; |
| 60 | + println!("{endpoint}") |
| 61 | + } |
| 62 | + false => { |
| 63 | + clipboard.set_text(&endpoint)?; |
| 64 | + println!("\nYour endpoint is now copied to your clipboard"); |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + let redacted_keys = api_keys |
| 69 | + .iter() |
| 70 | + .map(|e| e.as_redacted()) |
| 71 | + .collect::<Vec<RedactedKey>>(); |
| 72 | + |
| 73 | + let index = dialoguer::Select::with_theme(&ColorfulTheme::default()) |
| 74 | + .items(&redacted_keys) |
| 75 | + .with_prompt("Select an API key to copy") |
| 76 | + .interact()?; |
| 77 | + |
| 78 | + let redacted_ep = format!( |
| 79 | + "https://api.cloud.developerdao.com/rpc/{}/{}", |
| 80 | + CHAINS[chain].id(), |
| 81 | + redacted_keys[index].redacted, |
| 82 | + ); |
| 83 | + |
| 84 | + let endpoint = format!( |
| 85 | + "https://api.cloud.developerdao.com/rpc/{}/{}", |
| 86 | + CHAINS[chain].id(), |
| 87 | + api_keys[index].apikey |
| 88 | + ); |
| 89 | + |
| 90 | + println!("\nYour RPC Endpoint for {}:\n", CHAINS[chain]); |
| 91 | + println!("\n{redacted_ep}\n",); |
| 92 | + |
| 93 | + match _unsafe_op { |
| 94 | + true => { |
| 95 | + term.clear_screen()?; |
| 96 | + println!("{endpoint}") |
| 97 | + } |
| 98 | + false => { |
| 99 | + clipboard.set_text(&endpoint)?; |
| 100 | + println!("\nYour endpoint is now copied to your clipboard"); |
| 101 | + term.write_line("")?; |
| 102 | + term.write_line("")?; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
0 commit comments