carnet api
This commit is contained in:
parent
a4fcac516e
commit
576b9e7fc9
@ -8,6 +8,31 @@ async function sleep(ms: number) {
|
|||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
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) {
|
async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string) {
|
||||||
// First, try to get the user's WebDAV password
|
// First, try to get the user's WebDAV password
|
||||||
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const userInfo = await userInfoResponse.json();
|
const userInfo = await parseXMLResponse(userInfoResponse);
|
||||||
const webdavPassword = userInfo.ocs.data?.webdav_password;
|
const webdavPassword = userInfo.webdav_password;
|
||||||
|
|
||||||
if (!webdavPassword) {
|
if (!webdavPassword) {
|
||||||
// If no WebDAV password exists, create one
|
// If no WebDAV password exists, create one
|
||||||
@ -58,8 +83,8 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newUserInfo = await newUserInfoResponse.json();
|
const newUserInfo = await parseXMLResponse(newUserInfoResponse);
|
||||||
return newUserInfo.ocs.data?.webdav_password;
|
return newUserInfo.webdav_password;
|
||||||
}
|
}
|
||||||
|
|
||||||
return webdavPassword;
|
return webdavPassword;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user