Giter Club home page Giter Club logo

api-js's Introduction

Voxel51 Platform JavaScript Client Library

A JavaScript client library built on Node.js for the Voxel51 Platform.

Available at https://github.com/voxel51/api-js.

voxel51-logo.png

Installation

To install the library, first clone it:

git clone https://github.com/voxel51/api-js

and then run the install script:

cd api-js
bash install.bash

Documentation

This library is implemented with ES6-style classes and uses async/await to deliver Promised-based asynchronous execution.

For full documentation of the Voxel51 Platform API, including usage of this client library, see the API Documentation.

To learn how to use this client library to create and run jobs that execute each of the analytics exposed on the Voxel51 Platform, see the Analytics Documentation.

For more information about using this client library to operate an application on the Voxel51 Platform, see the Applications Quickstart.

Quickstart

This section provides a brief guide to using the Platform API with this client library.

Authentication

To use the API, you must first create an account at https://console.voxel51.com and download an API token.

Keep your API token private; it is your access key to the API.

Each API request you make must be authenticated by your token. There are a variety of ways to activate your token; for a full description, see the Authentication Documentation.

A simple approach is to set the VOXEL51_API_TOKEN environment variable in your shell to point to your API token file:

export VOXEL51_API_TOKEN=/path/to/your/api-token.json

Alternatively, you can permanently activate a token by executing the following commands:

let voxel51 = require('.');

voxel51.users.auth.activateToken('/path/to/your/api-token.json');

In the latter case, your token is copied to ~/.voxel51/ and will be automatically used in all future sessions. A token can be deactivated via the voxel51.users.auth.deactivateToken() method.

After you have activated an API token, you have full access to the API.

API Sessions

To initialize an API session, issue the following commands:

let voxel51 = require('.');

let api = new voxel51.users.api.API();

Analytics

List available analytics:

api.listAnalytics().then(function(analytics) {
  // Use analytics
});

Get documentation for the analytic with the given ID:

// ID of the analytic
let analyticId = 'XXXXXXXX';

api.getAnalyticDoc(analyticId).then(function(doc) {
  // Use doc
});

Data

List uploaded data:

api.listData().then(function(data) {
  // Use data
});

Upload data to the cloud storage:

// Local path to the data
let dataPath = '/path/to/video.mp4';

api.uploadData(dataPath);

Jobs

List jobs you have created:

api.listJobs().then(function(jobs) {
  // Use jobs
});

Create a job request to perform an analytic on a data, where <analytic> is the name of the analytic to run, <data-id> is the ID of the data to process, and any <parameter> values are set as necessary to configre the analytic:

let jobRequest = new voxel51.users.jobs.JobRequest('<analytic>');
let inputPath = voxel51.users.jobs.RemoteDataPath.fromDataId('<data-id>');
jobRequest.setInput('<input>', inputPath);
jobRequest.setParameter('<parameter>', val);

console.log(jobRequest.toString());

Upload a job request:

let metadata = api.uploadJobRequest(jobRequest, '<job-name>');
let jobId = metadata.id;

Start a job:

api.startJob(jobId);

Get the status of a job:

api.getJobStatus(jobId).then(function(status) {
  // Use status
});

Download the output of a completed job:

// Local path to which to download the output
let outputPath = '/path/to/labels.json';

api.downloadJobOutput(jobId, outputPath).then(function() {
  console.log('Download complete!');
});

Improving Request Efficiency

A common pattern when interacting with the platform is to perform an operation to a list of data or jobs. In such cases, you can dramatically increase the efficiency of your code by taking advantage of the Promise-based nature of this library.

For example, the following code will run VehicleSense on a list of videos using Promise.all to wait for the asynchronous requests to complete:

let voxel51 = require('.');

let api = new voxel51.users.api.API();

async function runVehicleSense(paths) {
  // Upload all data
  let allData = await Promise.all(paths.map(api.uploadData));

  // Run VehicleSense jobs
  let promises = [];
  allData.forEach(function(data) {
    let jobRequest = new voxel51.users.jobs.JobRequest('voxel51/vehicle-sense');
    let remotePath = voxel51.users.jobs.RemoteDataPath.fromDataId(data.id);
    jobRequest.setInput('video', remotePath);
    let jobName = `vehicle-sense-${data.name}`;
    promises.push(api.uploadJobRequest(jobRequest, jobName, true));
  });

  return await Promise.all(promises);
}

// Run VehicleSense on all videos
let paths = ['1.mp4', '2.mp4', '3.mp4', ...];
let jobs = await runVehicleSense(paths);

Or, the following code will start all unstarted jobs on the platform, using Promise.all to wait for the asynchronous requests to complete:

let voxel51 = require('.');

let api = new voxel51.users.api.API();

async function startAllJobs() {
  // Get all unarchived jobs
  let jobsQuery = (new voxel51.users.query.JobsQuery()
    .addAllFields()
    .addSearch('archived', false));
  let result = await api.queryJobs(jobsQuery);
  let jobs = result.jobs;

  // Start all unstarted jobs
  let promises = [];
  jobs.forEach(function(job) {
    if (job.state === voxel51.users.jobs.JobState.READY) {
      promises.push(api.startJob(job.id));
    }
  });

  Promise.all(promises);
}

await startAllJobs();

Generating Documentation

This project uses JSDoc to generate its documentation from source. To generate the documentation, run:

bash docs/generate_docs.bash

To view the documentation, open the docs/build/index.html file in your browser.

Copyright

Copyright 2017-2020, Voxel51, Inc.
voxel51.com

api-js's People

Contributors

brimoor avatar dependabot[bot] avatar himapatel2011 avatar hoddr avatar jinyixin621 avatar lethosor avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mangalbhaskar

api-js's Issues

Enums not appearing in generated docs

We are using the JSDoc @enum decorator to document enums such as voxel51.users.api.AnalyticType that appear in the code. However, they are not currently appearing in the generated docs.

This issue from the JSDoc repository reports a similar behavior, but I have not been able to get the @namespace suggestion made there to work...
jsdoc/jsdoc#1184

README instructions are not correct on require statement to load client library

@hoddr I just tried to reinstall this client lib using the instructions in the README, and then I get this error:

> let voxel51 = require('.');
Error: Cannot find module './utils.js'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)

Any idea what's up?

extendDataTTL throws unexpected JSON token

extendDataTTL w/ valid input throws error:

> await api.extendDataTTL('2390bfd2-1a89-4ba9-9aff-f361cc4c5551', 3)
SyntaxError: Unexpected token < in JSON at position 0

job.getJobRequest() returns null signed url field

> await api.getJobRequest('70529251-5658-4410-ad3a-cbc04fc4ea82')
JobRequest {
  analytic: 'video-formatter',
  inputs:
   { video:
      RemoteDataPath {
        dataId: 'c89b7c0a-f646-47a3-b818-5e20a5c4e5c2',
        signedUrl: null } },
  parameters: {} }

Noting again that the upload job request will not have a signed url field in its JSON representation.

Figure out how to include multiple markdown files in the generated docs

I would like to have the README.md and APPLICATIONS.md files be top-level files in the sidebar of the generated docs, but I cannot figure out how to do this.

The current build process just puts README.md in the generated index.html, and ignores (I think?) the APPLICATIONS.md.

The workaround is to link to https://github.com/voxel51/api-js/blob/develop/APPLICATIONS.md on github.com from the README, but it would be preferable to be able to link to an HTML version on our website...

Add intra-doc links where possible

todo:
(a) replace AnalyticsQuery with {@link module:users/query~AnalyticsQuery AnalyticsQuery} so that the docs are easier to navigate
(b) replace @param {module:users/query~AnalyticsQuery} so that parameter types are linked

Unfortunately I cannot find a way to render short name links in @param links, and (b) looks exceptionally ugly as is, so I have not acted on this issue yet.

The JSDoc issue linked below seems to indicate that one will be able to automatically get short names by writing {@link namepath} without having to write {@link namepath display} using a new useShortNamesInLinks=true option in your jsdoc config. One hopes that this will apply to @param blocks as well. Unfortunately, JSDoc 3.6.0 doesn't seem to be coming out..... :(

jsdoc/jsdoc#738

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.