Skip to content

Latest commit

 

History

History
385 lines (269 loc) · 14.2 KB

README.md

File metadata and controls

385 lines (269 loc) · 14.2 KB

ftrack JavaScript API Client

The ftrack JavaScript API Client is a JavaScript Library to help developing integrations that communicate with the ftrack API and Event server.

This documentation focuses on the client. More information about the API and its concepts can be found at our general API documentation. You may also find it useful to look at the documentation for the Python client.

Installation

npm:

npm install @ftrack/api

yarn:

yarn add @ftrack/api

TypeScript: Generated Schema Types

You can generate schema types for your own workspace with @ftrack/ts-schema-generator.

Once generated, you can integrate them with @ftrack/api by passing them as a type variable:

import { EntityTypeMap } from "./__generated__/schema.ts";
import { Session } from "@ftrack/api";

const session = new Session<EntityTypeMap>(...);

// user will now be of type User
// and provide all available properties for its entity type.
const user = await session.query<"User">("...");

Tutorial

The API uses sessions to manage communication with an ftrack server. Create a session that connects to your ftrack server (changing the passed values as appropriate):

const session = new ftrack.Session(
  "https://my-company.ftrackapp.com",
  "john.doe@example.com",
  "7545344e-a653-11e1-a82c-f22c11dd25eq",
);

await session.initializing;

console.info("API session initialized successfully.");

If everything works as expected, you should see the console message appear in the JavaScript console. If not, double check that the credentials you specified are correct.

The communication with the ftrack server in the JavaScript API is asynchronous, often returning Promises. When the session is constructed, the instance is returned immediately, while the API is being initialized in the background. Once the API has been initialized, the session.initializing promise will be resolved.

Query projects

Now, let’s start using the API with an example. Let’s list the names of all projects.

const response = await session.query("select name from Project");

const projects = response.data;
console.info("Listing " + projects.length + " projects");

console.log(projects.map((project) => project.name));

Each project returned will be a plain JavaScript object and contain the selected attributes.

The session contains a few other methods besides query(), such as create(), update() and delete(). Next up, let’s take a look at combining the query call with an update operation.

In the example below a specific project is retrieved, and then its status is set to hidden, hiding the project from the UI.

const projectName = "my_project";
const response = await session.query(
  "select id from Project where name is " + projectName,
);
const projectId = response.data[0].id;
const response = await session.update("Project", [projectId], {
  status: "hidden",
});

console.info("Project hidden", response);

Uploading files

Files are stored as components in ftrack. Here is an example on how to create a component from a file in ftrack and upload it to the ftrack.server location.

const data = { foo: "bar" };
const file = new File([JSON.stringify(data)], "data.json");

const response = await session.createComponent(file);
const component = response[0].data;
console.log(component.id);

Handling Events

The EventHub is a class that provides functionality to interact with an ftrack event server. It allows you to connect to the event server, subscribe to specific events, publish events, and handle event responses.

Events are generated in ftrack when things happen such as a task being updated or a new version being published. Each Session can optionally connect to the event server and can be used to subscribe to specific events and perform an action as a result. That action could be updating another related entity based on a status change or generating folders when a new shot is created for example.

The EventHub for each Session is accessible via session.eventHub.

Connecting to event hub

To connect to the event hub, run session.eventHub.connect(). You can also automatically connect the event hub when it is instantiated by providing the option autoConnectEventHub when constructing the Session instance:

session = new ftrack.Session(..., { autoConnectEventHub: true });
session.eventHub.isConnected();

Subscribing to events

To listen to events, you register a function against a subscription using ’Session.eventHub.subscribe()’. The subscription uses the expression syntax and will filter against each Event instance to determine if the registered function should receive that event. If the subscription matches, the registered function will be called with the Event instance as its sole argument. The Event instance is a mapping like structure and can be used like a normal object.

The following example subscribes a function to receive all ‘ftrack.update’ events and then print out the entities that were updated:

import { Session } from "@ftrack/api";

// Define a function to handle the received event
function myCallback(event) {
  // Iterate through the updated entities and print their data
  event.data.entities.forEach((entity) => {
    // Print data for the entity
    console.log(entity);
  });
}

// Create a session and automatically connect to the event hub
const session = new Session({ autoConnectEventHub: true });

// Subscribe to events with the 'ftrack.update' topic
session.eventHub.subscribe("topic=ftrack.update", myCallback);

Subscriber information

When subscribing, you can also specify additional information about your subscriber. This contextual information can be useful when routing events, particularly when targeting events. By default, the event hub will set some default information, but it can be useful to enhance this. To do so, simply pass in subscriber as a object of data to the subscribe() method:

session.eventHub.subscribe("topic=ftrack.update", myCallback, {
  id: "my-unique-subscriber-id",
  applicationId: "maya",
});

Sending replies

When handling an event it is sometimes useful to be able to send information back to the source of the event. For example, ftrack.location.request-resolve would expect a resolved path to be sent back.

You can publish a custom reply event using publishReply(), but an easier way is to return the appropriate data from your handler. Any returned value except null or undefined will be automatically sent as a reply:

function onEvent(event) {
  // Send following data in automatic reply
  return { success: true, message: "Cool!" };
}

session.eventHub.subscribe("topic=test-reply", onEvent);

Publishing events

So far we have looked at listening to events coming from ftrack. However, you are also free to publish your own events (or even publish relevant ftrack events).

To do this, simply construct an instance of Event and pass it to EventHub.publish() via the session:

import { Event } from "@ftrack/api";

event = new Event((topic = "my-company.some-topic"), (data = { key: "value" }));
session.eventHub.publish(event);

The event hub will automatically add some information to your event before it gets published, including the source of the event. By default the event source is just the event hub, but you can customise this to provide more relevant information if you want.

Handling replies

When publishing an event, you can specify onReply as a function which will be invoked whenever a reply event is received:

function onReply(event) {
  console.info("Reply received", event.data);
}
session.eventHub.publish(event, { onReply: onReply });

It is often the case that you want to wait for a single reply. In this case, you can use the convenience method publishAndWaitForReply(). It will return a promise which will be resolved with the response. You can test this using two browser tabs. In the first, run the following to listen for event and reply:

// Listen for events and reply
function onEvent(event) {
  console.info("Event received", event.data);
  return { message: "Event acknowledged" };
}
session.eventHub.subscribe("topic=my-company.some-topic", onEvent);

In the second environment we will publish an event, wait for and log the response:

// Publish event and wait for reply
function onReply(event) {
  console.info("Promise resolved with reply", event.data);
}
function onError(error) {
  console.error("Reply not received", error);
}
var event = new ftrack.Event("my-company.some-topic", {
  message: "Hello world!",
});
session.eventHub
  .publishAndWaitForReply(event, { timeout: 5 })
  .then(onReply, onError);

Limitations

The event hub in the JavaScript API has some minor differences and lacks some of the features available in the python counterpart.

Subscription expressions

The JavaScript API currently only support expressions on the format topic=value including wildcard support topic=ftrack.*, and more complex expressions such as filtering based on event source or data are not supported.

Target expression

Targeted events will invoke all subscribers of the topic, not just those matching the target expression.

Stopping events

Subscription callback priorities and the ability to stop events is not supported at this point.

Methods

connect()

Connects to the event server.

session.eventHub.connect();

isConnected(): boolean

Checks if the EventHub is connected to the event server.

const isConnected = session.eventHub.isConnected(); // Returns true if connected, false otherwise

publish(event: Event, options?: { onReply?: EventCallback, timeout?: number }): Promise<string>

Publishes an event to the event server and returns a promise resolved with the event ID when the event is sent.

  • event (Event): An instance of the Event class to publish.
  • options.onReply (EventCallback, optional): A function to be invoked when a reply is received.
  • options.timeout (number, optional): Timeout in seconds (default is 30 seconds).

Example:

const event = new Event(/* ... */);
session.eventHub.publish(event, {
  onReply: (reply) => {
    // Handle the reply event
  },
  timeout: 60,
});

publishAndWaitForReply(event: Event, options: { timeout: number }): Promise<unknown>

Publishes an event and waits for a single reply event.

  • event (Event): An instance of the Event class to publish.
  • options.timeout (number): Timeout in seconds for waiting for a reply event.

Returns a promise resolved with the reply event payload when received or rejects if no reply event is received within the specified timeout.

Example:

const event = new EventEvent(
  (topic = "my-company.some-topic"),
  (data = { key: "value" }),
);
session.eventHub
  .publishAndWaitForReply(event, { timeout: 60 })
  .then((replyEvent) => {
    // Handle the reply event
  });

subscribe(subscription: string, callback: EventCallback, metadata?: SubscriberMetadata): string

Subscribes to events matching a specified subscription expression.

  • subscription (string): The subscription expression in the format "topic=value" or with wildcards like "topic=ftrack.*".
  • callback (EventCallback): A function to be called when an event matching the subscription is received. Callbacks can either be synchronous or asynchronous function.
  • metadata (SubscriberMetadata, optional): Optional metadata about the subscriber.

Returns a subscriber ID, which can be used to unsubscribe from events later.

Example:

const subscriberId = session.eventHub.subscribe(
  "topic=ftrack.action.launch",
  (eventPayload) => {
    // Handle the received event
  },
);

unsubscribe(identifier: string): boolean

Unsubscribes from events based on a subscriber ID returned from the subscribe method.

  • identifier (string): The subscriber ID to unsubscribe.

Returns true if a subscriber was removed, false otherwise.

Example:

const subscriberId = session.eventHub.subscribe(/* ... */);
const unsubscribed = session.eventHub.unsubscribe(subscriberId);
if (unsubscribed) {
  console.log(`Unsubscribed subscriber with ID: ${subscriberId}`);
} else {
  console.log(`Subscriber with ID ${subscriberId} not found.`);
}

getSubscriberByIdentifier(identifier: string): Subscriber | null

Returns information about a subscriber based on its identifier.

  • identifier (string): The subscriber identifier.

Returns a Subscriber object if found, or null if no subscriber with the specified identifier exists.

Example:

const subscriberId = session.eventHub.subscribe(/* ... */);
const subscriber = session.eventHub.getSubscriberByIdentifier(subscriberId);
if (subscriber) {
  console.log("Subscriber information:", subscriber);
} else {
  console.log(`Subscriber with ID ${subscriberId} not found.`);
}

Further documentation

If there's any part of the client that you would like us to expand the documentation for, please add a GitHub issue and we'll try to get to it as soon as possible. Otherwise more information about the API and its concepts can be found at our general API documentation. You may also find it useful to look at the documentation for the Python client.