This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 23:42:39 +03:00
parent bf6966bd67
commit 0c64f098c6
9 changed files with 136 additions and 14 deletions

View file

@ -4,6 +4,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.12.9", features = ["multipart"] }
chrono = { version = "0.4.38", features = ["serde"] }
reqwest = { version = "0.12.9", features = ["json"] }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
sha3 = "0.10.8"
tokio = { version = "1.41.1", features = ["full"] }

31
client/src/file.rs Normal file
View file

@ -0,0 +1,31 @@
use tokio::{
fs::{read_dir, remove_file, File},
io::AsyncWriteExt,
};
const FILE_LOCATION: &str = "~/.local/share/";
pub async fn save_package(package_name: String, package_data: &[u8]) -> Result<(), std::io::Error> {
let mut package_file = File::create_new(format!("{}{}", FILE_LOCATION, package_name)).await?;
package_file.write_all(package_data).await
}
pub async fn delete_package(package_name: String) -> Result<(), std::io::Error> {
remove_file(format!("{}{}", FILE_LOCATION, package_name)).await
}
pub async fn list_packages() -> Option<Vec<String>> {
let mut folder_elements = read_dir(FILE_LOCATION).await.ok()?;
let mut packages = vec![];
loop {
match folder_elements.next_entry().await.ok()? {
Some(file_entry) => packages.push(file_entry.file_name().into_string().ok()?),
None => break,
}
}
if packages.is_empty() {
return None;
}
Some(packages)
}

4
client/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod file;
pub mod package;
pub mod request;
mod test;

27
client/src/package.rs Normal file
View file

@ -0,0 +1,27 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Package {
name: String,
publisher: Publisher,
version: Version,
size: u64,
hash: Vec<u8>,
dependencies: Vec<String>,
publish_date_time: DateTime<Utc>,
last_update_date_time: DateTime<Utc>,
location: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Version {
first: u8,
second: u8,
third: u8,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Publisher {
name: String,
}

47
client/src/request.rs Normal file
View file

@ -0,0 +1,47 @@
use std::{error::Error, sync::LazyLock};
use crate::package::Package;
static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(reqwest::Client::new);
const URL: &str = "http://localhost:2345";
pub async fn read_all_packages() -> Result<Vec<Package>, Box<dyn Error>> {
Ok(CLIENT
.get(format!("{}{}", URL, "/packages"))
.send()
.await
.unwrap()
.json::<serde_json::Value>()
.await?
.as_array()
.map_or(Err(""), |values| Ok(values))?
.iter()
.map(|value| serde_json::from_value::<Package>(value.clone()).unwrap())
.collect())
}
pub async fn read_package(package_name: String) -> Option<Package> {
CLIENT
.get(format!("{}{}/{}", URL, "/packages", package_name))
.send()
.await
.ok()?
.json()
.await
.ok()?
}
pub async fn download_package(package_name: String) -> Option<Vec<u8>> {
Some(
CLIENT
.get(format!("{}{}/{}", URL, "/packages", package_name))
.send()
.await
.ok()?
.text()
.await
.ok()?
.as_bytes()
.to_vec(),
)
}

12
client/src/test.rs Normal file
View file

@ -0,0 +1,12 @@
#[cfg(test)]
use tokio::test;
#[test]
async fn test_list_packages() {
use crate::request::read_all_packages;
let packages = read_all_packages().await;
println!("{:#?}", packages);
assert_eq!(packages.is_ok(), true);
}

View file

@ -13,7 +13,12 @@ static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
pub async fn establish_connection() -> Result<(), surrealdb::Error> {
DB.connect::<Ws>("localhost:8000").await?;
DB.use_ns("Packages").await?;
DB.use_db("Packages").await
DB.use_db("Packages").await?;
DB.query("DEFINE TABLE Packages")
.await.map(|_| ())?;
DB.query("DEFINE INDEX package_nameINDEX on TABLE Packages COLUMNS package_name UNIQUE").await.map(|_| ())
}
pub async fn is_alive() -> bool {
@ -27,29 +32,24 @@ pub async fn is_alive() -> bool {
}
pub async fn create_package(package: Package) -> Option<Package> {
DB.create::<Option<Package>>(package.get_name())
DB.create::<Option<Package>>(("Packages", package.get_name()))
.content(package)
.await
.map_or_else(|_| None, |package| package)
}
pub async fn read_package(package_name: String) -> Option<Package> {
DB.select::<Vec<Package>>(package_name)
.await
.map_or_else(|_| None, |mut package| package.pop())
DB.select(("Packages", package_name)).await.ok()?
}
pub async fn update_package(package_name: String, package: Package) -> Option<Package> {
DB.update::<Vec<Package>>(package_name)
DB.update(("Packages", package_name))
.content(package)
.await
.map_or_else(|_| None, |mut package| package.pop())
.await.ok()?
}
pub async fn delete_package(package_name: String) -> Option<Package> {
DB.delete::<Vec<Package>>(package_name)
.await
.map_or_else(|_| None, |mut package| package.pop())
DB.delete(("Packages", package_name)).await.ok()?
}
pub async fn read_all_packages() -> Option<Vec<Package>> {

View file

@ -1 +0,0 @@

View file

@ -1,5 +1,4 @@
pub mod database;
pub mod http;
pub mod package;
pub mod routing;