NetworkingModule Module

The NetworkingModule class enables effects to fetch information from a network.

Usage

const Networking = require('Networking');

Properties

This class exposes no properties.

Methods

MethodDescription

fetch(url: String)

returns Promise

Returns a promise for the result of the call. A then() clause attached to this takes a single argument; that argument is a callback function which takes a single argument, which is the result of the call. The result of the call is an object with two elements: A status property indicating the HTTP response status of the call, and json() function, which returns a promise for the dictionary containing the body of the response (which is expected to be JSON).

Remarks

AR Studio requires the URL passed to the fetch() method to be an HTTPS URL, with a certificate that chains up to a trusted certificate authority. Self-signed and other non-trusted certificates should not be expected to work.

In order to access specific URLs using fetch(), the domains of these URLs must be whitelisted. In AR Studio the domain whitelist is specified under 'Networking' capability in Project -> Edit Properties... -> Capabilities. Please note that 'Return' key must be pressed after the domain is added to the list, simply closing the dialogue without that would result in discarding the changes.

Example

The following example demonstrates how to use a value from a REST endpoint to modify a text object in a scene. To use the Networking class you must add the Networking capability to the project manifest. You also need to add jsonplaceholder.typicode.com to the domain whitelist in order to make the example below work.

const Reactive = require('Reactive');
const Networking = require('Networking');
const Diagnostics = require('Diagnostics');

const url = 'https://jsonplaceholder.typicode.com/users/1';

Networking.fetch(url).then(function(result) {
  Diagnostics.log(result);
  // Log result: {"status":200}
  if ((result.status >= 200) && (result.status < 300)) {
    return result.json();
  }
  throw new Error("HTTP status code " + result.status);
}).then(function(json) {
  Diagnostics.log(json);
  // Log result: {"id":1,"name":"Leanne Graham","username":"Bret","email":"Sincere@april.biz",...
  Diagnostics.log(json.address.geo.lat);
  // Log result: -37.3159
}).catch(function(error) {
  Diagnostics.log("There was an issue with fetch operation: " + error.message);
});