Skip to content

Commit

Permalink
Merge pull request #217 from Screenly/feature/api_separation
Browse files Browse the repository at this point in the history
Feature/api separation
  • Loading branch information
korvyashka authored Sep 2, 2024
2 parents 7591908 + 4d33fc2 commit f56d01b
Show file tree
Hide file tree
Showing 21 changed files with 1,087 additions and 778 deletions.
50 changes: 50 additions & 0 deletions src/api/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::api::Api;
use crate::commands;
use crate::commands::CommandError;

use serde::{Deserialize, Serialize};

#[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(
&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
),
)?)?)
}

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,
)?)
}
}
86 changes: 86 additions & 0 deletions src/api/edge_app/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use log::debug;

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

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

#[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)
}
}
44 changes: 44 additions & 0 deletions src/api/edge_app/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::api::Api;
use crate::commands;
use crate::commands::CommandError;

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(())
}
}
104 changes: 104 additions & 0 deletions src/api/edge_app/installation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use crate::api::Api;
use crate::commands;
use crate::commands::CommandError;

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 channel;
pub mod installation;
pub mod setting;
pub mod version;
Loading

0 comments on commit f56d01b

Please sign in to comment.