Skip to content

Commit

Permalink
Merge pull request #5281 from KKoukiou/webui-api-dont-repeat-yourself
Browse files Browse the repository at this point in the history
webui: apis: dont repeat yourself
  • Loading branch information
KKoukiou committed Oct 30, 2023
2 parents 49b172e + ab15aa0 commit a70ac7c
Show file tree
Hide file tree
Showing 19 changed files with 610 additions and 590 deletions.
19 changes: 11 additions & 8 deletions ui/webui/src/actions/storage-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ import cockpit from "cockpit";

import {
gatherRequests,
getAllDiskSelection,
getPartitioningMethod,
} from "../apis/storage_partitioning.js";
import {
getDeviceData,
getDevices,
getDiskFreeSpace,
getDiskTotalSpace,
getFormatData,
getPartitioningMethod,
} from "../apis/storage_devicetree.js";
import {
getAllDiskSelection,
getUsableDisks,
} from "../apis/storage.js";
} from "../apis/storage_disks_selection.js";
import {
setCriticalErrorAction,
} from "../actions/miscellaneous-actions.js";
Expand All @@ -36,9 +40,8 @@ export const getDevicesAction = () => {
return async (dispatch) => {
try {
const devices = await getDevices();
const devicesData = await Promise.all(devices[0].map(async (device) => {
let devData = await getDeviceData({ disk: device });
devData = devData[0];
const devicesData = await Promise.all(devices.map(async (device) => {
const devData = await getDeviceData({ disk: device });

const free = await getDiskFreeSpace({ diskNames: [device] });
// extend it with variants to keep the format consistent
Expand Down Expand Up @@ -77,7 +80,7 @@ export const getDiskSelectionAction = () => {
diskSelection: {
ignoredDisks: diskSelection[0].IgnoredDisks.v,
selectedDisks: diskSelection[0].SelectedDisks.v,
usableDisks: usableDisks[0],
usableDisks,
}
},
});
Expand All @@ -98,7 +101,7 @@ export const getPartitioningDataAction = ({ requests, partitioning }) => {
if (props.method === "MANUAL") {
const reqs = await gatherRequests({ partitioning });

props.requests = convertRequests(reqs[0]);
props.requests = convertRequests(reqs);
}
} else {
props.requests = convertRequests(requests);
Expand Down
23 changes: 11 additions & 12 deletions ui/webui/src/apis/boss.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/

import cockpit from "cockpit";
import { _callClient } from "./helpers.js";

const OBJECT_PATH = "/org/fedoraproject/Anaconda/Boss";
const INTERFACE_NAME = "org.fedoraproject.Anaconda.Boss";

const callClient = (...args) => {
return _callClient(BossClient, OBJECT_PATH, INTERFACE_NAME, ...args);
};

/**
* @param {string} address Anaconda bus address
Expand All @@ -33,7 +41,7 @@ export class BossClient {
BossClient.instance = this;

this.client = cockpit.dbus(
"org.fedoraproject.Anaconda.Boss",
INTERFACE_NAME,
{ superuser: "try", bus: "none", address }
);
this.address = address;
Expand Down Expand Up @@ -63,21 +71,12 @@ export const getSteps = ({ task }) => {
* @returns {Promise} Resolves a list of tasks
*/
export const installWithTasks = () => {
return new BossClient().client.call(
"/org/fedoraproject/Anaconda/Boss",
"org.fedoraproject.Anaconda.Boss",
"InstallWithTasks", []
)
.then(ret => ret[0]);
return callClient("InstallWithTasks", []);
};

/**
* @param {string} locale Locale id
*/
export const setLocale = ({ locale }) => {
return new BossClient().client.call(
"/org/fedoraproject/Anaconda/Boss",
"org.fedoraproject.Anaconda.Boss",
"SetLocale", [locale]
);
return callClient("SetLocale", [locale]);
};
32 changes: 32 additions & 0 deletions ui/webui/src/apis/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2023 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with This program; If not, see <http://www.gnu.org/licenses/>.
*/

export const _callClient = (Client, OBJECT_PATH, INTERFACE_NAME, ...args) => {
return new Client().client.call(OBJECT_PATH, INTERFACE_NAME, ...args).then(res => res[0]);
};

export const _setProperty = (Client, OBJECT_PATH, INTERFACE_NAME, ...args) => {
return new Client().client.call(
OBJECT_PATH, "org.freedesktop.DBus.Properties", "Set", [INTERFACE_NAME, ...args]
);
};

export const _getProperty = (Client, OBJECT_PATH, INTERFACE_NAME, ...args) => {
return new Client().client.call(
OBJECT_PATH, "org.freedesktop.DBus.Properties", "Get", [INTERFACE_NAME, ...args]
).then(res => res[0].v);
};
86 changes: 23 additions & 63 deletions ui/webui/src/apis/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ import cockpit from "cockpit";

import { getLanguageAction, getLanguagesAction } from "../actions/localization-actions.js";
import { debug } from "../helpers/log.js";
import { _callClient, _setProperty, _getProperty } from "./helpers.js";

const OBJECT_PATH = "/org/fedoraproject/Anaconda/Modules/Localization";
const INTERFACE_NAME = "org.fedoraproject.Anaconda.Modules.Localization";

const callClient = (...args) => {
return _callClient(LocalizationClient, OBJECT_PATH, INTERFACE_NAME, ...args);
};
const setProperty = (...args) => {
return _setProperty(LocalizationClient, OBJECT_PATH, INTERFACE_NAME, ...args);
};
const getProperty = (...args) => {
return _getProperty(LocalizationClient, OBJECT_PATH, INTERFACE_NAME, ...args);
};

export class LocalizationClient {
constructor (address) {
Expand All @@ -31,7 +45,7 @@ export class LocalizationClient {
LocalizationClient.instance = this;

this.client = cockpit.dbus(
"org.fedoraproject.Anaconda.Modules.Localization",
INTERFACE_NAME,
{ superuser: "try", bus: "none", address }
);
this.address = address;
Expand All @@ -46,29 +60,14 @@ export class LocalizationClient {
* @returns {Promise} Resolves a list of language ids
*/
export const getLanguages = () => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.fedoraproject.Anaconda.Modules.Localization",
"GetLanguages", []
)
.then(res => res[0])
);
return callClient("GetLanguages", []);
};

/**
* @returns {Promise} The language the system will use
*/
export const getLanguage = () => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.freedesktop.DBus.Properties",
"Get",
["org.fedoraproject.Anaconda.Modules.Localization", "Language"]
)
.then(res => res[0].v)
);
return getProperty("Language");
};

/**
Expand All @@ -77,14 +76,7 @@ export const getLanguage = () => {
* @returns {Promise} Resolves a language data object
*/
export const getLanguageData = ({ lang }) => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.fedoraproject.Anaconda.Modules.Localization",
"GetLanguageData", [lang]
)
.then(res => res[0])
);
return callClient("GetLanguageData", [lang]);
};

/**
Expand All @@ -93,28 +85,14 @@ export const getLanguageData = ({ lang }) => {
* @returns {Promise} Resolves a list of locales ids
*/
export const getLocales = ({ lang }) => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.fedoraproject.Anaconda.Modules.Localization",
"GetLocales", [lang]
)
.then(res => res[0])
);
return callClient("GetLocales", [lang]);
};

/**
* @returns {Promise} Resolves a list of common locales id's.
*/
export const getCommonLocales = () => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.fedoraproject.Anaconda.Modules.Localization",
"GetCommonLocales"
)
.then(res => res[0])
);
return callClient("GetCommonLocales");
};

/**
Expand All @@ -123,32 +101,14 @@ export const getCommonLocales = () => {
* @returns {Promise} Resolves a locale data object
*/
export const getLocaleData = ({ locale }) => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.fedoraproject.Anaconda.Modules.Localization",
"GetLocaleData", [locale]
)
.then(res => res[0])
);
return callClient("GetLocaleData", [locale]);
};

/**
* @param {string} lang Language id
*/
export const setLanguage = ({ lang }) => {
return (
new LocalizationClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Localization",
"org.freedesktop.DBus.Properties",
"Set",
[
"org.fedoraproject.Anaconda.Modules.Localization",
"Language",
cockpit.variant("s", lang)
]
)
);
return setProperty("Language", cockpit.variant("s", lang));
};

export const startEventMonitorLocalization = ({ dispatch }) => {
Expand All @@ -157,7 +117,7 @@ export const startEventMonitorLocalization = ({ dispatch }) => {
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === "org.fedoraproject.Anaconda.Modules.Localization" && Object.hasOwn(args[1], "Language")) {
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "Language")) {
dispatch(getLanguageAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
Expand Down
22 changes: 11 additions & 11 deletions ui/webui/src/apis/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import cockpit from "cockpit";

import { getConnectedAction } from "../actions/network-actions.js";
import { debug } from "../helpers/log.js";
import { _getProperty } from "./helpers.js";

const OBJECT_PATH = "/org/fedoraproject/Anaconda/Modules/Network";
const INTERFACE_NAME = "org.fedoraproject.Anaconda.Modules.Network";

const getProperty = (...args) => {
return _getProperty(NetworkClient, OBJECT_PATH, INTERFACE_NAME, ...args);
};

export class NetworkClient {
constructor (address) {
Expand All @@ -31,7 +39,7 @@ export class NetworkClient {
NetworkClient.instance = this;

this.client = cockpit.dbus(
"org.fedoraproject.Anaconda.Modules.Network",
INTERFACE_NAME,
{ superuser: "try", bus: "none", address }
);
this.address = address;
Expand All @@ -46,15 +54,7 @@ export class NetworkClient {
* @returns {Promise} The bool state of the network connection
*/
export const getConnected = () => {
return (
new NetworkClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Network",
"org.freedesktop.DBus.Properties",
"Get",
["org.fedoraproject.Anaconda.Modules.Network", "Connected"]
)
.then(res => res[0].v)
);
return getProperty("Connected");
};

export const startEventMonitorNetwork = ({ dispatch }) => {
Expand All @@ -63,7 +63,7 @@ export const startEventMonitorNetwork = ({ dispatch }) => {
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === "org.fedoraproject.Anaconda.Modules.Network" && Object.hasOwn(args[1], "Connected")) {
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "Connected")) {
dispatch(getConnectedAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
Expand Down
17 changes: 10 additions & 7 deletions ui/webui/src/apis/payloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
* along with This program; If not, see <http://www.gnu.org/licenses/>.
*/
import cockpit from "cockpit";
import { _callClient } from "./helpers.js";

const OBJECT_PATH = "/org/fedoraproject/Anaconda/Modules/Payloads";
const INTERFACE_NAME = "org.fedoraproject.Anaconda.Modules.Payloads";

const callClient = (...args) => {
return _callClient(PayloadsClient, OBJECT_PATH, INTERFACE_NAME, ...args);
};

export class PayloadsClient {
constructor (address) {
Expand All @@ -27,7 +35,7 @@ export class PayloadsClient {
PayloadsClient.instance = this;

this.client = cockpit.dbus(
"org.fedoraproject.Anaconda.Modules.Payloads",
INTERFACE_NAME,
{ superuser: "try", bus: "none", address }
);
this.address = address;
Expand All @@ -45,10 +53,5 @@ export class PayloadsClient {
* @returns {Promise} Resolves the total space required by the payload
*/
export const getRequiredSpace = () => {
return new PayloadsClient().client.call(
"/org/fedoraproject/Anaconda/Modules/Payloads",
"org.fedoraproject.Anaconda.Modules.Payloads",
"CalculateRequiredSpace", []
)
.then(res => res[0]);
return callClient("CalculateRequiredSpace", []);
};
Loading

0 comments on commit a70ac7c

Please sign in to comment.