Skip to content

Commit 9684787

Browse files
committed
Add two new utility functions
Signed-off-by: David Weik <geekupyourlife@gmail.com>
1 parent 2432068 commit 9684787

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
<script src="./js/utility/submit-sas-code.js"></script>
107107
<script src="./js/utility/get-sas-job-state.js"></script>
108108
<script src="./js/utility/get-URL-search-parameters.js"></script>
109+
<script src="./js/utility/get-model-variables.js"></script>
110+
<script src="./js/utility/delete-model-variable.js"></script>
109111

110112
<!-- Import Object Builder -->
111113
<script src="./js/objects/add-text-objects.js"></script>

js/utility/delete-model-variable.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Delete a specifc model content file
3+
*
4+
* @param {String} VIYAHOST - The Host URL of the SAS Viya Host
5+
* @param {String} modelID - The ID of the model from which the content should be deleted
6+
* @param {String} variableID - The ID of the variable that should be deleted
7+
* @returns {Promise/Object of Client deletion Response} - Returns a Promise that should resolve into a status code
8+
*/
9+
async function deleteModelVariable(VIYAHOST, modelID, variableID) {
10+
let DELETEMODELVARIABLERESPONSE = await fetch(
11+
`${VIYAHOST}/modelRepository/models/${modelID}/variables/${variableID}`,
12+
{
13+
// mode: 'no-cors',
14+
method: 'delete',
15+
headers: {
16+
'Accept': '*/*',
17+
'X-CSRF-TOKEN':
18+
document?.csrfToken != undefined ? document.csrfToken : ''
19+
},
20+
credentials: 'include'
21+
}
22+
);
23+
if (!DELETEMODELVARIABLERESPONSE.ok) {
24+
if (
25+
DELETEMODELVARIABLERESPONSE.status === 403 &&
26+
DELETEMODELVARIABLERESPONSE.headers.get('x-forbidden-reason') === 'CSRF'
27+
) {
28+
let h = DELETEMODELVARIABLERESPONSE.headers.get('x-csrf-header');
29+
let t = DELETEMODELVARIABLERESPONSE.headers.get('x-csrf-token');
30+
document.csrfToken = t;
31+
DELETEMODELVARIABLERESPONSE = await fetch(
32+
`${VIYAHOST}/modelRepository/models/${modelID}/variables/${variableID}`,
33+
{
34+
// mode: 'no-cors',
35+
method: 'delete',
36+
headers: {
37+
'Accept': '*/*',
38+
'X-CSRF-TOKEN': t,
39+
},
40+
credentials: 'include',
41+
}
42+
);
43+
}
44+
}
45+
46+
return DELETEMODELVARIABLERESPONSE.status
47+
}

js/utility/get-model-variables.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Returns a list of Model Manager project models
3+
*
4+
* @param {String} VIYAHOST - The Host URL of the SAS Viya Host
5+
* @param {String} modelID - UUID of the SAS Model Manager model
6+
* @param {Integer} start - Optional - Specify from where the request should start - default is 0
7+
* @param {Integer} limit - Optional - Specify how many items should be requested at a time - default is 1000
8+
* @returns {Promise/Array of SAS Model Manager Model Variables} - Returns a Promise that should resolve into a list of SAS Model Manager Model Variables
9+
*/
10+
async function getModelVariables(
11+
VIYAHOST,
12+
modelID,
13+
start = 0,
14+
limit = 1000
15+
) {
16+
17+
// Get the variables of a model
18+
const modelVariablesResponse = await fetch(
19+
`${VIYAHOST}/modelRepository/models/${modelID}/variables?start=${start}&limit=${limit}`,
20+
{
21+
// mode: 'no-cors',
22+
method: 'get',
23+
headers: {
24+
'X-Requested-With': 'XMLHttpRequest',
25+
Accept: 'application/json',
26+
'Content-Type': 'application/json',
27+
},
28+
credentials: 'include',
29+
}
30+
);
31+
32+
// Parse the response
33+
const modelVariablesItems = await modelVariablesResponse.json();
34+
return modelVariablesItems?.items;
35+
}

0 commit comments

Comments
 (0)