oneibl.webclient

class oneibl.webclient.AlyxClient(**kwargs)

Class that implements simple GET/POST wrappers for the Alyx REST API http://alyx.readthedocs.io/en/latest/api.html

authenticate(username='', password='', base_url='')

Gets a security token from the Alyx REST API to create requests headers. Credentials are in the params_secret_template.py file

Parameters:
  • username (str) – Alyx database user
  • password (str) – Alyx database password
  • base_url (str) – Alyx server address, including port and protocol
delete(rest_query)

Sends a DELETE request to the Alyx server. Will raise an exception on any status_code other than 200, 201.

Parameters:rest_query (str) – examples: ‘/weighings/c617562d-c107-432e-a8ee-682c17f9e698’ ‘https://test.alyx.internationalbrainlab.org/weighings/c617562d-c107-432e-a8ee-682c17f9e698’.
Returns:(dict/list) json interpreted dictionary from response
get(rest_query)

Sends a GET request to the Alyx server. Will raise an exception on any status_code other than 200, 201. For the dictionary contents and list of endpoints, refer to: https://alyx.internationalbrainlab.org/docs

Parameters:rest_query (str) – example: ‘/sessions?user=Hamish’.
Returns:(dict/list) json interpreted dictionary from response
patch(rest_query, data=None)

Sends a PATCH request to the Alyx server. For the dictionary contents, refer to: https://alyx.internationalbrainlab.org/docs

Parameters:
  • rest_query (str) – (required)the endpoint as full or relative URL
  • data (None, dict or str) – json encoded string or dictionary
Returns:

response object

post(rest_query, data=None)

Sends a POST request to the Alyx server. For the dictionary contents, refer to: https://alyx.internationalbrainlab.org/docs

Parameters:
  • rest_query (str) – (required)the endpoint as full or relative URL
  • data (None, dict or str) – dictionary or json encoded string
Returns:

response object

put(rest_query, data=None)

Sends a PUT request to the Alyx server. For the dictionary contents, refer to: https://alyx.internationalbrainlab.org/docs

Parameters:
  • rest_query (str) – (required)the endpoint as full or relative URL
  • data (None, dict or str) – dictionary or json encoded string
Returns:

response object

rest(url=None, action=None, id=None, data=None, **kwargs)

alyx_client.rest(): lists endpoints alyx_client.rest(endpoint): lists actions for endpoint alyx_client.rest(endpoint, action): lists fields and URL

Example with a rest endpoint with all actions

>>> alyx.client.rest('subjects', 'list')
    alyx.client.rest('subjects', 'list', field_filter1='filterval')
    alyx.client.rest('subjects', 'create', data=sub_dict)
    alyx.client.rest('subjects', 'read', id='nickname')
    alyx.client.rest('subjects', 'update', id='nickname', data=sub_dict)
    alyx.client.rest('subjects', 'partial_update', id='nickname', data=sub_ict)
    alyx.client.rest('subjects', 'delete', id='nickname')
Parameters:
  • url – endpoint name
  • action – ‘list’, ‘create’, ‘read’, ‘update’, ‘partial_update’, ‘delete’
  • id – lookup string for actions ‘read’, ‘update’, ‘partial_update’, and ‘delete’
  • data – data dictionary for actions ‘update’, ‘partial_update’ and ‘create’
  • **kwargs – filter as per the REST documentation
Returns:

list of queried dicts (‘list’) or dict (other actions)

oneibl.webclient.dataset_record_to_url(dataset_record)

Extracts a list of files urls from a list of dataset queries.

Parameters:dataset_record (list) – dataset Json from a rest request.
Returns:(list) a list of strings representing files urls corresponding to the datasets records
oneibl.webclient.file_record_to_url(file_records, urls=[])

Translate a Json dictionary to an usable http url for downlading files.

Parameters:
  • file_records (dict) – json containing a ‘data_url’ field
  • urls (list) – a list of strings containing previous data_urls on which new urls will be appended
Returns:

urls: (list) a list of strings representing full data urls

oneibl.webclient.http_download_file(full_link_to_file, *, clobber=False, offline=False, username='', password='', cache_dir='')
Parameters:
  • full_link_to_file (str) – http link to the file.
  • clobber (bool) – [False] If True, force overwrite the existing file.
  • username (str) – [‘’] authentication for password protected file server.
  • password (str) – [‘’] authentication for password protected file server.
  • cache_dir (str) – [‘’] directory in which files are cached; defaults to user’s Download directory.
Returns:

(str) a list of the local full path of the downloaded files.

oneibl.webclient.http_download_file_list(links_to_file_list, **kwargs)

Downloads a list of files from the flat Iron from a list of links. Same options behaviour as http_download_file

Parameters:links_to_file_list (list) – list of http links to files.
Returns:(list) a list of the local full path of the downloaded files.