2013-05-16

Using the REST API of ImageVault

In ImageVault 4 we introduced a REST API for the ImageVault Core service. This will show a small example on how to use that.

Javascript

This example uses javascript and the javascript classes that are included in the ImageVault installation (in the UI). I will use the scripts that are located on the beta installation but you can use the ones that are on your installation as well.
We need three scripts, jquery, json2 and ImageVault.Client. ImageVault.Client handles authentication and CORS logic for the app.

<script type="text/javascript" src="https://demo2.imagevault.se/ImageVault/Scripts/lib/jquery-1.6.1.js" ></script>
<script type="text/javascript" src="https://demo2.imagevault.se/ImageVault/Scripts/lib/json2.js"></script>
<script type="text/javascript" src="https://demo2.imagevault.se/ImageVault/Scripts/ImageVault.Client.js"></script>

Client

To connect to core we need the javascript client.
var core = new ImageVault.Client({
    core: "https://demo2.imagevault.se",
    username: "sdkUser",
    password: "sdkKey",
    resourceOwnerUsername: "username",
    resourceOwnerPassword: "password",
    grant_type:"password"
});
There are several methods to authenticate to core, this shows the resource owner password credential grant method. See the authentication documentation for more information.
The core client is created using the following parameters
core
This is the url to the Core server
username/password
the username and password of the sdkuser.
resourceOwnerUsername/resourceOwnerPassword
the username and password of the user.
grant_type
The authentication mechanism to use. For Resource Owner password credential grant method, the value "password" is used.

Authentication

We use federated authentication and to be able to call the services you need to have an authentication ticket. To get the ticket you need to authenticate vs the IdentityProvider (Idp). This is handled by the ImageVault.Client script. Also reissuing of tickets when they exipire are handled by the client script.

API calls to Core

To call the REST API we use the json method of the client that performs an authenticated call to the core.

core.json("MediaService/Find", {
  Filter: { SearchString: searchString },
  Populate: {
    MediaFormats: [
      //Thumbnail format
      {  
        $type: "ImageVault.Common.Data.ThumbnailFormat,ImageVault.Common", 
        Effects:[
          {$type:"ImageVault.Common.Data.Effects.ResizeEffect, ImageVault.Common",Width:200,Height:200,ResizeMode:'ScaleToFill'}
        ]
      }
    ],
    PublishIdentitifier:"http://myserver.com/"
  }
}, function (d) {
  alert(JSON.stringify(d));
});
The json call uses the following parameters
path
The service/method to call. Name of the service is the same as the interface (omit the leading I)
arguments
the arguments to the service method as a javascript object. If the method takes multiple arguments, wrap them in a single object (.e {arg1:'test',arg2:'test2'} )
success callback
The function to call when the service call is done. The argument to the function is the return value of the service method.
The services are documented at the ImageVault API reference documentation. The full ImageVault developer documentation can be found at http://imagevault.se/doc

You can test a fully fledged demo below that uses the ImageVault demo site scripts and content that performs a search and displays the thumbnail media

Demo


2 comments:

  1. Hi, does this code work anymore? I get d=null.

    ReplyDelete
    Replies
    1. Hi,
      The example is fixed now. The testserver had been moved and I forgot all about this article. Sorry.

      Delete