def load_batch(server_context, assay_id, batch_id):
    """
    Loads a batch from the server.
    :param server_context: A LabKey server context. See utils.create_server_context.
    :param assay_id: The protocol id of the assay from which to load a batch.
    :param batch_id:
    :return:
    """
    load_batch_url = build_url(server_context, 'assay', 'getAssayBatch.api')
    session = server_context['session']
    loaded_batch = None

    payload = {'assayId': assay_id, 'batchId': batch_id}

    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

    try:
        response = session.post(load_batch_url,
                                data=json.dumps(payload, sort_keys=True),
                                headers=headers)
        json_body = handle_response(response)
        if json_body is not None:
            loaded_batch = Batch.from_data(json_body['batch'])
    except SSLError as e:
        raise ServerContextError(e)

    return loaded_batch
Example #2
0
def load_batch(server_context, assay_id, batch_id):
    """
    Loads a batch from the server.
    :param server_context: A LabKey server context. See utils.create_server_context.
    :param assay_id: The protocol id of the assay from which to load a batch.
    :param batch_id:
    :return:
    """
    load_batch_url = build_url(server_context, 'assay', 'getAssayBatch.api')
    session = server_context['session']
    loaded_batch = None

    payload = {
        'assayId': assay_id,
        'batchId': batch_id
    }

    headers = {
        'Content-type': 'application/json',
        'Accept': 'text/plain'
    }

    try:
        response = session.post(load_batch_url, data=json.dumps(payload, sort_keys=True), headers=headers)
        json_body = handle_response(response)
        if json_body is not None:
            loaded_batch = Batch.from_data(json_body['batch'])
    except SSLError as e:
        raise ServerContextError(e)

    return loaded_batch
Example #3
0
def _make_request(server_context, url, payload, headers=None, timeout=_default_timeout):
    try:
        session = server_context['session']
        raw_response = session.post(url, data=payload, headers=headers, timeout=timeout)
        return handle_response(raw_response)
    except SSLError as e:
        raise ServerContextError(e)
Example #4
0
def _make_request(server_context,
                  url,
                  payload,
                  headers=None,
                  timeout=_default_timeout):
    try:
        session = server_context['session']
        raw_response = session.post(url,
                                    data=payload,
                                    headers=headers,
                                    timeout=timeout)
        return handle_response(raw_response)
    except SSLError as e:
        raise ServerContextError(e)
Example #5
0
def save_batches(server_context, assay_id, batches):
    """
    Saves a modified batches.
    :param server_context: A LabKey server context. See utils.create_server_context.
    :param assay_id: The assay protocol id.
    :param batches: The Batch(es) to save.
    :return:
    """

    save_batch_url = build_url(server_context, 'assay', 'saveAssayBatch.api')
    session = server_context['session']

    json_batches = []
    if batches is None:
        return None  # Nothing to save

    for batch in batches:
        if isinstance(batch, Batch):
            json_batches.append(batch.to_json())
        else:
            raise Exception('save_batch() "batches" expected to be a set Batch instances')

    payload = {
        'assayId': assay_id,
        'batches': json_batches
    }
    headers = {
        'Content-type': 'application/json',
        'Accept': 'text/plain'
    }

    try:
        # print(payload)
        response = session.post(save_batch_url, data=json.dumps(payload, sort_keys=True), headers=headers)
        json_body = handle_response(response)
        if json_body is not None:
            resp_batches = json_body['batches']
            return [Batch.from_data(resp_batch) for resp_batch in resp_batches]
    except SSLError as e:
        raise ServerContextError(e)

    return None
def save_batches(server_context, assay_id, batches):
    """
    Saves a modified batches.
    :param server_context: A LabKey server context. See utils.create_server_context.
    :param assay_id: The assay protocol id.
    :param batches: The Batch(es) to save.
    :return:
    """

    save_batch_url = build_url(server_context, 'assay', 'saveAssayBatch.api')
    session = server_context['session']

    json_batches = []
    if batches is None:
        return None  # Nothing to save

    for batch in batches:
        if isinstance(batch, Batch):
            json_batches.append(batch.to_json())
        else:
            raise Exception(
                'save_batch() "batches" expected to be a set Batch instances')

    payload = {'assayId': assay_id, 'batches': json_batches}
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

    try:
        # print(payload)
        response = session.post(save_batch_url,
                                data=json.dumps(payload, sort_keys=True),
                                headers=headers)
        json_body = handle_response(response)
        if json_body is not None:
            resp_batches = json_body['batches']
            return [Batch.from_data(resp_batch) for resp_batch in resp_batches]
    except SSLError as e:
        raise ServerContextError(e)

    return None
Example #7
0
def update_wiki(server_context, wiki_name, wiki_body, container_path=None):
    """
    Used to update an existing wiki page
    :param server_context: A LabKey server context. See labkey.utils.create_server_context.
    :param wiki_name: The name of the wiki.
    :param wiki_body: The body of the wiki.
    :param container_path: Optional container path that can be used to override the server_context container path
    :return: returns a dictionary containing the response from the server. The 'success' key
    in the dictionary will be true when the wiki was successfully updated. It will be false
    in the case of a failure.  In the case of a failure, the 'error' key contains the error
    message returned by the server.
    """
    # Build the URL for reading the wiki page
    read_wiki_url = build_url(server_context,
                              'wiki',
                              'editWiki.api',
                              container_path=container_path)
    payload = {'name': wiki_name}
    headers = {'Content-type': 'application/json'}

    session = server_context['session']

    try:
        read_response = session.get(read_wiki_url,
                                    params=payload,
                                    headers=headers)
    except SSLError as e:
        print(
            "There was a problem while attempting to submit the read for the wiki page "
            + str(wiki_name) + " via the URL " + str(e.geturl()) +
            ". The HTTP response code was " + str(e.getcode()))
        print("The HTTP client error was: " + format(e))
        return 1  # TODO: this is incorrect, should return 'success'/'error' properly like the docs say

    data = read_response.text

    # Search HTML response for required information on wiki. This is stored in the javascript
    # variable named
    #  - _wikiProps: for 14.3 and earlier
    #  - LABKEY._wiki.setProps for 15.1 and later
    data_list = data.split('\n')

    # If LabKey Server is v14.3 or earlier find line containing '_wikiProp'
    v = next((i for i in range(len(data_list)) if '_wikiProp' in data_list[i]),
             None)

    # If v = None, then server is running 15.1 or later and find the line
    # containing 'LABKEY._wiki.setProps'
    if v is None:
        v = next((i for i in range(len(data_list))
                  if 'LABKEY._wiki.setProps' in data_list[i]), None)

    # Verify that we found the variable in the HTML response. If not
    # do not proceed
    if v is None:
        print(
            "There was a problem while attempting to read the data for the wiki page '"
            + str(wiki_name) + "'.")
        print(
            "The script is unable to find the wiki properties in the HTML response"
        )
        return 1  # TODO: this is incorrect, should return 'success'/'error' properly like the docs say

    wiki_vars = {}
    for j in range(100):
        # Read each line, until find a javascript closing bracket.
        if '};' in data_list[v + j + 1]:
            break
        if '});' in data_list[v + j + 1]:
            break
        wvar = data_list[v + j + 1].rstrip().lstrip().replace('\'',
                                                              '').replace(
                                                                  ',', '')
        wiki_vars[wvar.split(':')[0]] = wvar.split(':')[1]

    # Build the URL for updating the wiki page
    update_wiki_url = build_url(server_context,
                                'wiki',
                                'saveWiki.api',
                                container_path=container_path)
    headers = {'Content-type': 'application/json'}

    # Update wiki_vars to use the new wiki content.
    wiki_vars['name'] = wiki_name
    wiki_vars['body'] = wiki_body

    try:
        response = session.post(update_wiki_url,
                                data=json.dumps(wiki_vars, sort_keys=True),
                                headers=headers)
        data = handle_response(response)
    except SSLError as e:
        print(
            "There was a problem while attempting to submit the read for the wiki page '"
            + str(wiki_name) + "' via the URL " + str(e.geturl()) +
            ". The HTTP response code was " + str(e.getcode()))
        print("The HTTP client error was: " + format(e))
        return 1  # TODO: this is incorrect, should return 'success'/'error' properly like the docs say

    return data