Locked History Actions

attachment:scaffold_user.py of Events/GCC2014/TrainingDay/API

Attachment 'scaffold_user.py'

Download

   1 #!/usr/bin/env python
   2 
   3 import sys
   4 import os
   5 import json
   6 import requests
   7 
   8 BASE_URL = 'http://localhost:8080'
   9 
  10 # -----------------------------------------------------------------------------
  11 def create_user( username, email, password ):
  12     full_url = BASE_URL + '/api/users'
  13 
  14     data = {
  15         'key'           : 'MASTERLOCK',
  16         'username'      : username,
  17         'email'         : email,
  18         'password'      : password
  19     }
  20     return requests.post( full_url, data=data )
  21 
  22 
  23 # -----------------------------------------------------------------------------
  24 def create_quota( name, description, user_id, amount ):
  25     full_url = BASE_URL + '/api/quotas'
  26 
  27     data = {
  28         'key'           : open( '.api-key' ).readline().strip(),
  29         'name'          : name,
  30         'description'   : description,
  31         'operation'     : '=',
  32         'amount'        : amount,
  33         'default'       : 'no',
  34         'in_users'      : user_id
  35     }
  36     return requests.post( full_url, data=data )
  37 
  38 
  39 # -----------------------------------------------------------------------------
  40 def create_history_as_another_user( user_id, name ):
  41     full_url = BASE_URL + '/api/histories'
  42     data = {
  43         'key'           : open( '.api-key' ).readline().strip(),
  44         'run_as'        : user_id,
  45         'name'          : name
  46     }
  47     return requests.post( full_url, data=data )
  48 
  49 
  50 # -----------------------------------------------------------------------------
  51 def upload_file_as_another_user( user_id, history_id, filepath, **kwargs ):
  52     full_url = BASE_URL + '/api/tools'
  53 
  54     inputs = {
  55         'files_0|type'  : 'upload_dataset',
  56         'ajax_upload'   : u'true',
  57         'files_0|NAME'  : os.path.basename( filepath ),
  58         'dbkey'         : '?',
  59         'file_type'     : 'auto',
  60     }
  61     data = {
  62         'key'           : open( '.api-key' ).readline().strip(),
  63         'tool_id'       : 'upload1',
  64         'history_id'    : history_id,
  65         'inputs'        : json.dumps( inputs ),
  66         'run_as'        : user_id
  67     }
  68 
  69     response = None
  70     with open( filepath, 'rb' ) as file_to_upload:
  71         files = { 'files_0|file_data' : file_to_upload }
  72         response = requests.post( full_url, data=data, files=files )
  73     return response
  74 
  75 
  76 # -----------------------------------------------------------------------------
  77 if __name__ == '__main__':
  78     username, email, password = sys.argv[1:4]
  79     # create user
  80     response = create_user( username, email, password )
  81     user_data = response.json()
  82     user_id = user_data[ 'id' ]
  83 
  84     response = create_quota( 'individual quota', 'default new user quota', user_id, '20gb' )
  85     response = create_history_as_another_user( user_id, 'starter history' )
  86     history_data = response.json()
  87     history_id = history_data[ 'id' ]
  88 
  89     response = upload_file_as_another_user( user_id, history_id, 'data/5.bed' )
  90     print user_data

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2014-07-20 20:52:44, 281.3 KB) [[attachment:The Galaxy API (GCC2014 TD API).pdf]]
  • [get | view] (2014-07-01 14:55:36, 8.2 KB) [[attachment:api-scripts.completed.tar.gz]]
  • [get | view] (2014-07-01 14:55:22, 8.4 KB) [[attachment:api-scripts.exercises.tar.gz]]
  • [get | view] (2014-07-01 15:06:45, 0.6 KB) [[attachment:disk_usage_and_job_report.py]]
  • [get | view] (2014-07-01 15:07:52, 2.2 KB) [[attachment:install_toolshed_yaml.py]]
  • [get | view] (2014-07-01 15:08:02, 2.3 KB) [[attachment:install_toolshed_yaml_COMPLETE.py]]
  • [get | view] (2014-07-01 15:07:02, 1.4 KB) [[attachment:run_as.dataset_upload.py]]
  • [get | view] (2014-07-01 15:08:22, 2.8 KB) [[attachment:scaffold_user.py]]
  • [get | view] (2014-07-01 15:07:30, 0.1 KB) [[attachment:tools.yaml]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.