-
-
Notifications
You must be signed in to change notification settings - Fork 4
API examples
Jon Clucas edited this page Jul 23, 2018
·
19 revisions
- Login/Sign up User
- Assign user role from admin user, invite user
- CRUD volumes
- CRUD activities in volume
- CRUD screens in activity
In this set of example code blocks, variables may not be assigned on this page or may be assigned in examples earlier on the page.
import girder_client as gc
girder_connection = gc.GirderClient(
apiUrl='http://mindlogger-girder-atlas.a4vwd5q7ib.us-east-1.elasticbeanstalk.com/api/v1'
)
girder_connection.authenticate(
username=username,
password=password,
apiKey=apiKey
)
girder_connection.post(
"&".join([
"user?login={}".format(username),
"email={}".format(email),
"firstName={}".format(firstname),
"lastName={}".format(lastname),
"password='{}'".format(password),
"admin=false"
])
) # create new user
import random
import string
girder_connection.post(
"&".join([
"user?login={}".format(username),
"email={}".format(email),
"firstName={}".format(firstname),
"lastName={}".format(lastname),
"password='{}'".format(
"".join(
random.sample(
string.printable,
28
)
)
), # random password
"admin=false"
])
) # create new user
girder_connection.put(
"user/password/temporary?email={}".format(
email
)
) # send temporary password
role = "Viewers"
groups = girder_connection.get(
"group"
) # get all groups
role_id = [
group for group in groups if group['name']==role
][0]["_id"] # get Girder_id for "Viewers" group
users = girder_connection.get("user")
user_id = [
user for user in users if user['email']==email
][0]["_id"] # get Girder_id for user
girder_connection.post(
"".join([
"group/{}".format(role_id),
"/invitation?userId={}".format(user_id),
"&quiet=true&force=true"
])
)
role = "Viewers"
role_id = girder_connection.get(
"group?text={}".format(role)
)[0]["_id"]
user_id = girder_connection.get(
"user?email={}".format(email)
)[0]["_id"]
girder_connection.post(
"".join([
"group/{}".format(role_id),
"/invitation?userId={}".format(user_id),
"&quiet=true&force=true"
])
)
girder_connection.post(
"".join([
"group/{}".format(role_id),
"/invitation?userId={}".format(user_id),
"&quiet=false&force=false"
])
)
volumes_id = girder_connection.get(
"collection?text=Volumes"
)[0]["_id"]
girder_connection.post(
"&".join([
"folder?name={}".format(volume_name),
"parentId={}".format(volumes_id),
"parentType=collection",
"reuseExisting=true"
])
)
girder_connection.get(
"&".join([
"folder?parentType=folder&parentId={}".format(
girder_connection.get(
"&".join([
"folder?parentType=collection",
"parentId={}".format(volumes_id),
"text={}".format(volume_name)
])
)[0]["_id"]
)
])
) # list folders in Volume
import json
girder_connection.put(
"folder/{}".format(
girder_connection.get(
"&".join([
"folder?parentType=collection",
"parentId={}".format(volumes_id),
"text={}".format(volume_name)
])
)[0]["_id"]
),
parameters={
"metadata": json.dumps(
{'status': 'inactive'}
)
}
) # make volume's status inactive
girder_connection.delete(
"folder/{}".format(
girder_connection.get(
"&".join([
"folder?parentType=collection",
"parentId={}".format(volumes_id),
"text={}".format(volume_name)
])
)[0]["_id"]
)
)
from datetime import datetime
import json
import tzlocal
from urllib.parse import quote
activities_id = girder_connection.post(
"&".join([
"folder?name=Activities",
"parentId={}".format(volume_id),
"parentType=folder",
"reuseExisting=true"
])
)["_id"] # create or find the Activities Folder in the relevant Volume; store the _id to a variable
activity_id = girder_connection.post(
"&".join([
"folder?name={}".format(activity_name),
"parentId={}".format(activities_id),
"parentType=folder",
"reuseExisting=true"
])
)["_id"] # create or find the Activity Folder in the relevant Volume.Activities; store the _id to a variable
activity_version_id = girder_connection.post(
"&".join([
"item?name={}".format(
"{} ({})".format(
activity_name,
date.today().strftime("%F")
)
),
"folderId={}".format(activity_id),
"reuseExisting=true",
"metadata={}".format(
quote(
json.dumps(
{
"@context": {
"@base": "http://localhost:8080/api/v1/",
"devices": "http://www.w3id.org/abdn/irp/devices/",
"nidm": "http://nidm.nidash.org/specs/nidm-experiment.html#",
"oslc": "http://open-services.net/ns/core#",
"pav": "http://purl.org/pav/",
"pav:lastUpdatedOn": {
"@type": "xsd:dateTime"
},
"schema": "http://schema.org/",
"schema:description": {
"@type": "xsd:string"
},
"schema:name": {
"@type": "xsd:string"
},
"xsd": "http://www.w3.org/2001/XMLSchema"
},
"oslc:modifiedBy": [
{
"@id": "user/{}".format(
girder_connection.get(
"user/me"
)["_id"]
)
}
],
"pav:lastUpdatedOn": datetime.now(
tzlocal.get_localzone()
).strftime("%FT%T.%f%z"),
"schema:name": {
"@language": "en-US",
"@value": activity_name
}
}
)
)
)
])
)["_id"] # create a version Item of the Activity in the Activity Folder; store the _id to a variable
activities_id = girder_connection.get(
"&".join([
"folder?parentType=folder",
"parentId={}".format(
girder_connection.get(
"&".join([
"folder?parentType=collection",
"parentId={}".format(volumes_id),
"text={}".format(volume_name)
])
)[0]["_id"]
),
"name={}".format(
"Activities"
)
])
)[0]["_id"] # get "Activities" _id within Volume
activity_id = girder_connection.get(
"&".join([
"folder?parentType=folder",
"parentId={}".format(
activities_id
),
"name={}".format(
activity_name
)
])
)[0]["_id"] # get _id of named Activity
girder_connection.get(
"&".join([
"folder?parentId={}".format(
activity_id
),
"parentType=folder"
])
) # list Folders (versions) within Activity
from IPython.display import Image
activity_version_id = girder_connection.get(
"&".join([
"folder?parentId={}".format(
activity_id
),
"parentType=folder"
])
)[0]["_id"] # get the _id of the first Image
girder_connection.downloadFile(
fileId=girder_connection.get(
"folder/{}/files".format(activity_version_id)
)[0]["_id"],
path=os.path.join(
os.getcwd(),
girder_connection.get(
"folder/{}/files".format(activity_version_id)
)[0]["name"]
)
) # download the Image
Image(
filename=os.path.join(
os.getcwd(),
girder_connection.get(
"folder/{}/files".format(activity_version_id)
)[0]["name"]
)
) # display the Image
In this example, the default sequence is a sequence of three screens in a list or array that excludes the second screen, which presumably is conditional on a response in the first screen.
meta = girder_connection.get(
"folder/{}".format(activity_version_id)
)["meta"]
meta["screens"] = [
{
"@id": "item/{}".format(screens_ids[0])
},
{
"@id": "item/{}".format(screens_ids[2])
},
{
"@id": "item/{}".format(screens_ids[3])
}
]
girder_connection.put(
"?".join([
"folder/{}".format(activity_version_id),
"metadata={}".format(
quote(
json.dumps(
meta # dictionary
)
)
)
])
)
meta = girder_connection.get(
"folder/{}".format(activity_version_id)
)['meta'] # get existing metadata
meta['schema:name']["@value"] = new_name # set new name in metadata
meta['oslc:modifiedBy'] = [
{
"@id": user_id
} for user_id in set(
user["@id"] for user in meta['oslc:modifiedBy']
)
] # add self to list of modifying users if not already included
meta['pav:lastUpdatedOn'] = datetime.now(
tzlocal.get_localzone()
).strftime("%FT%T.%f%z") # set lastUpdatedOn to now
girder_connection.put(
"&".join([
"folder/{}?name={}".format(
activity_version_id,
"{} ({})".format(
new_name,
date.today().strftime("%F")
)
), # rename activity version
"folderId={}".format(
girder_connection.post(
"&".join([
"folder?parentId={}".format(
activities_id
),
"parentType=folder",
"name={}".format(
new_name
),
"reuseExisting=true"
])
)["_id"]
), # move to new_name activity folder, creating if not exists
"metadata={}".format(quote(json.dumps(meta))), # update metadata,
"reuseExisting=true"
])
)
from urllib.request import urllopen
image_stream = urlopen(url_or_filepath) # url_or_filepath includes protocol, eg, "https://", "file://", "ftp://"
img_id = girder_connection.uploadFile(
parentId=activity_version_id,
parentType=folder,
stream=image_stream,
name=".".join([
new_name,
image_stream.info()["Content-Type"].split("/")[-1]
]), # name to save the File as in Mindlogger, including extension
size=int(
image_stream.info()["Content-Length"]
) # size of the File in bytes
)["_id"]
girder_connection.delete(
"folder/{}".format(activity_version_id)
)
girder_connection.delete(
"folder/{}".format(folder_id)
)
screen_id = girder_connection.post(
"&".join([
"item?name={}".format(
screen_name
),
"folderId={}".format(activity_version_id),
"reuseExisting=true",
"metadata={}".format(
quote(
json.dumps(
meta # dictionary
)
)
)
])
)["_id"] # create a Screen in the Activity Folder; store the _id to a variable
In this example, screens_ids
is a list or array of Girder_id strings pointing to screens.
After reading each option in the first screen, we add a 'next_screen'
entry (pointing to the second screen) to each option whose value is not None
. For options with value None
, the current 'next_screen'
entry is kept if one exists, otherwise the next screen in the default sequence follows the first question in screens_ids
.
meta = girder_connection.get(
"item/{}".format(screens_ids[0])
)["meta"]
meta['options'] = [
*[
{
**option,
'next_screen': {
"@id": "item/{}".format(
screens_ids[1]
)
}
} for option in meta['options'] if option['value'] is not None
],
*[
option for option in meta['options'] if option['value'] is None
]
]
girder_connection.put(
"?".join([
"item/{}".format(screens_ids[0]),
"metadata={}".format(
quote(
json.dumps(
meta
)
)
)
])
)
girder_connection.delete(
"item/{}".format(screen_id)
)