Skip to content

Commit 695742b

Browse files
authored
Add axios post util (#2153)
## Description This replaces boilerplate that checks whether we've successfully sent the request, whether there was an error, etc. closes #2151 ## Meta Merge checklist: - [x] Pull Request title is [short, imperative summary](https://cbea.ms/git-commit/) of proposed changes - [x] The description documents the _what_ and _why_ - [ ] If this PR changes behavior or adds a feature, user documentation is updated - [ ] If this PR touches photon-serde, all messages have been regenerated and hashes have not changed unexpectedly - [ ] If this PR touches configuration, this is backwards compatible with settings back to v2025.3.2 - [ ] If this PR touches pipeline settings or anything related to data exchange, the frontend typing is updated - [ ] If this PR addresses a bug, a regression test for it is added
1 parent 5df9137 commit 695742b

File tree

5 files changed

+116
-435
lines changed

5 files changed

+116
-435
lines changed

photon-client/src/components/cameras/CameraSettingsCard.vue

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { useCameraSettingsStore } from "@/stores/settings/CameraSettingsStore";
66
import { useStateStore } from "@/stores/StateStore";
77
import { computed, ref, watchEffect } from "vue";
88
import { type CameraSettingsChangeRequest, ValidQuirks } from "@/types/SettingTypes";
9-
import axios from "axios";
109
import { useTheme } from "vuetify";
10+
import { axiosPost } from "@/lib/PhotonUtils";
1111
1212
const theme = useTheme();
1313
@@ -120,36 +120,10 @@ const deleteThisCamera = () => {
120120
121121
const payload = { cameraUniqueName: useStateStore().currentCameraUniqueName };
122122
123-
axios
124-
.post("/utils/nukeOneCamera", payload)
125-
.then(() => {
126-
useStateStore().showSnackbarMessage({
127-
message: "Successfully dispatched the delete command. Waiting for backend to start back up",
128-
color: "success"
129-
});
130-
})
131-
.catch((error) => {
132-
if (error.response) {
133-
useStateStore().showSnackbarMessage({
134-
message: "The backend is unable to fulfil the request to delete this camera.",
135-
color: "error"
136-
});
137-
} else if (error.request) {
138-
useStateStore().showSnackbarMessage({
139-
message: "Error while trying to process the request! The backend didn't respond.",
140-
color: "error"
141-
});
142-
} else {
143-
useStateStore().showSnackbarMessage({
144-
message: "An error occurred while trying to process the request.",
145-
color: "error"
146-
});
147-
}
148-
})
149-
.finally(() => {
150-
deletingCamera.value = false;
151-
showDeleteCamera.value = false;
152-
});
123+
axiosPost("/utils/nukeOneCamera", "delete this camera", payload).finally(() => {
124+
deletingCamera.value = false;
125+
showDeleteCamera.value = false;
126+
});
153127
};
154128
const wrappedCameras = computed<SelectItem[]>(() =>
155129
Object.keys(useCameraSettingsStore().cameras).map((cameraUniqueName) => ({

photon-client/src/components/settings/DeviceControlCard.vue

Lines changed: 21 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,16 @@ import { inject, ref } from "vue";
33
import { useStateStore } from "@/stores/StateStore";
44
import PvSelect from "@/components/common/pv-select.vue";
55
import PvInput from "@/components/common/pv-input.vue";
6-
import axios from "axios";
76
import { useTheme } from "vuetify";
7+
import { axiosPost } from "@/lib/PhotonUtils";
88
99
const theme = useTheme();
1010
1111
const restartProgram = () => {
12-
axios
13-
.post("/utils/restartProgram")
14-
.then(() => {
15-
useStateStore().showSnackbarMessage({ message: "Successfully sent program restart request", color: "success" });
16-
})
17-
.catch((error) => {
18-
// This endpoint always return 204 regardless of outcome
19-
if (error.request) {
20-
useStateStore().showSnackbarMessage({
21-
message: "Error while trying to process the request! The backend didn't respond.",
22-
color: "error"
23-
});
24-
} else {
25-
useStateStore().showSnackbarMessage({
26-
message: "An error occurred while trying to process the request.",
27-
color: "error"
28-
});
29-
}
30-
});
12+
axiosPost("/utils/restartProgram", "restart PhotonVision");
3113
};
3214
const restartDevice = () => {
33-
axios
34-
.post("/utils/restartDevice")
35-
.then(() => {
36-
useStateStore().showSnackbarMessage({
37-
message: "Successfully dispatched the restart command. It isn't confirmed if a device restart will occur.",
38-
color: "success"
39-
});
40-
})
41-
.catch((error) => {
42-
if (error.response) {
43-
useStateStore().showSnackbarMessage({
44-
message: "The backend is unable to fulfil the request to restart the device.",
45-
color: "error"
46-
});
47-
} else if (error.request) {
48-
useStateStore().showSnackbarMessage({
49-
message: "Error while trying to process the request! The backend didn't respond.",
50-
color: "error"
51-
});
52-
} else {
53-
useStateStore().showSnackbarMessage({
54-
message: "An error occurred while trying to process the request.",
55-
color: "error"
56-
});
57-
}
58-
});
15+
axiosPost("/utils/restartDevice", "restart the device");
5916
};
6017
6118
const address = inject<string>("backendHost");
@@ -77,47 +34,25 @@ const handleOfflineUpdate = () => {
7734
timeout: -1
7835
});
7936
80-
axios
81-
.post("/utils/offlineUpdate", formData, {
82-
headers: { "Content-Type": "multipart/form-data" },
83-
onUploadProgress: ({ progress }) => {
84-
const uploadPercentage = (progress || 0) * 100.0;
85-
if (uploadPercentage < 99.5) {
86-
useStateStore().showSnackbarMessage({
87-
message: "New Software Upload in Process, " + uploadPercentage.toFixed(2) + "% complete",
88-
color: "secondary",
89-
timeout: -1
90-
});
91-
} else {
92-
useStateStore().showSnackbarMessage({
93-
message: "Installing uploaded software...",
94-
color: "secondary",
95-
timeout: -1
96-
});
97-
}
98-
}
99-
})
100-
.then((response) => {
101-
useStateStore().showSnackbarMessage({ message: response.data.text || response.data, color: "success" });
102-
})
103-
.catch((error) => {
104-
if (error.response) {
105-
useStateStore().showSnackbarMessage({
106-
color: "error",
107-
message: error.response.data.text || error.response.data
108-
});
109-
} else if (error.request) {
37+
axiosPost("/utils/offlineUpdate", "upload new software", formData, {
38+
headers: { "Content-Type": "multipart/form-data" },
39+
onUploadProgress: ({ progress }) => {
40+
const uploadPercentage = (progress || 0) * 100.0;
41+
if (uploadPercentage < 99.5) {
11042
useStateStore().showSnackbarMessage({
111-
color: "error",
112-
message: "Error while trying to process the request! The backend didn't respond."
43+
message: "New Software Upload in Process, " + uploadPercentage.toFixed(2) + "% complete",
44+
color: "secondary",
45+
timeout: -1
11346
});
11447
} else {
11548
useStateStore().showSnackbarMessage({
116-
color: "error",
117-
message: "An error occurred while trying to process the request."
49+
message: "Installing uploaded software...",
50+
color: "secondary",
51+
timeout: -1
11852
});
11953
}
120-
});
54+
}
55+
});
12156
};
12257
12358
const exportLogFile = ref();
@@ -166,29 +101,9 @@ const handleSettingsImport = () => {
166101
break;
167102
}
168103
169-
axios
170-
.post(`/settings${settingsEndpoint}`, formData, { headers: { "Content-Type": "multipart/form-data" } })
171-
.then((response) => {
172-
useStateStore().showSnackbarMessage({ message: response.data.text || response.data, color: "success" });
173-
})
174-
.catch((error) => {
175-
if (error.response) {
176-
useStateStore().showSnackbarMessage({
177-
color: "error",
178-
message: error.response.data.text || error.response.data
179-
});
180-
} else if (error.request) {
181-
useStateStore().showSnackbarMessage({
182-
color: "error",
183-
message: "Error while trying to process the request! The backend didn't respond."
184-
});
185-
} else {
186-
useStateStore().showSnackbarMessage({
187-
color: "error",
188-
message: "An error occurred while trying to process the request."
189-
});
190-
}
191-
});
104+
axiosPost(`/settings${settingsEndpoint}`, "import settings", formData, {
105+
headers: { "Content-Type": "multipart/form-data" }
106+
});
192107
193108
showImportDialog.value = false;
194109
importType.value = undefined;
@@ -199,32 +114,8 @@ const showFactoryReset = ref(false);
199114
const expected = "Delete Everything";
200115
const yesDeleteMySettingsText = ref("");
201116
const nukePhotonConfigDirectory = () => {
202-
axios
203-
.post("/utils/nukeConfigDirectory")
204-
.then(() => {
205-
useStateStore().showSnackbarMessage({
206-
message: "Successfully dispatched the reset command. Waiting for backend to start back up",
207-
color: "success"
208-
});
209-
})
210-
.catch((error) => {
211-
if (error.response) {
212-
useStateStore().showSnackbarMessage({
213-
message: "The backend is unable to fulfill the request to reset the device.",
214-
color: "error"
215-
});
216-
} else if (error.request) {
217-
useStateStore().showSnackbarMessage({
218-
message: "Error while trying to process the request! The backend didn't respond.",
219-
color: "error"
220-
});
221-
} else {
222-
useStateStore().showSnackbarMessage({
223-
message: "An error occurred while trying to process the request.",
224-
color: "error"
225-
});
226-
}
227-
});
117+
axiosPost("/utils/nukeConfigDirectory", "delete the config directory");
118+
228119
showFactoryReset.value = false;
229120
};
230121
</script>

0 commit comments

Comments
 (0)