@viewar/backend-api
v1.11.1
Published
Package to allow apps to change ViewAR content in the backend.
Downloads
102
Keywords
Readme
@viewar/backend-api
Package to allow apps to change ViewAR content in the backend.
Usage
Each request (read or write) will return the changed data from the server. This way the programmer can keep his own state in sync with the server state.
Configuration
First a user must configure this module to get access to the backend. This should be done before any call to this API:
import { Configuration } from '@viewar/backend-api';
Configuration.setHost('https://dev2.viewar.com');
Authentication
Second a user has to authenticate to set a valid token. The package internally saves the token during runtime.
One way is to authenticate with email and password:
import { Authentication } from '@viewar/backend-api';
const token = await Authentication.authenticate('[email protected]', '12345');
The other way is to directly use the token:
import { Authentication } from '@viewar/backend-api';
const token = 'token1234';
Authentication.useToken(token);
Storage
The storage is to read from ViewAR's cloud storage or write into it.
import { Storage } from '@viewar/backend-api';
const storageId = 'viewar.app.id';
// Write a file.
content = await Storage.write(storageId, 'test.json', {
test: 1234,
});
// Read from file.
let content = await Storage.read(storageId, 'test.json');
Resources
The resources namespace is used to handle resources on the ViewAR servers.
import { Resources } from '@viewar/backend-api';
const file = new File(['mocked file content'], 'filename.txt'); // Usually taken from UI input or from a filesystem read.
const name = 'Test Texture'; // Name of the texture.
const type = 'png'; // Either png or jpg.
// Create a new texture.
const texture = await Resources.createTexture(file, name, type);
ModelData
ModelData is to read or write to a model's data field.
import { ModelData } from '@viewar/backend-api';
const modelId = '108721';
// Read model data.
let data = await ModelData.get(modelId);
// Update model data.
// Here we use object.assign to merge the new data with the
// existing ones (to avoid overwriting converter output).
data = await ModelData.save(
modelId,
Object.assign({}, data, {
test: 2345,
})
);
Error Handling
Every backend request can throw an error. If the request fails to reach the server a RequestError is thrown. If there is an error in the server response (e.g. authentication error) a BackendError is thrown.
import { Backend, ModelData, Authentication } from "@viewar/backend-api";
const token = "invalid token";
const modelId = "108721";
Authentication.useToken(token);
try {
// Try to get model data with invalid token.
await ModelData.get(modelId);
} catch(err) {
if (err instanceOf Backend.RequestError) {
// RequestError is thrown if connection to the server can't be established.
console.log("Failed to reach the server!", err.message);
} else if (err instanceOf Backend.BackendError) {
// BackendError will occur.
console.log("Server returned an error in response", err.message);
}
} else if (err instanceOf Backend.InvalidTokenError) {
// InvalidTokenError is thrown is neither Authentication.useToken nor Authentication.authenticate was called.
console.log("Please authenticate first.", err.message);
}
}