Skip to content

Instantly share code, notes, and snippets.

@kylenstone
Created September 22, 2020 17:56
Show Gist options
  • Save kylenstone/fa914d52f163da94949f79c2f8664849 to your computer and use it in GitHub Desktop.
Save kylenstone/fa914d52f163da94949f79c2f8664849 to your computer and use it in GitHub Desktop.
Sample code to automatically create Frame.io Projects and Folders
// Note: this sample code is in the process of being re-written
// and assumes a client SDK is available to wrap API requests
// such as createFolder(). The intent is simply to provide
// convenience methods, but you can also write your own API requests
// as shown in this example: https://github.com/Frameio/custom-actions-example-app/blob/ks/DEVREL-589/examples/custom-action-offload-to-s3/modules/api.js
const apiClient = require('./api/client.js')
const template = require('./templates/onlineTemplate.json')
function createProject(name, teamId) {
const path = `/teams/${teamId}/projects`
const body = {
name: name,
projectPreferences
}
const resp = apiClient.post(path, body)
return resp.json()
}
function createFolder(name, parentId) {
const path = `/assets/${parentId}/children`
const body = {
type: "folder",
name: name,
}
const resp = apiClient.post(path, body)
return resp.json()
}
const runTemplater = function(template) {
// This example uses preorder traversal to create each folder then
// traverse subfolders before moving to next branch of the tree
const itemsToCreate = template
while (itemsToCreate.length) {
let item = itemsToCreate.shift()
try {
if (item['type'] === 'project') {
//TODO: set a variable root_asset_id to pull the project ID
//from the JSON response. This variable will be used to specify the 'parent' for each subfolder to follow.
//See for reference: https://docs.frame.io/docs/root-asset-ids
root_asset_id = apiClient.createProject(teamId)
}
else if(item['type'] === 'folder') {
//TODO: Similar to project creation, handle the JSON response
//and store the parent folder ID as the parentId,
//in case there is another level of sub-folders to traverse.
parentId = apiClient.createFolder(item['name'], parentId)
if(item['subfolders']) itemsToCreate.unshift(...item.subfolders)
}
} catch (err) {
console.log(error);
}
}
}
// Store the team ID in an environment variable.
teamId = env.process.teamId
// This sample code makes an assumption that the API Token required to
// Authenticate to the Frame.io API is stored in an environment variable
// and accessible to the client SDK (TBD for Node.js but exists for other languages)
runTemplater(onlineFolderTemplate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment