Skip to content

Commit

Permalink
refactor: refactor spoolman integration to support v2 response
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Dej <[email protected]>
  • Loading branch information
meteyou committed Jan 19, 2024
1 parent 4ae21da commit 4945ad5
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/store/server/spoolman/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import { ActionTree } from 'vuex'
import { RootState } from '@/store/types'
import { ServerSpoolmanState } from '@/store/server/spoolman/types'

function convertV2response(payload: { error?: { message: string } | null; response: any }) {
if ((payload.error?.message ?? null) !== null) {
Vue.$toast.error(payload.error?.message ?? 'unknown spoolman error')
return null
}

// if the response is v2, we need to get the response into the payload
if ('response' in payload) return payload.response

return payload
}

export const actions: ActionTree<ServerSpoolmanState, RootState> = {
reset({ commit }) {
commit('reset')
Expand All @@ -15,6 +27,7 @@ export const actions: ActionTree<ServerSpoolmanState, RootState> = {
{
request_method: 'GET',
path: '/v1/info',
use_v2_response: true,
},
{ action: 'server/spoolman/getInfo' }
)
Expand All @@ -23,6 +36,7 @@ export const actions: ActionTree<ServerSpoolmanState, RootState> = {
{
request_method: 'GET',
path: '/v1/health',
use_v2_response: true,
},
{ action: 'server/spoolman/getHealth' }
)
Expand All @@ -31,6 +45,7 @@ export const actions: ActionTree<ServerSpoolmanState, RootState> = {
{
request_method: 'GET',
path: '/v1/vendor',
use_v2_response: true,
},
{ action: 'server/spoolman/getVendors' }
)
Expand All @@ -57,6 +72,7 @@ export const actions: ActionTree<ServerSpoolmanState, RootState> = {
'server.spoolman.proxy',
{
request_method: 'GET',
use_v2_response: true,
path: `/v1/spool/${payload.spool_id}`,
},
{ action: 'server/spoolman/getActiveSpool' }
Expand All @@ -65,29 +81,41 @@ export const actions: ActionTree<ServerSpoolmanState, RootState> = {

getActiveSpool({ commit }, payload) {
if ('requestParams' in payload) delete payload.requestParams
payload = convertV2response(payload)
if (payload === null) return

commit('setActiveSpool', payload)
},

getHealth({ commit, dispatch }, payload) {
delete payload.requestParams
commit('setHealth', payload.status)
dispatch('socket/removeInitModule', 'server/spoolman/getHealth', { root: true })

payload = convertV2response(payload)
if (payload === null) return

commit('setHealth', payload.status)
},

getInfo({ commit, dispatch }, payload) {
delete payload.requestParams
commit('setInfo', payload)
dispatch('socket/removeInitModule', 'server/spoolman/getInfo', { root: true })
payload = convertV2response(payload)
if (payload === null) return

commit('setInfo', payload)
},

getVendors({ commit, dispatch }, payload) {
delete payload.requestParams
dispatch('socket/removeInitModule', 'server/spoolman/getVendors', { root: true })
payload = convertV2response(payload)
if (payload === null) return

commit(
'setVendors',
Object.entries(payload).map((value) => value)
)
dispatch('socket/removeInitModule', 'server/spoolman/getVendors', { root: true })
},

refreshSpools({ dispatch }) {
Expand All @@ -105,10 +133,12 @@ export const actions: ActionTree<ServerSpoolmanState, RootState> = {

getSpools({ commit, dispatch }, payload) {
if ('requestParams' in payload) delete payload.requestParams
dispatch('socket/removeLoading', 'refreshSpools', { root: true })
payload = convertV2response(payload)
if (payload === null) return

const spools = Object.entries(payload).map((value) => value[1])
commit('setSpools', spools)

dispatch('socket/removeLoading', 'refreshSpools', { root: true })
},

setActiveSpool(_, id: number | null) {
Expand Down

0 comments on commit 4945ad5

Please sign in to comment.