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
Solved! Go to Solution.
Solved by gasevsm. Go to Solution.
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).
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" } }
Thanks Martin, that worked a treat!
Starting to realise the potential we have here now for automating certain tasks within Fusion Lifecycle.
Exciting!
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