Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor spoolman integration to support v2 response #1749

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
meteyou marked this conversation as resolved.
Show resolved Hide resolved
},
{ 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