improved some api commands
This commit is contained in:
parent
c8d7c41f33
commit
0f930802f5
|
@ -197,6 +197,7 @@ async fn query_all(
|
|||
}
|
||||
}
|
||||
|
||||
/// Generates random API key. It's like a long, strong password. I guess.
|
||||
pub async fn generate_api_key() -> String {
|
||||
use rand::Rng;
|
||||
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
||||
|
|
|
@ -63,25 +63,49 @@ pub async fn dispatch(args: Args, conf: &Config) {
|
|||
}
|
||||
Command::ShowData => todo!(),
|
||||
Command::Info => todo!(),
|
||||
Command::Api { subcommand } => match subcommand {
|
||||
ApiCommand::Generate { comment } => {
|
||||
let key = api::generate_api_key().await;
|
||||
let db = DB::new(&conf.db).await.unwrap();
|
||||
let res = comment.unwrap_or(String::new());
|
||||
match db.add_key(&key, &res).await {
|
||||
Ok(_) => {
|
||||
info!("Added api key to database. Copy your key:");
|
||||
println!("{key}");
|
||||
Command::Api { subcommand } => {
|
||||
let db = DB::new(&conf.db).await.unwrap();
|
||||
match subcommand {
|
||||
ApiCommand::Generate { comment } => {
|
||||
let key = api::generate_api_key().await;
|
||||
let res = comment.unwrap_or(String::new());
|
||||
match db.add_key(&key, &res).await {
|
||||
Ok(_) => {
|
||||
info!("Added api key to database. Copy your key:");
|
||||
println!("{key}");
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("{e:?}");
|
||||
println!("Error generating/adding key.")
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
ApiCommand::List => match db.get_all_keys().await {
|
||||
Ok(keys) => {
|
||||
for key in keys {
|
||||
println!(
|
||||
"id: {} key: {} comment: {}",
|
||||
key.key_id, key.key, key.comment
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("{e:?}");
|
||||
println!("Error generating/adding key.")
|
||||
println!("Error getting key list.");
|
||||
}
|
||||
};
|
||||
},
|
||||
ApiCommand::Delete { id } => match db.delete_key(id).await {
|
||||
Ok(_) => {
|
||||
println!("Deleted key with id: {id}.");
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("{e:?}");
|
||||
println!("Could not delete or find key.");
|
||||
}
|
||||
},
|
||||
}
|
||||
ApiCommand::List => todo!(),
|
||||
ApiCommand::Delete => todo!(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ pub struct Args {
|
|||
///1: Info
|
||||
///2: Debug
|
||||
///3: Trace
|
||||
#[arg(short, long, default_value_t = 1)]
|
||||
#[arg(short, long, default_value_t = 0)]
|
||||
pub verbosity: u8,
|
||||
|
||||
#[command(subcommand)]
|
||||
|
@ -68,5 +68,8 @@ pub enum ApiCommand {
|
|||
/// Shows all API keys
|
||||
List,
|
||||
/// Remove/revoke an API key.
|
||||
Delete,
|
||||
Delete {
|
||||
/// ID of the api key to remove. Use `dasher api list` to get the id.
|
||||
id: i64,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -91,10 +91,31 @@ impl DB {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: Hash keys!
|
||||
|
||||
/// Adds API key.
|
||||
pub async fn add_key(&self, api_key: &str, comment: &str) -> Result<(), DBError> {
|
||||
sqlx::query("INSERT INTO api_keys ( key, comment ) VALUES ( ?1, ?2 )")
|
||||
.bind(api_key)
|
||||
.bind(comment)
|
||||
.execute(&(self.0))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Deletes an API key.
|
||||
pub async fn delete_key(&self, key_id: i64) -> Result<(), DBError> {
|
||||
sqlx::query("DELETE FROM api_keys WHERE key_id=?1")
|
||||
.bind(key_id)
|
||||
.execute(&(self.0))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets API key.
|
||||
pub async fn get_key(&self, api_key: &str) -> Result<ApiKey, DBError> {
|
||||
let key = sqlx::query_as::<_, ApiKey>(&format!(
|
||||
"SELECT key, comment FROM api_keys WHERE key=?1;"
|
||||
"SELECT key_id, key, comment FROM api_keys WHERE key=?1;"
|
||||
))
|
||||
.bind(api_key)
|
||||
.fetch_one(&(self.0))
|
||||
|
@ -102,14 +123,12 @@ impl DB {
|
|||
Ok(key)
|
||||
}
|
||||
|
||||
/// Adds API key.
|
||||
pub async fn add_key(&self, api_key: &str, comment: &str) -> Result<(), DBError> {
|
||||
let added_key = sqlx::query("INSERT INTO api_keys ( key, comment ) VALUES ( ?1, ?2 )")
|
||||
.bind(api_key)
|
||||
.bind(comment)
|
||||
.execute(&(self.0))
|
||||
/// Gets all API keys.
|
||||
pub async fn get_all_keys(&self) -> Result<Vec<ApiKey>, DBError> {
|
||||
let keys = sqlx::query_as::<_, ApiKey>("SELECT key_id, key, comment FROM api_keys")
|
||||
.fetch_all(&(self.0))
|
||||
.await?;
|
||||
Ok(())
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
/// Adds a certain value to the dataset <name>. This function performs lookup
|
||||
|
|
|
@ -15,6 +15,7 @@ pub struct Entry {
|
|||
|
||||
#[derive(Default, Serialize, Debug, FromRow)]
|
||||
pub struct ApiKey {
|
||||
pub key_id: i64,
|
||||
pub key: String,
|
||||
pub comment: String,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue