CoreEndpoints

CoreEndpoints

A standard Girder instance provide the following set of endpoints.
By adding the one listed here as extension inside a GirderClientBuilder,
you will get the following set of methods:

// Usage example
import CoreEndpoints from 'paraviewweb/src/IO/Girder/CoreEndpoints';
import HpcCloudEndpoints from 'paraviewweb/src/IO/Girder/HpcCloudEndpoints';
import { build } from 'paraviewweb/src/IO/Girder/GirderClientBuilder';

const client = build(location, ...CoreEndpoints, ...HpcCloudEndpoints);

assetstore

listAssetStores(query={})

createAssetStore(assetstore)

updateAssetStore(assetstore)

deleteAssetStore(id)

collection

listCollections(query={})

createCollection(collection)

deleteCollection(id)

getCollection(id)

editCollection(collection={})

getCollectionAccess(id)

editCollectionAccess(collection) {

file

uploadFileToItem(params, file)

getUploadOffset(id)

downloadFile(id)

updateFileContent(id, size)

deleteFile(id)

editFile(file)

newFile(file)

folder

listFolders(query={})

createFolder(folder)

editFolderMetaData(id, metadata)

deleteFolder(id)

getFolder(id)

editFolder(folder)

downloadFolder(id)

getFolderAccess(id)

editFolderAccess(folder)

group

updateGroupModerator(groupId, userId, onOff)

updateGroupAdmin(groupdId, userId, onOff)

createGroup(group)

deleteGroup(id)

getGroup(id)

editGroup(group={})

listGroupInvitations(id, query={})

addGroupInvitation(id, options={})

listGroupMembers(id, query={})

removeUserFromGroup(id, userId)

joinGroup(id)

getGroupAccess(id)

item

downloadItem(id)

updateItemMetadata(id, metadata={})

listItems(query={})

createItem(folderId, name, description=’’)

listFiles(id, query)

getItemRootPath(id)

getItem(id)

deleteItem(id)

editItem(item)

copyItem(id, destinationItem)

resource

downloadResources(resourceList, withMetadata=false)

searchResources(query,types)

deleteResources(resourceList)

system

deleteSetting(key)

getSettings(settings)

setSettings(keyValueMap)

getServerVersion()

listUnfinishedUpload(query={})

removeUnfinishedUpload(query={})

listPlugins()

setActivePlugins(plugins)

user

listUsers(query)

createUser(user)

changePassword(old, newPassword)

resetPassword(email)

deleteUser(id)

getUser(id)

updateUser(user)

Source

assetstore.js
/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
listAssetStores(query = {}) {
const expected = ['limit', 'offset', 'sort', 'sortdir'];
const params = filterQuery(query, ...expected);

return client._.get('/assetstore', {
params,
});
},

createAssetStore(assetstore) {
const required = ['name', 'type'];
const possible = [
'root',
'db',
'bucket',
'prefix',
'accessKeyId',
'secretKey',
'service',
];
const params = filterQuery(assetstore, ...[].concat(required, possible));
const { missingKeys, promise } = mustContain(assetstore, ...required);

return missingKeys
? promise
: busy(client._.post(`/assetstore${encodeQueryAsString(params)}`));
},

updateAssetStore(assetstore) {
const expected = ['name', 'root', 'db', 'current', '_id'];
const params = filterQuery(
assetstore,
expected.slice(0, expected.length - 1)
); // Remove 'id'

return client._.put(`/assetstore/${assetstore._id}`, {
params,
});
},

deleteAssetStore(id) {
return client._.delete(`/assetstore/${id}`);
},
};
}
collection.js
/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
listCollections(query = {}) {
const expected = ['text', 'limit', 'offset', 'sort', 'sortdir'];
const params = filterQuery(query, ...expected);

return client._.get('/collection', { params });
},

createCollection(collection) {
const expected = ['name', 'description', 'public'];
const params = filterQuery(collection, ...expected);
const { missingKeys, promise } = mustContain(params, ...expected);

return missingKeys
? promise
: busy(client._.post(`/collection${encodeQueryAsString(params)}`));
},

deleteCollection(id) {
return busy(client._.delete(`/collection/${id}`));
},

getCollection(id) {
return busy(client._.get(`/collection/${id}`));
},

editCollection(collection = {}) {
const expected = ['name', 'description'];
const params = filterQuery(collection, ...expected);
const { missingKeys, promise } = mustContain(collection, '_id');

return missingKeys
? promise
: busy(
client._.put(
`/collection/${collection._id}${encodeQueryAsString(params)}`
)
);
},

getCollectionAccess(id) {
return busy(client._.get(`/collection/${id}/access`));
},

editCollectionAccess(collection) {
const expected = ['access', 'public'];
const params = filterQuery(collection, ...expected);
const { missingKeys, promise } = mustContain(collection, '_id');

return missingKeys
? promise
: busy(
client._.put(
`/collection/${collection._id}/access${encodeQueryAsString(
params
)}`
)
);
},
};
}
file.js
/* global FormData XMLHttpRequest */

/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
progress,
}) {
function uploadChunk(uploadId, offset, chunk) {
return new Promise((resolve, reject) => {
const data = new FormData();
data.append('uploadId', uploadId);
data.append('offset', offset);
data.append('chunk', chunk);

const xhr = new XMLHttpRequest();

function extractResponse(ctx) {
return {
ctx,
data: JSON.parse(xhr.responseText),
status: xhr.status,
statusText: xhr.statusText,
headers: {},
config: {},
};
}

xhr.addEventListener('progress', (event) => {
if (event.lengthComputable) {
const complete = event.loaded / event.total;
console.log('chunk progress', complete);
}
});
xhr.addEventListener('load', (event) => {
resolve(extractResponse('load'));
});
xhr.addEventListener('error', (event) => {
console.log('Transfer as failed', event);
reject(extractResponse('error'));
});
xhr.addEventListener('abort', (event) => {
console.log('Transfer as been canceled', event);
reject(extractResponse('abort'));
});

xhr.open('POST', `${client.baseURL}/file/chunk`, true);
xhr.responseType = 'text';
xhr.setRequestHeader('Accept', 'application/json, text/plain, */*');
xhr.setRequestHeader('Girder-Token', client.token);
xhr.send(data);
});
}

function uploadFileToItem(params, file) {
const fileId = `${file.name}_${file.lastModified}`;
// upload file to item
return new Promise((resolve, reject) => {
busy(client._.post(`/file${encodeQueryAsString(params)}`))
.then((upload) => {
const chunkSize = 10 * 1024 * 1024;
const uploadNextChunk = (offset) => {
let blob;
progress(fileId, offset, file.size);
if (offset + chunkSize >= file.size) {
blob = file.slice(offset);
uploadChunk(upload.data._id, offset, blob)
.then((uploadResp) => {
progress(fileId, file.size, file.size);
resolve(uploadResp);
})
.catch((error) => {
console.warn('could not upload final chunk');
console.warn(error);
reject(error);
});
} else {
blob = file.slice(offset, offset + chunkSize);
uploadChunk(upload.data._id, offset, blob)
.then((uploadResp) => {
uploadNextChunk(offset + chunkSize);
})
.catch((error) => {
console.warn('could not upload chunk');
console.warn(error);
reject(error);
});
}
};
uploadNextChunk(0);
})
.catch((error) => {
console.warn('Could not upload file');
console.warn(error);
reject(error);
});
});
}

return {
uploadChunk,

uploadFileToItem,

getUploadOffset(id) {
return busy(client._.get('/file/offset', { params: { uploadId: id } }));
},

downloadFile(id, offset, endByte, contentDisposition) {
const params = { offset, endByte, contentDisposition };
Object.keys(params).forEach((key) => {
if (params[key] === null) {
delete params[key];
}
});
return busy(
client._.get(`/file/${id}/download${encodeQueryAsString(params)}`)
);
},

updateFileContent(id, size) {
return busy(client._.put(`/file/${id}/contents?size=${size}`));
},

deleteFile(id) {
return busy(client._.delete(`/file/${id}`));
},

editFile(file) {
const expected = ['name', 'mimeType'];
const params = filterQuery(file, ...expected);
const { missingKeys, promise } = mustContain(file, '_id');

return missingKeys
? promise
: busy(client._.put(`/file/${file._id}${encodeQueryAsString(params)}`));
},

newFile(file) {
const expected = [
'parentType',
'parentId',
'name',
'size',
'mimeType',
'linkUrl',
];
const params = filterQuery(file, ...expected);
const { missingKeys, promise } = mustContain(
file,
'parentType',
'parentId',
'name'
);

return missingKeys
? promise
: busy(client._.post(`/file${encodeQueryAsString(params)}`));
},
};
}
folder.js
/* eslint-disable no-underscore-dangle */
function jsonToString(data) {
return JSON.stringify(data);
}

export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
listFolders(query = {}) {
const allowed = [
'parentType',
'parentId',
'text',
'limit',
'offset',
'sort',
'sortdir',
];
const params = filterQuery(query, ...allowed);

return busy(client._.get('/folder', { params }));
},

createFolder(folder) {
const allowed = [
'parentType',
'parentId',
'name',
'description',
'public',
];
const params = filterQuery(folder, ...allowed);
const { missingKeys, promise } = mustContain(
folder,
'parentType',
'parentId',
'name'
);

return missingKeys
? promise
: busy(client._.post(`/folder${encodeQueryAsString(params)}`));
},

editFolderMetaData(id, metadata) {
return busy(
client._.put(`/folder/${id}`, metadata, {
transformRequest: jsonToString,
})
);
},

deleteFolder(id) {
return busy(client._.delete(`/folder/${id}`));
},

getFolder(id) {
return busy(client._.get(`/folder/${id}`));
},

editFolder(folder) {
const allowed = ['parentType', 'parentId', 'name', 'description'];
const params = filterQuery(folder, ...allowed);
const { missingKeys, promise } = mustContain(folder, '_id');

return missingKeys
? promise
: busy(
client._.put(`/folder/${folder._id}${encodeQueryAsString(params)}`)
);
},

downloadFolder(id) {
return busy(client._.get(`/folder/${id}/download`));
},

getFolderAccess(id) {
return busy(client._.get(`/folder/${id}/access`));
},

editFolderAccess(folder) {
const allowed = ['access', 'public'];
const params = filterQuery(folder, ...allowed);
const { missingKeys, promise } = mustContain(folder, '_id');

return missingKeys
? promise
: busy(
client._.put(
`/folder/${folder._id}/access${encodeQueryAsString(params)}`
)
);
},
};
}
group.js
/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
updateGroupModerator(groupId, userId, onOff) {
const url = `/group/${groupId}/moderator?userId=${userId}`;
return onOff ? client._.post(url) : client._.delete(url);
},

updateGroupAdmin(groupdId, userId, onOff) {
const url = `/group/${groupdId}/admin?userId=${userId}`;
return onOff ? client._.post(url) : client._.delete(url);
},

createGroup(group) {
const expected = ['name', 'description', 'public'];
const params = filterQuery(group, ...expected);
const { missingKeys, promise } = mustContain(params, 'name');

return missingKeys
? promise
: busy(client._.post(`/group${encodeQueryAsString(params)}`));
},

deleteGroup(id) {
return busy(client._.delete(`/group/${id}`));
},

getGroup(id) {
return busy(client._.get(`/group/${id}`));
},

editGroup(group = {}) {
const expected = ['name', 'description', 'public'];
const params = filterQuery(group, ...expected);
const { missingKeys, promise } = mustContain(group, '_id');

return missingKeys
? promise
: busy(
client._.put(`/group/${group._id}${encodeQueryAsString(params)}`)
);
},

listGroupInvitations(id, query = {}) {
const allowed = ['limit', 'offset', 'sort', 'sortdir'];
const params = filterQuery(query, ...allowed);

return busy(
client._.get(`/group/${id}/invitation`, {
params,
})
);
},

addGroupInvitation(id, options = {}) {
const allowed = ['userId', 'level', 'quiet'];
const params = filterQuery(options, ...allowed);
const { missingKeys, promise } = mustContain(params, 'userId');

return missingKeys
? promise
: busy(
client._.post(
`/group/${id}/invitation${encodeQueryAsString(params)}`
)
);
},

listGroupMembers(id, query = {}) {
const allowed = ['limit', 'offset', 'sort', 'sortdir'];
const params = filterQuery(query, ...allowed);

return busy(
client._.get(`/group/${id}/member`, {
params,
})
);
},

removeUserFromGroup(id, userId) {
const params = { userId };
return busy(
client._.delete(`/group/${id}/member`, {
params,
})
);
},

joinGroup(id) {
return busy(client._.post(`/group/${id}/member`));
},

getGroupAccess(id) {
return busy(client._.get(`/group/${id}/access`));
},
};
}
index.js
import assetstore from './assetstore';
import collection from './collection';
import file from './file';
import folder from './folder';
import group from './group';
import item from './item';
import resource from './resource';
import system from './system';
import user from './user';

export default [
assetstore,
collection,
file,
folder,
group,
item,
resource,
system,
user,
];
item.js
/* eslint-disable no-underscore-dangle */
function jsonToString(data) {
return JSON.stringify(data);
}

export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
downloadItem(id, offset, endByte, contentDisposition) {
const params = { offset, endByte, contentDisposition };
Object.keys(params).forEach((key) => {
if (params[key] === null) {
delete params[key];
}
});
return busy(
client._.get(`/item/${id}/download${encodeQueryAsString(params)}`)
);
},

updateItemMetadata(id, metadata = {}) {
return busy(
client._.put(`/item/${id}/metadata`, metadata, {
transformRequest: jsonToString,
})
);
},

// query = { folderId, text, limit, offset, sort, sortdir }
listItems(query = {}) {
const allowed = [
'folderId',
'text',
'limit',
'offset',
'sort',
'sortdir',
];
const params = filterQuery(query, ...allowed);

return busy(client._.get(`/item${encodeQueryAsString(params)}`));
},

createItem(folderId, name, description = '') {
const params = {
folderId,
name,
description,
};
return busy(client._.post(`/item${encodeQueryAsString(params)}`));
},

// query = { limit, offset, sort }
listFiles(id, query) {
const allowed = ['limit', 'offset', 'sort'];
const params = filterQuery(query, ...allowed);

if (!id) {
return Promise.reject(new Error('No argument id provided'));
}

return busy(
client._.get(`/item/${id}/files${encodeQueryAsString(params)}`)
);
},

getItemRootPath(id) {
return busy(client._.get(`/item/${id}/rootpath`));
},

getItem(id) {
return busy(client._.get(`/item/${id}`));
},

deleteItem(id) {
return busy(client._.delete(`/item/${id}`));
},

// item = { id, folderId, name, description }
editItem(item) {
const expected = ['folderId', 'name', 'description'];
const params = filterQuery(item, ...expected);
const { missingKeys, promise } = mustContain(params, '_id');

return missingKeys
? promise
: busy(client._.put(`/item/${item._id}${encodeQueryAsString(params)}`));
},

// destinationItem = { folderId, name, description }
copyItem(id, destinationItem) {
const expected = ['folderId', 'name', 'description'];
const params = filterQuery(destinationItem, ...expected);
const { missingKeys, promise } = mustContain(params, 'folderId');

return missingKeys
? promise
: busy(client._.post(`/item/${id}/copy${encodeQueryAsString(params)}`));
},
};
}
resource.js
/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
downloadResources(resourceList, withMetadata = false) {
const params = {
resourceList: JSON.toString(resourceList),
withMetadata,
};

return busy(
client._.get('/resource/download', {
params,
})
);
},

searchResources(query, types) {
const params = {
q: JSON.toString(query),
types: JSON.toString(types),
};
return busy(
client._.get('/resource/search', {
params,
})
);
},

deleteResources(resourceList) {
const params = {
resources: JSON.toString(resourceList),
};
return busy(
client._.delete('/resource', {
params,
})
);
},
};
}
system.js
/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
deleteSetting(key) {
return busy(
client._.delete(`/system/setting${encodeQueryAsString({ key })}`)
);
},

getSettings(settings) {
const expected = ['key', 'list', 'default'];
const params = filterQuery(settings, ...expected);

return busy(
client._.get('/system/setting', {
params,
})
);
},

setSettings(keyValueMap) {
const list = Object.keys(keyValueMap).map((key) => {
const value = keyValueMap[key];
return {
key,
value,
};
});

return busy(
client._.put(`/system/setting${encodeQueryAsString({ list })}`)
);
},

getServerVersion() {
return busy(client._.get('/system/version'));
},

listUnfinishedUpload(query = {}) {
const allowed = [
'uploadId',
'userId',
'parentId',
'assetstoreId',
'minimumAge',
'includeUntracked',
'limit',
'offset',
'sort',
'sortdir',
];
const params = filterQuery(query, ...allowed);

return busy(
client._.get('/system/uploads', {
params,
})
);
},

removeUnfinishedUpload(query = {}) {
const allowed = [
'uploadId',
'userId',
'parentId',
'assetstoreId',
'minimumAge',
'includeUntracked',
];
const params = filterQuery(query, ...allowed);

return busy(
client._.delete(`/system/uploads${encodeQueryAsString(params)}`)
);
},

listPlugins() {
return busy(client._.get('/system/plugins'));
},

setActivePlugins(plugins) {
return busy(
client._.put(`/system/plugins${encodeQueryAsString({ plugins })}`)
);
},
};
}
user.js
/* eslint-disable no-underscore-dangle */
export default function ({
client,
filterQuery,
mustContain,
busy,
encodeQueryAsString,
}) {
return {
listUsers(query) {
const params = filterQuery(
query,
'text',
'limit',
'offset',
'sort',
'sortdir'
);
return busy(
client._.get('/user', {
params,
})
);
},

createUser(user) {
const expected = [
'login',
'email',
'firstName',
'lastName',
'password',
'admin',
];
const params = filterQuery(user, ...expected);
const { missingKeys, promise } = mustContain(user, ...expected);

return missingKeys
? promise
: busy(client._.post(`/user${encodeQueryAsString(params)}`));
},

changePassword(old, newPassword) {
const params = {
old,
new: newPassword,
};
return busy(client._.put(`/user/password${encodeQueryAsString(params)}`));
},

resetPassword(email) {
const params = {
email,
};
return busy(
client._.delete('/user/password', {
params,
})
);
},

deleteUser(id) {
return busy(client._.delete(`/user/${id}`));
},

getUser(id) {
return busy(client._.get(`/user/${id}`));
},

updateUser(user) {
const expected = ['email', 'firstName', 'lastName', '_id'];
const params = filterQuery(user, ...expected.slice(0, 3)); // Remove '_id'
const { missingKeys, promise } = mustContain(user, ...expected);

return missingKeys
? promise
: busy(client._.put(`/user/${user._id}${encodeQueryAsString(params)}`));
},
};
}