Last week, we posted about the View And Data API and showed how to upload an assembly from Vault via powerJobs and PowerShell. This week, I’d like to add some more background. For this purpose I’ve created a stand-alone script that uploads a little sample assembly.
In order to get started, you need your own access keys. It’s free, so go on https://developer.autodesk.com and register for a free account. Under “My Apps” you can create a new app. Just give it a name and select the View and Data API. Once you have your app registered, you will receive a Consumer Key and Consumer Secret. These two strings are necessary for the login. In the script, at the top, you’ll find the two variables where you can enter your keys.
The following script will take an assembly from the Inventor sample folder and upload it to the cloud. As this is pure PowerShell, you may create your own batch scripts for uploading the files. Here is the complete script:
cls $ConsumerKey="<youConsumerKeyHere>" $ConsumerSecret="<ourConsumerSecretHere>" $samplePath = "C:\Users\Public\Documents\Autodesk\Inventor 2015\Samples\Models\Assemblies\Tuner" #login $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 #create bucket $bucketName = "tuner" #must be lower case and/or numbers $policy = "transient" #transient $json = '{"bucketKey":"'+$bucketName+'","policy":"'+$policy+'"}' $bucket = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth} #upload assembly $assembly = Get-Item "$samplePath\Tuner.iam" $uploadedFiles = @() $uploadedFiles += Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets/$bucketName/objects/$($assembly.Name.ToLower())" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$assembly.Length;"Expect"=""} -InFile $assembly.FullName.ToLower() $json = '{ "master" : "urn:adsk.objects:os.object:'+$bucketName+'/'+$assembly.Name.ToLower()+'", "dependencies" : [' #upload children $files = Get-ChildItem -LiteralPath "$samplePath\Tuner Components" -File foreach ($file in $files) { echo "uploading $($file.Name) ..." $uploadedFiles += Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets/$bucketName/objects/$($file.Name)" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$file.Length;"Expect"=""} -InFile $file.FullName $json += '{ "file" : "urn:adsk.objects:os.object:'+$bucketName+'/'+$file.Name+'", "metadata" : { "childPath" : "'+$file.Name+'", "parentPath" : "'+$assembly.Name+'" } },' } $json = $json.Substring(0,$json.Length-1) $json += ']}' #set references Invoke-RestMethod -Uri "https://developer.api.autodesk.com/references/v1/setreference" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth} #register view service foreach ($uploadedFile in $uploadedFiles) { $base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($uploadedFile.objects[0].id)) $json = '{"urn":"'+$base64+'"}' $result = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/viewingservice/v1/register" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth} echo "$($uploadedFile.objects[0].key) = $($result.Result)" } #get status foreach ($uploadedFile in $uploadedFiles) { $base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($uploadedFile.objects[0].id)) $status.progress = "" while ($status.progress -ne "complete") { $status = Invoke-RestMethod -Uri "https://developer.api.autodesk.com/viewingservice/v1/$base64/status" -ContentType "application/json" -Method Get -Headers @{"Authorization"=$auth} Start-Sleep -Seconds 1 } echo "$($uploadedFile.objects[0].key) = $($status.progress)" } echo "Access token: $($login.access_token)" $file = $uploadedFiles[1].objects[0] $base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($file.id)) echo "$($file.key) urn: $base64"
As mentioned in my previous post, you’ll need PowerShell 3.0 (default on Windows 8), as it provides the Invoke-RestMethod.
The first step is to login into the view and data API backend. We do this with this command-let
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"
Then we create a folder (bucket) where we can upload all our files, like this
Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
Unfortunately, there is no way (yet) to check whether the bucket is already created or not, so the first time we execute the command above, everything should be fine, but the next time the commandlets returns an error
Invoke-RestMethod : {"reason":"Bucket already exists"}
The script will continue anyway, but the error message is annoying. Hopefully there will be a call for testing a bucket existence or retrieve the list of buckets soon.
Then, we upload the assembly
Invoke-RestMethod -Uri "https://developer.api.autodesk.com/oss/v1/buckets/$bucketName/objects/$($assembly.Name.ToLower())" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$assembly.Length;"Expect"=""} -InFile $assembly.FullName.ToLower()
We will repeat this operation for all the children as well. Once the upload is completed, we will set the references between the parts. The according json string has been created while uploading the files.
Invoke-RestMethod -Uri "https://developer.api.autodesk.com/references/v1/setreference" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth} </pre> So, now that all the data is in the cloud and the references are set, we can start the transformation of our files into to a web suitable format. We do this with the registration service, for each file, like this: <pre> Invoke-RestMethod -Uri "https://developer.api.autodesk.com/viewingservice/v1/register" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
At the end, we will check the status of the transformation. This is not mandatory, but as some file may take a bit longer, this way, we can wait until everything is done.
Invoke-RestMethod -Uri "https://developer.api.autodesk.com/viewingservice/v1/$base64/status" -ContentType "application/json" -Method Get -Headers @{"Authorization"=$auth}
The last two lines of the script just prints out the token and the assembly URN that might be helpful in case you like to use them in your HTML code for showing the assembly.
There are more API calls, but i hope that with this simple sample, you have something that works and might be useful, and you have a feel of how to use PowerShell for consuming the View And Data API.