Author Topic: RPC/JSON API  (Read 4309 times)

Offline dreznik

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
RPC/JSON API
« on: December 11, 2013, 09:40:48 am »
Hi, is it possible to use the rpc/json api from a standalone application, not a plugin?  Basically I would like to develop a custom UI similar to what could be done with RAIDF json api.  If it is, how would i know the hostId, raidId, etc?
Thanks.

Offline Brahim

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8,547
  • Karma: +204/-16
    • View Profile
Re: RPC/JSON API
« Reply #1 on: December 11, 2013, 11:56:48 am »
Yes, it can be used standalone.

All you need is a JSON RPC client for your programming language: http://json-rpc.org/wiki/implementations

Every can be known by querying the system. Check the API reference.
Obviously, you need to know target Web UI installation URL. From there, you can query for managed hosts, which will give you the host IDs.
You can drill from there.

What's your programming language?
« Last Edit: December 11, 2013, 12:01:32 pm by Brahim »

Offline dreznik

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: RPC/JSON API
« Reply #2 on: December 11, 2013, 05:46:31 pm »
Brahim, thanks for a prompt reply.
I'll be using C# with Newtosoft Json.Net library.
My plan is to build a monitoring app to feed the LCD SysInfo display built into a computer case (http://coldtearselectronics.wikispaces.com)

Offline Brahim

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8,547
  • Karma: +204/-16
    • View Profile
Re: RPC/JSON API
« Reply #3 on: December 11, 2013, 06:52:27 pm »
Nice. :)
I can certainly see users here willing to pay for such a plugin. So, do share with the community once you have something going.

Offline dreznik

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: RPC/JSON API
« Reply #4 on: December 11, 2013, 08:20:57 pm »
I have a loooong way to go.

When I make a request, even the service.login, I get the http 500 error and and a status saying "Login exception: unauthenticated user!".
What's the authentication mechanism for code running outside of the plugin framework?

Thanks.

Offline Brahim

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8,547
  • Karma: +204/-16
    • View Profile
Re: RPC/JSON API
« Reply #5 on: December 11, 2013, 09:32:37 pm »
1. Are you on the latest release?
2. Are you passing the correct login info?

Make sure that your RPC parameters are correct.

Offline dreznik

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: RPC/JSON API
« Reply #6 on: December 11, 2013, 11:30:01 pm »
I've downloaded the latest version.  The Web UI says "Enterprise Host Manager v1.0.0 2013.12.05"

Here's the JSON payload - I havn't changed the default username/password:
{method:"service.login", params: ["admin","admin"], id:"0"}

My test code:
            var http = (HttpWebRequest)WebRequest.Create("http://nas2013:8095/json/rpc");
            http.ContentType = "text/json";
            http.Method = "POST";
           
            using (var streamWriter = new StreamWriter(http.GetRequestStream()))
            {
                string json = "{method:\"service.login\", params: [\"admin\",\"admin\"], id:\"0\"}";               
                streamWriter.Write(json);
            }
            var httpResponse = (HttpWebResponse)http.GetResponse();

I would appriciate if anybody could post a working C#/Java/C++ code.

Thanks.

Offline Brahim

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8,547
  • Karma: +204/-16
    • View Profile
Re: RPC/JSON API
« Reply #7 on: December 12, 2013, 08:11:59 am »
Okay, this is my fault.  :-[
Looks like the current build has a bug that I fixed after the release.
I have a security filter applied on the wrong URL in the current release. I did not catch that early because the plugin pages inherit the parent UI's authentication. So, they work regardless.

Here is what you can try until the next build (2-steps login). This will only work if your RPC client can maintain session cookies (http headers).
The intent is for this to work regardless of whether the client can maintain session cookies, but the bug is breaking that. So, this is just a temporary workaround till the next build.

1. Log in using the secured URL (note: srpc and 3 paramaters - also result payload is Boolean instead of String):
Code: [Select]
String host = "http://localhost:8080/json/srpc";
ServiceProxy proxy = new ServiceProxy(host, "service");
Object result = proxy.call("login", "admin", "admin", true);
ObjectMapper mapper = new ObjectMapper();
ActionStatus<Boolean> status = mapper.convertValue(result, ActionStatus.class);


2. Call the normal RPC login (note: rpc and 2 parameters - result payload is String as documented in the API):
Code: [Select]
String host = "http://localhost:8080/json/rpc";
ServiceProxy proxy = new ServiceProxy(host, "service");
Object result = proxy.call("login", "admin", "admin");
ObjectMapper mapper = new ObjectMapper();
ActionStatus<String> status = mapper.convertValue(result, ActionStatus.class);

This will be fixed in the next build.