Struct smol_db_client::SmolDbClient
source · pub struct SmolDbClient { /* private fields */ }
Expand description
SmolDbClient
struct used for communicating to the database.
This struct has implementations that allow for end to end communication with the database server.
Implementations§
source§impl SmolDbClient
impl SmolDbClient
sourcepub fn new(ip: &str) -> Result<Self, ClientError>
pub fn new(ip: &str) -> Result<Self, ClientError>
Creates a new SmolDBClient
struct connected to the ip address given.
use smol_db_client::SmolDbClient;
// create the new client
let mut client = SmolDbClient::new("localhost:8222").unwrap();
// client should be functional provided a database server was able to be connected to at the given location
sourcepub fn setup_encryption(
&mut self
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn setup_encryption( &mut self ) -> Result<DBSuccessResponse<String>, ClientError>
Requests the server to use encryption for communication. Encryption is done both ways, and is done using RSA with a 2048 bit key This function is slow due to large rsa key size ~1-4 seconds to generate the key Encryption is done invisibly.
use smol_db_client::SmolDbClient;
use smol_db_common::prelude::DBSettings;
let key = "test_key_123";
let mut client = SmolDbClient::new("localhost:8222").unwrap();
client.set_access_key(key.to_string()).unwrap();
client.setup_encryption().unwrap(); // encryption is done invisibly after it is setup, nothing else is needed :)
client.create_db("docsetup_encryption_test",DBSettings::default()).unwrap();
let _ = client.delete_db("docsetup_encryption_test").unwrap();
sourcepub fn is_encryption_enabled(&self) -> bool
pub fn is_encryption_enabled(&self) -> bool
Returns true if end to end encryption is enabled
sourcepub fn reconnect(&mut self) -> Result<(), ClientError>
pub fn reconnect(&mut self) -> Result<(), ClientError>
Reconnects the client, this will reset the session, which can be used to remove any key that was used. Or to reconnect in the event of a loss of connection
use smol_db_client::SmolDbClient;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
// disconnecting is optional between reconnects
client.disconnect().unwrap();
client.reconnect().unwrap();
// as shown here
client.reconnect().unwrap();
sourcepub fn get_connected_ip(&self) -> Result<SocketAddr>
pub fn get_connected_ip(&self) -> Result<SocketAddr>
Returns a result containing the peer address of this client
sourcepub fn disconnect(&self) -> Result<()>
pub fn disconnect(&self) -> Result<()>
Disconnects the socket from the database.
use smol_db_client::SmolDbClient;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
// disconnect the client
let _ = client.disconnect().expect("Failed to disconnect socket");
sourcepub fn delete_data(
&mut self,
db_name: &str,
db_location: &str
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn delete_data( &mut self, db_name: &str, db_location: &str ) -> Result<DBSuccessResponse<String>, ClientError>
Deletes the data at the given db location, requires permissions to do so.
use smol_db_client::client_error::ClientError;
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_packet_response::DBPacketResponseError;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_delete_data",DBSettings::default()).unwrap();
let _ = client.write_db("doctest_delete_data","cool_data_location","cool_data");
let read_data1 = client.read_db("doctest_delete_data","cool_data_location").unwrap().as_option().unwrap().to_string();
assert_eq!(read_data1.as_str(),"cool_data");
// delete the data at the given location
let _ = client.delete_data("doctest_delete_data","cool_data_location").unwrap();
let read_data2 = client.read_db("doctest_delete_data","cool_data_location");
assert_eq!(read_data2.unwrap_err(),ClientError::DBResponseError(DBPacketResponseError::ValueNotFound)); // is err here means DBResponseError(ValueNotFound)
let _ = client.delete_db("doctest_delete_data").unwrap();
sourcepub fn get_role(&mut self, db_name: &str) -> Result<Role, ClientError>
pub fn get_role(&mut self, db_name: &str) -> Result<Role, ClientError>
Returns the role of the given client in the given db.
use smol_db_client::SmolDbClient;
use smol_db_common::db::Role;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_get_role",DBSettings::default()).unwrap();
// get the given clients role from a db
let role = client.get_role("doctest_get_role").unwrap();
assert_eq!(role,Role::SuperAdmin);
let _ = client.delete_db("doctest_get_role").unwrap();
sourcepub fn get_db_settings(
&mut self,
db_name: &str
) -> Result<DBSettings, ClientError>
pub fn get_db_settings( &mut self, db_name: &str ) -> Result<DBSettings, ClientError>
Gets the DBSettings
of the given DB.
Error on IO error, or when database name does not exist, or when the user lacks permissions to view DBSettings
.
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_get_db_settings",DBSettings::default()).unwrap();
// get the db settings
let settings = client.get_db_settings("doctest_get_db_settings").unwrap();
assert_eq!(settings,DBSettings::default());
let _ = client.delete_db("doctest_get_db_settings").unwrap();
sourcepub fn set_db_settings(
&mut self,
db_name: &str,
db_settings: DBSettings
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn set_db_settings( &mut self, db_name: &str, db_settings: DBSettings ) -> Result<DBSuccessResponse<String>, ClientError>
Sets the DBSettings
of a given DB
Error on IO Error, or when database does not exist, or when the user lacks permissions to set DBSettings
use std::time::Duration;
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_set_db_settings",DBSettings::default()).unwrap();
// set the new db settings
let new_settings = DBSettings::new(Duration::from_secs(10),(true,false,true),(false,false,false),vec![],vec![]);
let _ = client.set_db_settings("doctest_set_db_settings",new_settings.clone()).unwrap();
let settings = client.get_db_settings("doctest_set_db_settings").unwrap();
assert_eq!(settings,new_settings);
let _ = client.delete_db("doctest_set_db_settings").unwrap();
sourcepub fn set_access_key(
&mut self,
key: String
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn set_access_key( &mut self, key: String ) -> Result<DBSuccessResponse<String>, ClientError>
Sets this clients access key within the DB Server. The server will persist the key until the session is disconnected, or connection is lost.
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
// sets the access key of the given client
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
sourcepub fn create_db(
&mut self,
db_name: &str,
db_settings: DBSettings
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn create_db( &mut self, db_name: &str, db_settings: DBSettings ) -> Result<DBSuccessResponse<String>, ClientError>
Creates a db through the client with the given name. Error on IO Error, or when the user lacks permissions to create a DB
use std::time::Duration;
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_create_db",DBSettings::default()).unwrap();
let _ = client.delete_db("doctest_create_db").unwrap();
sourcepub fn write_db(
&mut self,
db_name: &str,
db_location: &str,
data: &str
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn write_db( &mut self, db_name: &str, db_location: &str, data: &str ) -> Result<DBSuccessResponse<String>, ClientError>
Writes to a db at the location specified, with the data given as a string. Returns the data in the location that was over written if there was any. Requires permissions to write to the given DB
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_write_data",DBSettings::default()).unwrap();
// write the given data to the given location within the specified db
let _ = client.write_db("doctest_write_data","cool_data_location","cool_data");
let read_data1 = client.read_db("doctest_write_data","cool_data_location").unwrap().as_option().unwrap().to_string();
assert_eq!(read_data1.as_str(),"cool_data");
let _ = client.delete_db("doctest_write_data").unwrap();
sourcepub fn read_db(
&mut self,
db_name: &str,
db_location: &str
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn read_db( &mut self, db_name: &str, db_location: &str ) -> Result<DBSuccessResponse<String>, ClientError>
Reads from a db at the location specific. Returns an error if there is no data in the location. Requires permissions to read from the given DB
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_read_db",DBSettings::default()).unwrap();
let _ = client.write_db("doctest_read_db","cool_data_location","cool_data");
// read the given database at the given location
let read_data1 = client.read_db("doctest_read_db","cool_data_location").unwrap().as_option().unwrap().to_string();
assert_eq!(read_data1.as_str(),"cool_data");
let _ = client.delete_db("doctest_read_db").unwrap();
sourcepub fn delete_db(
&mut self,
db_name: &str
) -> Result<DBSuccessResponse<String>, ClientError>
pub fn delete_db( &mut self, db_name: &str ) -> Result<DBSuccessResponse<String>, ClientError>
Deletes the given db by name. Requires super admin privileges on the given DB Server
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_delete_db",DBSettings::default()).unwrap();
// delete the db with the given name
let _ = client.delete_db("doctest_delete_db").unwrap();
sourcepub fn list_db(&mut self) -> Result<Vec<DBPacketInfo>, ClientError>
pub fn list_db(&mut self) -> Result<Vec<DBPacketInfo>, ClientError>
Lists all the current databases available by name from the server Only error on IO Error
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_packet_info::DBPacketInfo;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_list_db1",DBSettings::default()).unwrap();
// get list of databases currently on the server
let list_of_dbs1 = client.list_db().unwrap();
assert!(list_of_dbs1.contains(&DBPacketInfo::new("doctest_list_db1")));
assert!(!list_of_dbs1.contains(&DBPacketInfo::new("doctest_list_db2")));
let _ = client.create_db("doctest_list_db2",DBSettings::default()).unwrap();
// newly created databases show up after getting another copy of the list
let list_of_dbs2 = client.list_db().unwrap();
assert!(list_of_dbs2.contains(&DBPacketInfo::new("doctest_list_db2")));
assert!(list_of_dbs2.contains(&DBPacketInfo::new("doctest_list_db1")));
let _ = client.delete_db("doctest_list_db1").unwrap();
let _ = client.delete_db("doctest_list_db2").unwrap();
sourcepub fn list_db_contents(
&mut self,
db_name: &str
) -> Result<HashMap<String, String>, ClientError>
pub fn list_db_contents( &mut self, db_name: &str ) -> Result<HashMap<String, String>, ClientError>
Get the hashmap of the contents of a database. Contents are always String:String for the hashmap. Requires list permissions on the given DB
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;
let mut client = SmolDbClient::new("localhost:8222").unwrap();
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_list_cont_db",DBSettings::default()).unwrap();
let _ = client.write_db("doctest_list_cont_db","cool_data_location","cool_data");
let contents = client.list_db_contents("doctest_list_cont_db").unwrap();
assert_eq!(contents.len(),1);
assert_eq!(contents.get("cool_data_location").unwrap().as_str(),"cool_data");
let _ = client.delete_db("doctest_list_cont_db").unwrap();
sourcepub fn list_db_contents_generic<T>(
&mut self,
db_name: &str
) -> Result<HashMap<String, T>, ClientError>where
for<'a> T: Serialize + Deserialize<'a>,
pub fn list_db_contents_generic<T>(
&mut self,
db_name: &str
) -> Result<HashMap<String, T>, ClientError>where
for<'a> T: Serialize + Deserialize<'a>,
Lists the given db’s contents, deserializing the contents into a hash map.
sourcepub fn write_db_generic<T>(
&mut self,
db_name: &str,
db_location: &str,
data: T
) -> Result<DBSuccessResponse<T>, ClientError>where
for<'a> T: Serialize + Deserialize<'a>,
pub fn write_db_generic<T>(
&mut self,
db_name: &str,
db_location: &str,
data: T
) -> Result<DBSuccessResponse<T>, ClientError>where
for<'a> T: Serialize + Deserialize<'a>,
Writes to the db while serializing the given data, returning the data at the location given and deserialized to the same type.
sourcepub fn read_db_generic<T>(
&mut self,
db_name: &str,
db_location: &str
) -> Result<DBSuccessResponse<T>, ClientError>where
for<'a> T: Serialize + Deserialize<'a>,
pub fn read_db_generic<T>(
&mut self,
db_name: &str,
db_location: &str
) -> Result<DBSuccessResponse<T>, ClientError>where
for<'a> T: Serialize + Deserialize<'a>,
Reads from db and tries to deserialize the content at the location to the given generic