Event system

- App events, can be polled by apps using the HTTP API
- Node events, can be processed by a node implementation by registering a callback in the `Ubisync` object
- Some further additions, like adding pot members etc. to test these new event functions
This commit is contained in:
Philip (a-0) 2024-01-24 17:53:50 +01:00
parent 76f6a6b67b
commit d258060769
24 changed files with 479 additions and 96 deletions

View file

@ -2,11 +2,14 @@ use anyhow::anyhow;
use error::UbisyncError;
use reqwest::{Client, StatusCode};
use tracing::debug;
use ubisync_lib::api::{
app::{AppRegisterRequest, AppRegisterResponse, AppCreatePotRequest},
UbisyncRequest,
};
pub use ubisync_lib::*;
use ubisync_lib::{
api::{
app::{AppCreatePotRequest, AppRegisterRequest, AppRegisterResponse},
UbisyncRequest,
},
types::AppId,
};
pub mod error;
@ -15,15 +18,20 @@ pub struct UbisyncClient {
port: u16,
selected_api_version: String,
base_url: String,
jwt_token: String,
registration: AppRegistration,
reqwest_client: Client,
}
pub struct AppRegistration {
pub jwt_token: String,
pub app_id: AppId,
}
impl UbisyncClient {
pub async fn init(
host: &str,
port: u16,
jwt_token: Option<&str>,
registration: Option<AppRegistration>,
application_name: &str,
application_description: &str,
application_type: &str,
@ -43,8 +51,8 @@ impl UbisyncClient {
.get(0)
.expect("No available API version returned by ubisync node");
let token = match jwt_token {
Some(t) => t.to_string(),
let registration = match registration {
Some(t) => t,
None => {
let response = http_client
.put(Self::build_base_url(host, port, &selected_version) + "/app/register")
@ -59,11 +67,14 @@ impl UbisyncClient {
if response.status() != StatusCode::OK {
return Err(UbisyncError::AppRegistrationFailed);
}
response
let parsed = response
.json::<AppRegisterResponse>()
.await
.expect("Failed to extract JWT from app regstration request")
.token
.expect("Failed to extract JWT from app regstration request");
AppRegistration {
jwt_token: parsed.token,
app_id: parsed.app_id,
}
}
};
@ -72,15 +83,15 @@ impl UbisyncClient {
port: port,
selected_api_version: selected_version.to_string(),
base_url: Self::build_base_url(host, port, selected_version),
jwt_token: token.to_string(),
registration,
reqwest_client: http_client,
})
}
pub async fn create_default_pot(self) -> anyhow::Result<UbisyncClient> {
let response = self.send(AppCreatePotRequest {
app_type: None,
}, ()).await?;
let response = self
.send(AppCreatePotRequest { app_type: None }, ())
.await?;
debug!("Created new pot with ID {:?}", response.pot_id);
Ok(self)
}
@ -98,7 +109,7 @@ impl UbisyncClient {
request.method(),
&(self.base_url.to_owned() + &request.path(parameters)),
)
.bearer_auth(&self.jwt_token)
.bearer_auth(&self.registration.jwt_token)
.json(&request)
.send()
.await
@ -123,6 +134,10 @@ impl UbisyncClient {
self.base_url = Self::build_base_url(&self.host, self.port, &self.selected_api_version);
}
pub fn app_id(&self) -> AppId {
self.registration.app_id.clone()
}
fn build_base_url(host: &str, port: u16, api_version: &str) -> String {
format!("http://{}:{}/{}", host, port, api_version)
}