Skip to content

Commit

Permalink
changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Apr 18, 2024
1 parent c08a679 commit fcb2222
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
6 changes: 3 additions & 3 deletions rust/agama-server/src/software/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use crate::{
error::Error,
web::{
common::{issues_router, progress_router, service_status_router, Streams},
common::{issues_router, progress_router, service_status_router, EventStreams},
Event,
},
};
Expand Down Expand Up @@ -51,8 +51,8 @@ pub struct SoftwareConfig {
/// It emits the Event::ProductChanged and Event::PatternsChanged events.
///
/// * `connection`: D-Bus connection to listen for events.
pub async fn software_streams(dbus: zbus::Connection) -> Result<Streams, Error> {
let result: Streams = vec![
pub async fn software_streams(dbus: zbus::Connection) -> Result<EventStreams, Error> {
let result: EventStreams = vec![
(
"patterns_changed",
Box::pin(patterns_changed_stream(dbus.clone()).await?),
Expand Down
7 changes: 3 additions & 4 deletions rust/agama-server/src/users/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::{
error::Error,
web::{
common::{service_status_router, validation_router, Streams},
common::{service_status_router, validation_router, EventStreams},
Event,
},
};
Expand All @@ -17,7 +17,6 @@ use agama_lib::{
};
use axum::{extract::State, routing::get, Json, Router};
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use tokio_stream::{Stream, StreamExt};

#[derive(Clone)]
Expand All @@ -30,14 +29,14 @@ struct UsersState<'a> {
/// It emits the Event::RootPasswordChange, Event::RootSSHKeyChanged and Event::FirstUserChanged events.
///
/// * `connection`: D-Bus connection to listen for events.
pub async fn users_streams(dbus: zbus::Connection) -> Result<Streams, Error> {
pub async fn users_streams(dbus: zbus::Connection) -> Result<EventStreams, Error> {
const FIRST_USER_ID: &str = "first_user";
const ROOT_PASSWORD_ID: &str = "root_password";
const ROOT_SSHKEY_ID: &str = "root_sshkey";
// here we have three streams, but only two events. Reason is
// that we have three streams from dbus about property change
// and unify two root user properties into single event to http API
let result: Streams = vec![
let result: EventStreams = vec![
(
FIRST_USER_ID,
Box::pin(first_user_changed_stream(dbus.clone()).await?),
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-server/src/web/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::error::Error;

use super::Event;

pub type Streams = Vec<(&'static str, Pin<Box<dyn Stream<Item = Event> + Send>>)>;
pub type EventStreams = Vec<(&'static str, Pin<Box<dyn Stream<Item = Event> + Send>>)>;

/// Builds a router to the `org.opensuse.Agama1.ServiceStatus` interface of the
/// given D-Bus object.
Expand Down
37 changes: 29 additions & 8 deletions web/src/client/software.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const SelectedBy = Object.freeze({
* @property {string|null} email - Registration email, if any.
*/

/**
* @typedef {object} RegistrationFailure
* @property {Number} id - ID of error.
* @property {string} message - Failure message.
*/

/**
* @typedef {object} ActionResult
* @property {boolean} success - Whether the action was successfully done.
Expand Down Expand Up @@ -255,14 +261,12 @@ class ProductBaseClient {
async getRegistration() {
const response = await this.client.get("/software/registration");
if (!response.ok) {
console.log("Failed to get registration config: ", response);
console.log("Failed to get registration config:", response);
return { requirement: "unknown", code: null, email: null };
}
const config = await response.json();

const requirement = config.requirement;
const code = config.key;
const email = config.email;
const { requirement, key: code, email } = config;

const registration = { requirement, code, email };
if (code.length === 0) registration.code = null;
Expand All @@ -280,10 +284,18 @@ class ProductBaseClient {
*/
async register(code, email = "") {
const response = await this.client.post("/software/registration", { key: code, email });
if (response.status === 422) {
/** @type RegistrationFailure */
const body = await response.json();
return {
success: false,
message: body.message,
};
}

return {
success: response.ok,
message: "", // TODO: pass message and code in body
success: response.ok, // still we can fail 400 due to dbus issue or 500 if backend stop working. maybe some message for this case?
message: ""
};
}

Expand All @@ -295,9 +307,18 @@ class ProductBaseClient {
async deregister() {
const response = await this.client.delete("/software/registration");

if (response.status === 422) {
/** @type RegistrationFailure */
const body = await response.json();
return {
success: false,
message: body.message,
};
}

return {
success: response.ok,
message: "", // TODO: how to get message?
success: response.ok, // still we can fail 400 due to dbus issue or 500 if backend stop working. maybe some message for this case?
message: ""
};
}

Expand Down

0 comments on commit fcb2222

Please sign in to comment.