Fusion Manage Forum
Welcome to Autodesk’s Fusion Manage (formerly Fusion 360 Manage) Forum. Share your knowledge, ask questions, and explore popular Fusion Manage topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

REST API and scripting

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
1647 Views, 7 Replies

REST API and scripting

I was wondering, is it possible to call a REST API using a script?

 

We have a linking filtered picklist showing records in workspace A, that only shows records where a checkbox is ticked. What I was hoping to do was set up an on demand script in workspace B that when ran, calls the "https://tenant.autodeskplm360.net/api/rest/v1/setups/picklists/XXXX" request, then takes all the records from it which match a further filter (say, belong to a specific project) , and add each of the findings to a field in the grid tab.

 

I've been using the "XMLHttpRequest();" object in my attempts. I'll admit that this is completely unfamiliar to me, and I'm stabbing around in the dark.

 

using http: protocol and I can get a response which says "this has been moved to https://" . When I try with https:// I get "network_err", which I'm guessing is because I need to submit authentication info along with the request.

 

So I suppose what I'm asking is : 1. Is this even possible using the standard scripting tool? 2. Assuming I can get it working for myself, will I be able to get it working for whichever user decides to run the script? 3. Has anyone got an example of how to form the authentication component of the request in a script-friendly format?

 

Cheers,

 

Stuart

 

 

 

7 REPLIES 7
Message 2 of 8
tony.mandatori
in reply to: Anonymous

Stuart, 

 

I think this is any issue with authenticated https in our JavaScript.  The octopart integration uses unauthenticated HTTP and is able to retrieve data over REST.

 

This might also be a CORS issue where the you cannot make requests to a different domain, unless that domain supports CORS:  We have a setting to enable CORS in the general settings (but I never got this to work either).

 

 

 

 

Message 3 of 8
Anonymous
in reply to: tony.mandatori

Update:

 

Got my script working. Successfully pulling data from a picklist API, querying the items for a specific string in their names, and adding them as rows in a grid if matched.

 

You can imagine my excitement when this worked. Unfortunately, this led me to the realisation that the filters set up on picklists don't have an effect on the output from API requests!! The picklist in question is simply a workspace picklist, with a filter applied on it to only show records with a checkbox ticked. This filter is ignored by the API and so I'll try to use another method (querying each record using a workspace list API instead). Instead what it shows is every record in the workspace, however with another limitation: Only the first 1000 records are shown with no indication that the output is incomplete!

 

Anyway, a few steps forward and only one back so I'm still pretty content with the progress today.

 

Here's the code I used, for anyone else interested in this. Hopefully it can be of some use to others.

 

/**
 * Name: testScriptStuart
 * Created on: 08/08/2017
 * Created By: Stuart Gray
 * Purpose: Access API requests within Action Script
 * Version: 1.0a
 **/
 
item.grid.clear(); // Clear the grid 

var project = item.THE_PROJECT_FIELD;
var login = '{"userID": "my user name", "password": "my password"}'; 

xhr = new XMLHttpRequest(); // Start request for Logging In
url = "https://[My Tenant].autodeskplm360.net/rest/auth/1/login";
xhr.open("POST", url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(login); // Send Login details with request
var loginDetails = xhr.responseText;
//println(loginDetails);
var search = /([\-\.\w])+(?=","c)/;  //RegEx to parse data correctly for submission with further requests (http://regexr.com/ for testing)
var sessionID = search.exec(loginDetails);
//println("sessionID is : "+sessionID[0]);

rhx = new XMLHttpRequest(); //Start request for retrieving required Data
lru = "https://[My Tenant].autodeskplm360.net/api/rest/v1/setups/picklists/CUSTOM_LOOKUP_WS_PICKLIST";
rhx.open("GET", lru);
rhx.setRequestHeader('Content-Type', 'application/json');
var authentication = 'Cookie: customer=[MY TENANT];JSESSIONID='+sessionID[0]; //"customer=[MY TENANT]" where [MY TENANT] is Customer Token from loginDetails.
rhx.setRequestHeader('Cookie', authentication); // Submit authorisation info inside header (as required according to REST API v1 Reference)
//println(authentication);
rhx.send(); // No data to be sent in main body, as we are requesting data, not submitting it
Obj = JSON.parse(rhx.responseText); // Set response text as a Javascript object, for easier manipulation of data
for (var i in Obj.picklist.values){ // For each found record in the picklist, do...
    var record = Obj.picklist.values[i].label; // Returns items item descriptor in string format
    if(record.indexOf(project) > -1){ // If the record name contain reference to the project then...
//        println(record);
        item.grid.addRow({RECORD:record}); // item.grid.addRow({FIELD_1:field_1, FIELD_2:field_2, FIELD_N:field_n}) Add record to grid tab, in linking picklist field "RECORD"
    }
}
Message 4 of 8
gasevsm
in reply to: Anonymous

Hi Stuart, you could try building a report that filters exactly as the picklist does and call that via API to get a subset of the items you seek to interact with. There is /reports/{id} you could call to achieve this ~ check the help file API section. ReportId is in the URL as you run it. If have issues just ping me direct. Cheers and good luck~!

Martin Gasevski | Fusion 360 Team Product Manager
Message 5 of 8
Anonymous
in reply to: gasevsm

Thanks Martin, that worked a treat!

 

Starting to realise the potential we have here now for automating certain tasks within Fusion Lifecycle.

 

Exciting!

 

 

Message 6 of 8
Anonymous
in reply to: Anonymous

Further question regarding the login.

 

I've now got a couple of API implementations inside our tenant (validating against data in another workspace for example), and so have moved the login component to a library script.

 

Just wondering how long the login authentication information remains valid, so that I could login once, the first time someone uses the scripts that day, and bypass the login part for the remainder of the day (or until the authentication info needs updated). Thinking that this might reduce our likelihood of script timeouts, as well as reducing the overall number of API requests sent to the server.

 

Currently every time the API stuff runs, it starts by logging in ( and never logs out). ~ is there a problem doing this?

 

Cheers

Message 7 of 8
gasevsm
in reply to: Anonymous

Hi,
There is no problem logging in with every API call and not logout. It's a good practice to do logout after to relieve session and resources.
However, there is equally no need to login with every API call. Rather build resiliency with the API such that you try the call passing existing session and cookies, and only if call fails with auth denied then relogin and refdresh your session info for subsequent calls. The login session will remain active indefinitely as long as you make at least one activity within 30min of last activity. Said otherwise, a session expires after 30min of inactivity.
Hope this helps,
--mg

Martin Gasevski | Fusion 360 Team Product Manager
Message 8 of 8
gasevsm
in reply to: Anonymous

Hi,
There is no problem logging in with every API call and not logout. It's a good practice to do logout after to relieve session and resources.
However, there is equally no need to login with every API call. Rather build resiliency with the API such that you try the call passing existing session and cookies, and only if call fails with auth denied then relogin and refdresh your session info for subsequent calls. The login session will remain active indefinitely as long as you make at least one activity within 30min of last activity. Said otherwise, a session expires after 30min of inactivity.
Hope this helps,
--mg

Martin Gasevski | Fusion 360 Team Product Manager

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report