Skip to content

Instantly share code, notes, and snippets.

@cbare
Last active December 12, 2015 01:48
Show Gist options
  • Select an option

  • Save cbare/4693562 to your computer and use it in GitHub Desktop.

Select an option

Save cbare/4693562 to your computer and use it in GitHub Desktop.
##
## example session with the python synapse client
## Note: You'll have to insert your credentials and a project ID
######################################################################
## import the synapse library and sign in
import synapseclient
syn = synapseclient.Synapse()
syn.login('me@gmail.com', 'secret')
## retrieve an existing project
project = syn.getEntity('syn123')
## create a new entity under the existing project
## and upload a file to that entity
filename = 'path/to/my/file'
entity = {
'entityType': 'org.sagebionetworks.repo.model.Data',
'name': 'My shiny new entity',
'description': 'A description goes here',
'parentId': project['id'] }
entity = syn.createEntity(entity)
syn.uploadFile(entity, filename)
## query for files under an existing project
results = syn.query("select id, name from entity where entity.parentId=='%s'" % project['id'])
## we don't currently have a method to get the user's profile
## until we add it, here's how to do it.
import requests
headers = {'Content-type': 'application/json', 'Accept': 'application/json', 'sessionToken': syn.sessionToken }
response = requests.get(syn.repoEndpoint + "/userProfile", headers=headers)
user = response.json()
## now that we have a user profile, we can use it's ID to
## query for Data entities owned by that user
results = syn.query('select id, name, parentId from data where createdByPrincipalId == %s' % (user['ownerId'],))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment