Copy File Definition: Process multiple objects with single api request (#3163)

Refs https://github.com/openhab/openhab-core/pull/4734.

This solves the problem with YAML format returning the `version` and
`things` (or `items`) keys for each item in multi-select requests. By
combining them into a single request, only one key will be returned at
the top.

---------

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/3168/head
jimtng 2025-04-24 07:01:16 +10:00 committed by GitHub
parent 19c3a42d60
commit c9c4ab6f76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 45 deletions

View File

@ -30,40 +30,29 @@ export default {
get (uri, data) { get (uri, data) {
return wrapPromise(Framework7.request.promise.json(uri, data)) return wrapPromise(Framework7.request.promise.json(uri, data))
}, },
getPlain (uri_or_parameters, data, contentType, responseType) { getPlain (uri, data, contentType, responseType, headers) {
let parameters = {} return wrapPromise(Framework7.request.promise({
if (typeof uri_or_parameters === 'string') { method: 'GET',
parameters = { url: uri,
url: uri_or_parameters, data,
method: 'GET', processData: false,
data, contentType: contentType || 'text/plain',
processData: false, xhrFields: typeof responseType !== 'undefined' ? { responseType } : null,
contentType: contentType || 'text/plain', headers
xhrFields: typeof responseType !== 'undefined' ? { responseType } : null }))
}
} else if (typeof uri_or_parameters === 'object') {
parameters = {
contentType: 'text/plain',
processData: false,
method: 'GET',
...uri_or_parameters
}
} else {
throw new Error('Invalid parameters')
}
return wrapPromise(Framework7.request.promise(parameters))
}, },
post (uri, data, dataType) { post (uri, data, dataType) {
return wrapPromise(Framework7.request.promise.postJSON(uri, data, dataType)) return wrapPromise(Framework7.request.promise.postJSON(uri, data, dataType))
}, },
postPlain (uri, data, dataType, contentType) { postPlain (uri, data, dataType, contentType, headers) {
return wrapPromise(Framework7.request.promise({ return wrapPromise(Framework7.request.promise({
method: 'POST', method: 'POST',
url: uri, url: uri,
data, data,
processData: false, processData: false,
contentType: contentType || 'text/plain', contentType: contentType || 'text/plain',
dataType: dataType || 'application/json' dataType: dataType || 'application/json',
headers
})) }))
}, },
put (uri, data) { put (uri, data) {

View File

@ -7,22 +7,10 @@ function executeFileDefinitionCopy (vueInstance, objectType, objectTypeLabel, ob
const progressDialog = vueInstance.$f7.dialog.progress(`Loading ${objectTypeLabel} ${fileFormatLabel} definition...`) const progressDialog = vueInstance.$f7.dialog.progress(`Loading ${objectTypeLabel} ${fileFormatLabel} definition...`)
const path = `/rest/file-format/${objectType}s` const path = `/rest/file-format/${objectType}s`
let apiCalls = [] const headers = { accept: mediaType }
if (objectIds !== null) { const data = JSON.stringify(objectIds)
apiCalls = objectIds.map((id) => vueInstance.$oh.api.getPlain({ vueInstance.$oh.api.postPlain(path, data, 'text', 'application/json', headers)
url: path + '/' + id, .then(definition => {
headers: { accept: mediaType }
}))
} else {
apiCalls = [vueInstance.$oh.api.getPlain({
url: path,
headers: { accept: mediaType }
})]
}
Promise.all(apiCalls)
.then(definitions => {
const definition = definitions.join('\n')
progressDialog.close() progressDialog.close()
if (vueInstance.$clipboard(definition)) { if (vueInstance.$clipboard(definition)) {
vueInstance.$f7.toast.create({ vueInstance.$f7.toast.create({
@ -36,7 +24,7 @@ function executeFileDefinitionCopy (vueInstance, objectType, objectTypeLabel, ob
}) })
.catch(error => { .catch(error => {
progressDialog.close() progressDialog.close()
vueInstance.$f7.dialog.alert(`Error copying ${objectTypeLabel} ${fileFormatLabel} definition: ${error}`, 'Error') vueInstance.$f7.dialog.alert(`Error loading ${objectTypeLabel} ${fileFormatLabel} definition: ${error}`, 'Error')
}) })
} }

View File

@ -302,10 +302,7 @@ export default {
} }
}, },
copySelected () { copySelected () {
// When _all_ (not just filtered) items are selected, pass null to copyFileDefinitionToClipboard this.copyFileDefinitionToClipboard(this.ObjectType.ITEM, this.selectedItems)
// so that it only makes one call to the backend
const selectedItems = this.selectedItems.length === this.items.length ? null : this.selectedItems
this.copyFileDefinitionToClipboard(this.ObjectType.ITEM, selectedItems)
}, },
removeSelected () { removeSelected () {
const vm = this const vm = this