Archi Forum

Archi Plug-ins => jArchi => Topic started by: rfamans on October 09, 2018, 19:44:23 PM

Title: Jarchi - call rest API
Post by: rfamans on October 09, 2018, 19:44:23 PM
It seems that running javascript from Archi is a bit limited compared to running js from a browser. E.g. in Jarchi the XMLHttpClient library is not available which makes it kind of hard to call REST API's from jarchi script. 

Any ideas on how to call rest APIs from jarchi via other ways than popular js libraries based on XMLHttpClient are doing (e.g. Fetch or Axios)? It would be awesome if jarchi would allow calling REST APIs. I have got the feeling that achieving this is well within reach if a tiny missing piece of the puzzle can be found.
Title: Re: Jarchi - call rest API
Post by: Phil Beauvoir on October 09, 2018, 19:51:40 PM
The JS engine uses Nashorn which doesn't support as much as full JS. For example - https://blog.codecentric.de/en/2014/06/project-nashorn-javascript-jvm-polyglott/

"Nashorn is not the same as Node.js nor is it the same as the JavaScript environment in the browser. Nashorn is only a JavaScript engine, i.e. an implementation of the ECMAScript 5.1 language specification plus memory management. This means that global JavaScript functions such as setTimeout, setInterval or XMLHttpRequest do not exist in Nashorn. Neither does it have an event loop or a task queue. This means that many JavaScript libraries cannot be used with Nashorn because such libraries commonly expect a browser-like environment. "
Title: Re: Jarchi - call rest API
Post by: Jean-Baptiste Sarrodie on October 09, 2018, 20:07:06 PM
Hi,

In fact Nashorn implement JavaScript itself, but things like XMLHttpRequest are not part of JS itself but offered as "Add ons" by browser or Node.JS.

But fortunately, some people have developped polyfills (https://www.npmjs.com/package/nashorn-polyfill) for Nashorn and some ad-hoc libs (https://gist.github.com/billybong/a462152889b6616deb02) using underlying JVM.

Regards,

JB
Title: Re: Jarchi - call rest API
Post by: rfamans on October 09, 2018, 20:25:59 PM
Thanks JB!

I got it to work with: https://gist.github.com/billybong/a462152889b6616deb02 (https://gist.github.com/billybong/a462152889b6616deb02)

This script does not seem to work with https but for now this is not a problem for me. Maybe there are some other Nashorn examples available that also work with https.

Regards
Roy
Title: Re: Jarchi - call rest API
Post by: Jean-Baptiste Sarrodie on October 09, 2018, 21:38:05 PM
Hi,

Quote from: rfamans on October 09, 2018, 20:25:59 PM
This script does not seem to work with https but for now this is not a problem for me.

I haven't tested it but it should work. Are you targeting a self signed HTTPS server? If yes this might explain the issue.

Quote from: rfamans on October 09, 2018, 20:25:59 PM
Maybe there are some other Nashorn examples available that also work with https.

If you find one please let me know as I might need it in the future.

Regards,

JB
Title: Re: Jarchi - call rest API
Post by: rfamans on October 10, 2018, 08:11:39 AM
Hi JB,

For now, https is not required. If https is to be used, it will be done with proper certificates.
I will keep you posted.

//Roy
Title: Re: Jarchi - call rest API
Post by: rheward on November 05, 2019, 14:35:27 PM
Did anyone find a version of this that worked with https?
Title: Re: Jarchi - call rest API
Post by: Alberto on January 30, 2020, 16:19:10 PM
I too am wondering about https
Title: Re: Jarchi - call rest API
Post by: e.istomin on February 04, 2020, 22:28:33 PM
Before i moved to JRuby, i used java.net.URL directly from JS:

function getStreamFromURL(url) {
    with (new JavaImporter(java.io, java.net.URL)) {
        var is = new URL(url).openStream();
        try {
            var reader = new BufferedReader(
                new InputStreamReader(is));
            var buf = '', line = null;
            while ((line = reader.readLine()) != null) {
                buf += line;
            }
        } finally {
            reader.close();
        }
        return buf;
    }
}
....
var data = JSON.parse(getStreamFromURL(Url));