Scandit.DeferredHttp

Deferred HTTP request module

Classes

Response

Methods

(static) cancelRequests(query: object): Promise

Cancels and returns all deferred requests matching the given query. Will change state from one of PENDING/FAILED to CANCELED.

Parameters:
Name Type Description
query object

Query

Returns:
Promise

Promise which is resolved to Scandit.DeferredHttp.Response

Can return an error with code:

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.cancelRequests();
Scandit.DeferredHttp.cancelRequests();

(static) findRequests(query?: object, options?: object): Promise

Returns all deferred requests matching the given query

Parameters:
Name Type Attributes Default Description
query object <optional>
{}

Query

options object <optional>
{}
Properties
Name Type Attributes Default Description
limit integer <optional>
100

Number of results to return

skip integer <optional>
0

Number or results to skip

orderBy object[] <optional>
[{createdAt: DESC}]

Results order. Object is {key: direction} where key is a field name and direction is ASC or DESC.

Returns:
Promise

Promise which is resolved to array of Responses

Can return an error with code:

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.findRequests().then(function(allRequests) {
  console.log(allRequests);
});
Scandit.DeferredHttp.findRequests().then(allRequests => {
  console.log(allRequests);
});

(static) forgetRequests(query: object): Promise

Forgets/removes all deferred requests matching the given query. Requests will be completely removed from the database, including the result.

Parameters:
Name Type Description
query object

Query

Returns:
Promise

Can return an error with code:

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.forgetRequests();
Scandit.DeferredHttp.forgetRequests();

(static) get(url: string, options?: object): Promise

Performs an HTTP GET request

Parameters:
Name Type Attributes Default Description
url string

Request url

options object <optional>
{}

Request options, the same as Scandit.DeferredHttp.request

Returns:
Promise

Promise which is resolved to Scandit.DeferredHttp.Response

Can return an error with code:

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.get('http://google.com').then(function(result) {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(function(reason) {
  console.log(reason);
});
Scandit.DeferredHttp.get('http://google.com').then(result => {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(reason => {
  console.log(reason);
});

(static) getWorkerState(): Promise

Returns worker state. Possible states: OFFLINE, IDLE, WORKING

Returns:
Promise

Promise which is resolved to state name string.

Can return an error with code:

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.getWorkerState().then(function(workerState) {
  console.log(workerState);
});
Scandit.DeferredHttp.getWorkerState().then(workerState => {
  console.log(workerState);
});

(static) off(arg: (string | function))

Removes an event listener

Parameters:
Name Type Description
arg string | function

Event name or callback function

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.off('deferredRequestStateChange');
Scandit.DeferredHttp.off('deferredRequestStateChange');

(static) on(event: string, callback: function)

Adds an event listener

Parameters:
Name Type Description
event string

Event name

callback function

Event callback

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.on('deferredRequestStateChange', function(changedRequest) {
  console.log(changedRequest);
});
Scandit.DeferredHttp.on('deferredRequestStateChange', changedRequest => {
  console.log(changedRequest);
});

(static) post(url: string, options?: object): Promise

Performs an HTTP POST request

Parameters:
Name Type Attributes Default Description
url string

Request url

options object <optional>
{}

Request options, the same as Scandit.DeferredHttp.request

Returns:
Promise

Promise which is resolved to Scandit.DeferredHttp.Response

Can return an error with code:

Examples
  • JavaScript
  • ES6
// assuming that filePath is defined
Scandit.DeferredHttp.post('http://example.com', {files: {filename1: filePath}}).then(function(result) {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(function(reason) {
  console.log(reason);
});
// assuming that filePath is defined
Scandit.DeferredHttp.post('http://example.com', {files: {filename1: filePath}}).then(result => {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(reason => {
  console.log(reason);
});

(static) put(url: string, options?: object): Promise

Performs an HTTP PUT request

Parameters:
Name Type Attributes Default Description
url string

Request url

options object <optional>
{}

Request options, the same as Scandit.DeferredHttp.request

Returns:
Promise

Promise which is resolved to Scandit.DeferredHttp.Response

Can return an error with code:

Examples
  • JavaScript
  • ES6
// assuming that requestBody is defined
Scandit.DeferredHttp.put('http://example.com', {body: requestBody}).then(function(result) {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(function(reason) {
  console.log(reason);
});
// assuming that requestBody is defined
Scandit.DeferredHttp.put('http://example.com', {body: requestBody}).then(result => {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(reason => {
  console.log(reason);
});

(static) request(method: string, url: string, options?: object): Promise

Performs an HTTP request

Parameters:
Name Type Attributes Default Description
method string

Request method

url string

Request url

options object <optional>
{}

Request options

Properties
Name Type Attributes Default Description
body string <optional>

The body as a string, for example JSON

query object <optional>

Request query parameters (aka. query string)

headers object <optional>

Additional request headers. E.g. {authorization: 'Basic xxxxx'}

files object <optional>

Creates a multipart form upload POST request to send one or more files. E.g. {filename1: '/file/is/here.txt', filename2: '/another.png'}

formData object <optional>

Form body payload (will be sent together with files if provided)

timeout integer <optional>

Request timeout in seconds

connectionTimeout integer <optional>

Connection timeout in seconds

maxRetries integer <optional>
0

Max number of retries

retryInterval integer <optional>
600

Time to wait for a next retry [seconds]

retryStatusCodes string[] <optional>
[]

Unexpected status codes for which request should be retried. Accepts array of strings or numbers. String can contain wildcard "*" to match any number at given position

tag string <optional>
Returns:
Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.request('GET', 'http://google.com').then(function(result) {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(function(reason) {
  console.log(reason);
});
Scandit.DeferredHttp.request('GET', 'http://google.com').then(result => {
  console.log(result);
  // the request was added to the queue and the HTTP response will be added to it once completed
}).catch(reason => {
  console.log(reason);
});

(static) retryRequests(query: object): Promise

Retries and returns all deferred requests matching the given query. Will change state from one of (FAILED, CANCELED) to PENDING.

Parameters:
Name Type Description
query object

Query

Returns:
Promise

Promise which is resolved to Scandit.DeferredHttp.Response

Can return an error with code:

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.retryRequests();
Scandit.DeferredHttp.retryRequests();

Type Definitions

query

Format: {field: {operator: value, ...}, ...}

Type:
  • object
Properties:
Name Type Description
field string

Available fields:

  • id
  • state (PENDING, COMPLETED, FAILED, CANCELED)
  • createdAt
  • startAt
  • stateChangedAt
  • failedAt
  • retries
  • maxRetries
  • retryInterval
  • tag
Properties
Name Type Description
operator string

Available operators

  • $eq - equals
  • $neq - not equals
  • $in - is present in array
  • $nin - is not present in array
  • $gt - greater than
  • $gte - greater than or equal
  • $lt - lower than
  • $lte - lower than or equal
Examples
  • JavaScript
  • ES6
// all pending requests which failed more than 8 times for which limit is 10
{state: {$eq: 'PENDING'}, retries: {$gte: 8}, maxRetries: {$eq: 10}}

// all failed requests which failed last time more than one hour ago
{state: {$eq: 'FAILED'}, failedAt: {$lt: new Date(new Date().setHours(new Date().getHours() - 1))}}
// all pending requests which failed more than 8 times for which limit is 10
{state: {$eq: 'PENDING'}, retries: {$gte: 8}, maxRetries: {$eq: 10}}

// all failed requests which failed last time more than one hour ago
{state: {$eq: 'FAILED'}, failedAt: {$lt: new Date(new Date().setHours(new Date().getHours() - 1))}}

Events

deferredRequestStateChange

Deferred request state changed event

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.on('deferredRequestStateChange', function(changedRequest) {
  console.log(changedRequest);
});
Scandit.DeferredHttp.on('deferredRequestStateChange', changedRequest => {
  console.log(changedRequest);
});

deferredRequestsWorkerStateChange

Deferred requests worker state changed event

Examples
  • JavaScript
  • ES6
Scandit.DeferredHttp.on('deferredRequestsWorkerStateChange', function(workerState) {
  console.log(workerState);
});
Scandit.DeferredHttp.on('deferredRequestsWorkerStateChange', workerState => {
  console.log(workerState);
});