carnet api

This commit is contained in:
alma 2025-04-20 14:26:20 +02:00
parent a4fcac516e
commit 576b9e7fc9

View File

@ -8,6 +8,31 @@ async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function parseXMLResponse(response: Response): Promise<any> {
const text = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, 'text/xml');
// Convert XML to a simple object
const result: any = {};
const root = xmlDoc.documentElement;
if (root && root.nodeName === 'ocs') {
const data = root.getElementsByTagName('data')[0];
if (data) {
const children = data.childNodes;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.nodeType === 1) { // Element node
result[child.nodeName] = child.textContent;
}
}
}
}
return result;
}
async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string) {
// First, try to get the user's WebDAV password
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
@ -22,8 +47,8 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
return null;
}
const userInfo = await userInfoResponse.json();
const webdavPassword = userInfo.ocs.data?.webdav_password;
const userInfo = await parseXMLResponse(userInfoResponse);
const webdavPassword = userInfo.webdav_password;
if (!webdavPassword) {
// If no WebDAV password exists, create one
@ -58,8 +83,8 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
return null;
}
const newUserInfo = await newUserInfoResponse.json();
return newUserInfo.ocs.data?.webdav_password;
const newUserInfo = await parseXMLResponse(newUserInfoResponse);
return newUserInfo.webdav_password;
}
return webdavPassword;