Skip to content

Commit

Permalink
Adds: api functions for all edge apps commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
korvyashka committed Aug 30, 2024
1 parent 219efc2 commit 03e3aeb
Show file tree
Hide file tree
Showing 18 changed files with 927 additions and 689 deletions.
23 changes: 23 additions & 0 deletions src/api/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ use crate::commands::CommandError;
use crate::commands;

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

Check failure on line 6 in src/api/asset.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `serde_json::Value`

error: unused import: `serde_json::Value` --> src/api/asset.rs:6:5 | 6 | use serde_json::Value; | ^^^^^^^^^^^^^^^^^

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

impl Api {
pub fn get_version_asset_signatures(
Expand All @@ -23,4 +30,20 @@ impl Api {
),
)?)?)
}

pub fn get_processing_statuses(
&self,
app_id: &str,
revision: u32,
) -> Result<Vec<AssetProcessingStatus>, CommandError> {
let response = commands::get(
&self.authentication,
&format!(
"v4/assets?select=status,processing_error,title&app_id=eq.{}&app_revision=eq.{}&status=neq.finished",
app_id, revision
),
)?;

Ok(serde_json::from_value::<Vec<AssetProcessingStatus>>(response)?)
}
}
46 changes: 0 additions & 46 deletions src/api/edge_app.rs

This file was deleted.

87 changes: 87 additions & 0 deletions src/api/edge_app/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use log::debug;

use crate::api::Api;
use crate::commands::CommandError;
use crate::commands;

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


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

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct EdgeApp {
pub name: String,
}

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&deleted=eq.false",
)?))
}

pub fn delete_app(&self, app_id: &str) -> Result<(), CommandError> {
commands::delete(
&self.authentication,
&format!("v4/edge-apps?id=eq.{}", app_id),
)?;
Ok(())
}

pub fn update_app(&self, app_id: &str, name: &str) -> Result<(), CommandError> {
commands::patch(
&self.authentication,
&format!("v4/edge-apps?select=name&id=eq.{}", app_id),
&json!({ "name": name }),
)?;
Ok(())
}

pub fn get_app(&self, app_id: &str) -> Result<EdgeApp, CommandError> {
let response = commands::get(
&self.authentication,
&format!("v4/edge-apps?select=name&id=eq.{}", app_id),
)?;

Ok(serde_json::from_value::<Vec<EdgeApp>>(response)?[0].clone())
}

pub fn copy_assets(&self, payload: Value) -> Result<Vec<String>, CommandError> {
let response = commands::post(&self.authentication, "v4/edge-apps/copy-assets", &payload)?;
let copied_assets = serde_json::from_value::<Vec<String>>(response)?;

debug!("Copied assets: {:?}", copied_assets);
Ok(copied_assets)
}
}
39 changes: 39 additions & 0 deletions src/api/edge_app/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::api::Api;
use crate::commands::CommandError;
use crate::commands;

use serde_json::json;

use serde::Deserialize;

impl Api {
pub fn update_channel(&self, channel: &str, app_id: &str, revision: u32) -> Result<(), CommandError> {
let response = commands::patch(
&self.authentication,
&format!(
"v4/edge-apps/channels?select=channel,app_revision&channel=eq.{}&app_id=eq.{}",
channel, app_id
),
&json!(
{
"app_revision": revision,
}),
)?;

#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
struct Channel {
app_revision: u32,
channel: String,
}

let channels = serde_json::from_value::<Vec<Channel>>(response)?;
if channels.is_empty() {
return Err(CommandError::MissingField);
}
if channels[0].channel != channel || channels[0].app_revision != revision {
return Err(CommandError::MissingField);
}

Ok(())
}
}
111 changes: 111 additions & 0 deletions src/api/edge_app/installation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use crate::api::Api;
use crate::commands::CommandError;
use crate::commands;

use std::collections::HashMap;

Check failure on line 5 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `std::collections::HashMap`

error: unused import: `std::collections::HashMap` --> src/api/edge_app/installation.rs:5:5 | 5 | use std::collections::HashMap; | ^^^^^^^^^^^^^^^^^^^^^^^^^
use std::ops::Not;

Check failure on line 6 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `std::ops::Not`

error: unused import: `std::ops::Not` --> src/api/edge_app/installation.rs:6:5 | 6 | use std::ops::Not; | ^^^^^^^^^^^^^
use std::str::FromStr;

Check failure on line 7 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `std::str::FromStr`

error: unused import: `std::str::FromStr` --> src/api/edge_app/installation.rs:7:5 | 7 | use std::str::FromStr; | ^^^^^^^^^^^^^^^^^
use serde_json::Value;

Check failure on line 8 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `serde_json::Value`

error: unused import: `serde_json::Value` --> src/api/edge_app/installation.rs:8:5 | 8 | use serde_json::Value; | ^^^^^^^^^^^^^^^^^
use log::debug;

Check failure on line 9 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `log::debug`

error: unused import: `log::debug` --> src/api/edge_app/installation.rs:9:5 | 9 | use log::debug; | ^^^^^^^^^^

use serde::Deserializer;

Check failure on line 11 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `serde::Deserializer`

error: unused import: `serde::Deserializer` --> src/api/edge_app/installation.rs:11:5 | 11 | use serde::Deserializer; | ^^^^^^^^^^^^^^^^^^^
use strum::IntoEnumIterator;

Check failure on line 12 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `strum::IntoEnumIterator`

error: unused import: `strum::IntoEnumIterator` --> src/api/edge_app/installation.rs:12:5 | 12 | use strum::IntoEnumIterator; | ^^^^^^^^^^^^^^^^^^^^^^^
use strum_macros::{Display, EnumIter, EnumString};

Check failure on line 13 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `Display`, `EnumIter`, and `EnumString`

error: unused imports: `Display`, `EnumIter`, and `EnumString` --> src/api/edge_app/installation.rs:13:20 | 13 | use strum_macros::{Display, EnumIter, EnumString}; | ^^^^^^^ ^^^^^^^^ ^^^^^^^^^^

use crate::commands::serde_utils::{deserialize_string_field, serialize_non_empty_string_field};

Check failure on line 15 in src/api/edge_app/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `deserialize_string_field` and `serialize_non_empty_string_field`

error: unused imports: `deserialize_string_field` and `serialize_non_empty_string_field` --> src/api/edge_app/installation.rs:15:36 | 15 | use crate::commands::serde_utils::{deserialize_string_field, serialize_non_empty_string_field}; | ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use serde::{Deserialize, Serialize};
use serde_json::json;

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

impl EdgeAppInstances {
pub fn new(value: serde_json::Value) -> Self {
Self { value }
}
}

impl Api {
pub fn get_instance_name(&self, installation_id: &str) -> Result<String, CommandError> {
let response = commands::get(
&self.authentication,
&format!(
"v4.1/edge-apps/installations?select=name&id=eq.{}",
installation_id
),
)?;

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
struct Instance {
name: String,
}

let instances = serde_json::from_value::<Vec<Instance>>(response)?;
if instances.is_empty() {
return Err(CommandError::MissingField);
}

Ok(instances[0].name.clone())
}

pub fn list_installations(&self, app_id: &str) -> Result<EdgeAppInstances, CommandError> {
let response = commands::get(
&self.authentication,
&format!(
"v4/edge-apps/installations?select=id,name&app_id=eq.{}",
app_id
),
)?;

let instances = EdgeAppInstances::new(response);

Ok(instances)
}

pub fn delete_installation(&self, installation_id: &str) -> Result<(), CommandError> {
commands::delete(
&self.authentication,
&format!("v4.1/edge-apps/installations?id=eq.{}", installation_id),
)?;
Ok(())
}

pub fn update_installation_name(&self, installation_id: &str, name: &str) -> Result<(), CommandError> {
let payload = json!({
"name": name,
});
commands::patch(
&self.authentication,
&format!("v4.1/edge-apps/installations?id=eq.{}", installation_id),
&payload,
)?;
Ok(())
}

pub fn create_installation(&self, app_id: &str, name: &str) -> Result<String, CommandError> {
let payload = json!({
"app_id": app_id,
"name": name,
});

let response = commands::post(
&self.authentication,
"v4.1/edge-apps/installations?select=id",
&payload,
)?;

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
struct Installation {
id: String,
}

let installation = serde_json::from_value::<Vec<Installation>>(response)?;
if installation.is_empty() {
return Err(CommandError::MissingField);
}

Ok(installation[0].id.clone())
}
}
5 changes: 5 additions & 0 deletions src/api/edge_app/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod app;
pub mod setting;
pub mod version;
pub mod channel;
pub mod installation;
Loading

0 comments on commit 03e3aeb

Please sign in to comment.