-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds: api functions for all edge apps commands.
- Loading branch information
1 parent
219efc2
commit 03e3aeb
Showing
18 changed files
with
927 additions
and
689 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / clippyunused import: `std::collections::HashMap`
|
||
use std::ops::Not; | ||
use std::str::FromStr; | ||
use serde_json::Value; | ||
use log::debug; | ||
|
||
use serde::Deserializer; | ||
use strum::IntoEnumIterator; | ||
Check failure on line 12 in src/api/edge_app/installation.rs GitHub Actions / clippyunused import: `strum::IntoEnumIterator`
|
||
use strum_macros::{Display, EnumIter, EnumString}; | ||
Check failure on line 13 in src/api/edge_app/installation.rs GitHub Actions / clippyunused imports: `Display`, `EnumIter`, and `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 GitHub Actions / clippyunused imports: `deserialize_string_field` and `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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.