Vault, View And Data API via powerJobs

ViewAndData

Wouldn’t it be cool to access your 3D models from the web? Autodesk came out with a new technology called View And Data API, which allows displaying several file formats directly in your web-browser without the need of any plugin. Here’s a sample.

I know, as soon the word “cloud” is spoken, some of you are getting skin irritations, but sooner or later, accessing data in a meaningful way will be mandatory.

With this post, I’d like to show you how the View And Data API can be consumed via PowerShell, and to be more precise, via powerJobs. The following job will take an assembly from Vault and upload the files to the cloud services so that it can be viewed. The steps are more or less simple: login, create a folder (bucket), upload the files, create the references between the files, and start the translation.

Now, the entire world is moving toward REST API, which is the de-facto API language used in the web/cloud. Microsoft added a handy command-let with PowerShell 3.0. The command-let is called Invoke-RestMethod.

And here is the job

Add-Log -Text ">> starting cloud upload job"
$ConsumerKey="your consumer key"
$ConsumerSecret="your consumer secret"
$bucketName = "coolorange" #must be lower case and/or numbers
$policy = "transient" #transient

$file = PrepareEnvironmentForFile "Suspension.iam"

Add-Log -Text "Log in to ViewAndData..."
$login = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/authentication/v1/authenticate" -ContentType "application/x-www-form-urlencoded" -Method Post -Body "client_id=$ConsumerKey&client_secret=$ConsumerSecret&grant_type=client_credentials"
$auth = $login.token_type + " " + $login.access_token
$json = '{"bucketKey":"'+$bucketName+'","policy":"'+$policy+'"}'
Add-Log -Text "Creating bucket $bucketName ..."
try{
$bucket = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
}
catch{}

Add-Log -Text "Downloading file $($file.Name)"
$localTempPath = "c:\Temp\webview"
$assembly = Get-VaultFile -File $file.'Full Path' -DownloadPath $localTempPath
Get-ChildItem $localTempPath -Recurse -File | ForEach-Object { $_.IsReadOnly = $false }

$json = '{ "master" : "urn:adsk.objects:os.object:coolorange/'+$assembly.Name+'", "dependencies" : ['

$localFullPath = $assembly.'Full Path'
Add-Log -Text "-$localFullPath"
$localFullPath = $localFullPath.Replace("$",$localTempPath).Replace("/","\")
Add-Log -Text "uploading $localFullPath ..."
$filesInWeb =  @()
$fileInWeb = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets/$bucketName/objects/$($assembly.Name)" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$assembly.'File Size';"Expect"=""} -InFile $localFullPath
$filesInWeb += $fileInWeb

$children = Get-VaultFileAssociations -File "$/Designs/Suspension/Suspension.iam" -Dependencies
foreach ($child in $children) {
  $json += '{ "file" : "urn:adsk.objects:os.object:coolorange/'+$child.Name+'", "metadata" : { "childPath" : "'+$child.Name+'", "parentPath" : "'+$assembly.Name+'" } },'
  $localFullPath = $child.'Full Path'
  $localFullPath = $localFullPath.Replace("$",$localTempPath).Replace("/","\")
  Add-Log -Text "uploading $localFullPath ..."
  $fileInWeb = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets/$bucketName/objects/$($child.Name)" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$child.'File Size';"Expect"=""} -InFile $localFullPath
  $filesInWeb += $fileInWeb
}
$json = $json.Substring(0,$json.Length-1)
$json += ']}'
Add-Log -Text "Set the references"
$references = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/references/v1/setreference" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}

$services = @()
foreach($fileInWeb in $filesInWeb)
{
  $fileName = $fileInWeb.objects[0].key
  Add-Log -Text "Start conversion for $fileName"
  $fileName = [System.Web.HttpUtility]::UrlEncode($fileName)
  $urn = "urn:adsk.objects:os.object:$bucketName/$fileName"
  $base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($urn))
  $json = '{"urn":"'+$base64+'"}'
  $service = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/viewingservice/v1/register" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
  $services += $service
}

Add-Log -Text "<< Job completed!"

You can just copy paste it into a PS1 file within the C:\ProgramData\coolOrange\powerJobs\Jobs folder. Then go to an assembly in Vault and queue this job via powerJobs.

For the sake of simplicity, this job just takes care about one level of children. So, for multilevel assemblies, the job would have to be executed recursively for each level.

The key thing here is that you can see how to consume the View And Data API within PowerShell, and so create your own scripts, and how to combine this with Vault.

In order to see the uploaded files you need your own little web page, which is not part of this post. But if you like to know more about this technique, you can follow the quick start guide. If you like to get this job running on your side, you need to create your own account and place the consumer key and secret at the top of this script.

We love this stuff and will play more and talk more about this in the next months, so, in case you like this too, share your ideas with us!

 

This entry was posted in powerJobs, PowerShell, ViewAndDataAPI. Bookmark the permalink.

1 Response to Vault, View And Data API via powerJobs

  1. Pingback: View And Data API with PowerShell | coolorange

Leave a comment