Skip to content

Commit

Permalink
WIP: moving API in separate mod.
Browse files Browse the repository at this point in the history
  • Loading branch information
korvyashka committed Aug 27, 2024
1 parent df22f76 commit 56b7eb8
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 134 deletions.
26 changes: 26 additions & 0 deletions src/api/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::api::Api;
use crate::commands::CommandError;
use crate::commands;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct AssetSignature {
pub(crate) signature: String,
}

impl Api {
pub fn get_version_asset_signatures(
&self,
app_id: &str,
revision: u32,
) -> Result<Vec<AssetSignature>, CommandError> {
Ok(serde_json::from_value(commands::get(
&self.authentication,
&format!(
"v4/assets?select=signature&app_id=eq.{}&app_revision=eq.{}&type=eq.edge-app-file",
app_id, revision
),
)?)?)
}
}
46 changes: 46 additions & 0 deletions src/api/edge_app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::api::Api;
use crate::commands::CommandError;
use crate::commands;

use serde_json::json;
use serde::{Deserialize, Serialize};


#[derive(Debug)]
pub struct EdgeApps {
pub value: serde_json::Value,
}

impl Api {
pub fn create_app(&self, name: String) -> Result<String, CommandError> {
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ApiResponse {
#[serde(default)]
pub id: String,
#[serde(default)]
pub name: String,
}

let response = commands::post(
&self.authentication,
"v4/edge-apps?select=id,name",
&json!({ "name": name }),
)?;

let json_response = serde_json::from_value::<Vec<ApiResponse>>(response)?;
let app_id = json_response[0].id.clone();

if app_id.is_empty() {
return Err(CommandError::MissingField);
}

Ok(app_id)
}

pub fn list_apps(&self) -> Result<EdgeApps, CommandError> {
Ok(EdgeApps::new(commands::get(
&self.authentication,
"v4/edge-apps?select=id,name",
)?))
}
}
9 changes: 9 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::authentication::Authentication;

pub mod edge_app;
pub mod version;
pub mod asset;

pub struct Api {
pub authentication: Authentication
}
47 changes: 47 additions & 0 deletions src/api/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::api::Api;
use crate::commands::CommandError;
use crate::commands;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct EdgeAppVersion {
#[serde(default)]
pub user_version: Option<String>,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub icon: Option<String>,
#[serde(default)]
pub author: Option<String>,
#[serde(default)]
pub homepage_url: Option<String>,
#[serde(default)]
pub ready_signal: bool,
#[serde(default)]
pub revision: u32,
}


impl Api {
pub fn get_latest_revision(
&self,
app_id: &str,
) -> Result<Option<EdgeAppVersion>, CommandError> {
let response = commands::get(
&self.authentication,
&format!(
"v4.1/edge-apps/versions?select=user_version,description,icon,author,homepage_url,revision,ready_signal&app_id=eq.{}&order=revision.desc&limit=1",
app_id
),
)?;

let versions: Vec<EdgeAppVersion> =
serde_json::from_value::<Vec<EdgeAppVersion>>(response)?;

if versions.is_empty() {
return Ok(None);
}
Ok(versions.first().cloned())
}
}
Loading

0 comments on commit 56b7eb8

Please sign in to comment.