eLabSDK.API

.call(options)

Make a REST API call to the eLab Journal API.

This is the core method for making HTTP requests to the eLab API. It supports all HTTP
methods (GET, POST, PUT, DELETE, PATCH) and handles request bodies, query parameters,
authentication, and response processing. The method returns an XMLHttpRequest object for
advanced control. Use this for any API operations not covered by specialized SDK methods,
or when you need direct control over API requests.

Kind: static function
Returns: XMLHttpRequest - The XMLHttpRequest object used to execute the call

ParamTypeDescription
optionsObjectConfiguration object for the API call. See eLabSDK.API.Call for full options.
options.methodstringHTTP method: 'GET', 'POST', 'PUT', 'DELETE', or 'PATCH'
options.pathstringAPI endpoint path (e.g., 'samples', 'experiments/123')
[options.body]ObjectRequest body object (for POST, PUT, PATCH requests)
[options.params]ObjectQuery string parameters as key-value pairs
[options.onSuccess]functionCallback for successful response. Receives (xhr, status, response)
[options.onError]functionCallback for error response. Receives (xhr, status, error)
[options.onComplete]functionCallback executed after request completes (success or error)

Example

// POST: Create a new sample
eLabSDK.API.call({
  method: 'POST',
  path: 'samples',
  body: {
    sampleTypeID: 123,
    name: 'My New Sample',
    description: 'Created via SDK'
  },
  onSuccess: function(xhr, status, response) {
    var newSampleID = response;
    console.log('Created sample with ID: ' + newSampleID);
    alert('Sample created successfully!');
  },
  onError: function(xhr, status, error) {
    console.error('Failed to create sample:', error);
    alert('Error: ' + error);
  }
});

Example

// GET: Retrieve experiment data
eLabSDK.API.call({
  method: 'GET',
  path: 'experiments',
  params: {
    experimentID: 456,
    expand: 'sections,samples'
  },
  onSuccess: function(xhr, status, response) {
    console.log('Experiment data:', response);
    displayExperimentDetails(response.data[0]);
  }
});

Example

// PUT: Update sample properties
eLabSDK.API.call({
  method: 'PUT',
  path: 'samples/789',
  body: {
    name: 'Updated Sample Name',
    description: 'Modified via SDK',
    quantity: 50
  },
  onSuccess: function(xhr, status, response) {
    console.log('Sample updated successfully');
  },
  onError: function(xhr, status, error) {
    console.error('Update failed:', error);
  }
});

Example

// DELETE: Remove a sample
eLabSDK.API.call({
  method: 'DELETE',
  path: 'samples/999',
  onSuccess: function(xhr, status, response) {
    console.log('Sample deleted');
    refreshSampleList();
  },
  onError: function(xhr, status, error) {
    alert('Cannot delete sample: ' + error);
  }
});

Example

// Advanced: Using XHR object for progress tracking
var xhr = eLabSDK.API.call({
  method: 'POST',
  path: 'files/upload',
  body: formData,
  onSuccess: function(xhr, status, response) {
    console.log('Upload complete');
  }
});

// Track upload progress
xhr.upload.addEventListener('progress', function(e) {
  if (e.lengthComputable) {
    var percentComplete = (e.loaded / e.total) * 100;
    console.log('Upload progress: ' + percentComplete + '%');
  }
});

© 2023 eLabNext