Introducing the New iKnode SDK

Have you ever tried to call a Web Service from .Net or Javascript ? It is pretty simple. Right? This is true if you are only doing GET requests without security. Things start to get complicated once your start using security and doing POST requests.

There are frameworks that make it easier, but even after you reduce the complexity by adding a library to handle Web Requests, parsing adds to the problem. Thinking about these two problem we designed an SDK for iKnode Applications that makes it extremely simple to execute from your client.

First, by handling ALL Web Requests without your client having to know about the security or how to do POST requests. Second it parses the results as as native C# classes or Javascript objects.

One very important aspect of the new SDK, is that you learn it once in any language, and you learn it for the rest. Right now we are only supporting C# and Javascript, but the Android and iOS SDKs are on the way and will be handled in the same way, focused completly on consitency.

It’s time to look at some code. Let’s say that we are building a User Service which is used to manage User information in the cloud:

UserService iKnode Application
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using iKnode.Applications;
using iKnodeSdk.UnitTest.Domain;

namespace iKnodeSdkTest
{
    /// <summary>
    /// Defines the User Service.
    /// </summary>
 [Application]
  public class UserService
  {
        /// <summary>
        /// Gets the User First Name by its identifier.
        /// </summary>
        /// <param name="id">User Identifier.</param>
        /// <returns>User First Name/.</returns>
        public string GetFirstNameById(int id)
        {
            return "Robert";
        }

        /// <summary>
        /// Gets the Most Common User First Name.
        /// </summary>
        /// <returns>Most Common User First Name/.</returns>
        public string GetMostCommonFirstName()
        {
            return "John";
        }

        /// <summary>
        /// Gets the User by its identifier.
        /// </summary>
        /// <param name="id">User Identifier.</param>
        /// <returns>User Information.</returns>
        public User GetById(int id)
        {
            if(id != 1) {
                return null;
            }

            return new User(1, new FullName("John", "Doe"));
        }

        /// <summary>
        /// Saves the User Information.
        /// </summary>
        /// <param name="user">User Information.</param>
        /// <returns>User Identifier.</returns>
        public int Save(User user)
        {
            if(user == null) {
                return -1;
            }

            //TODO: Here your store the User Information.

            return user.Id;
        }

        /// <summary>
        /// Creates a new User with the Default Information.
        /// </summary>
        /// <returns>User Information.</returns>        
        public User CreateDefault()
        {
            return new User(2, new FullName("Jane", "Doe"));
        }

        /// <summary>
        /// Creates a new User.
        /// </summary>
        /// <param name="id">Identifier.</param>
        /// <param name="name">Name.</param>
        /// <returns>User Information.</returns>        
        public User Create(int id, FullName name)
        {
            return new User(id, name);
        }
  }
}

In this case we are going to have the following classes as part of the model to be shared by the service and the C# client. For the Javascript client the model is not required.

Model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace iKnodeSdkTest
{
    /// <summary>
    /// Defines the Full Name class.
    /// </summary>
    [Serializable]
    public class FullName
    {
        /// <summary>
        /// First Name.
        /// </summary>
        public string FirstName;

        /// <summary>
        /// Last Name.
        /// </summary>
        public string LastName;

        /// <summary>
        /// Initializes a new instance of the <see cref="FullName"/> class.
        /// </summary>
        /// <param name="firstName">First Name/</param>
        /// <param name="lastName">Last Name.</param>
        public FullName(string firstName, string lastName)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
        }
    }

    /// <summary>
    /// Defines the User class.
    /// </summary>
    [Serializable]
    public class User
    {
        /// <summary>
        /// Identifier.
        /// </summary>
        public int Id;

        /// <summary>
        /// Full Name.
        /// </summary>
        public FullName Name;

        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        /// <param name="id">Identifier.</param>
        /// <param name="name">Name.</param>
        public User(int id, FullName name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}

Now in the C# client side, you would call the UserService in the following way:

C# Client
1
2
3
4
5
6
7
8
9
10
11
// Creating User Id 1 with Name jdoe.
ApplicationClient userSvc = new ApplicationClient(
                        "https://api.iknode.com",
                        UserId,
                        ApiKey,
                        "UserService");

User user = userSvc.Execute<User>(
                      "Create",
                      new MethodParameter("id", 1),
                      new MethodParameter("name", "jdoe"));

Now in Javascript the same call would look like this:

Javascript Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   var userSvc = new iKnodeSdk.ApplicationClient({
      userId: USERID,
      apiKey: APIKEY,
      appName: "UserService"
  });

  var user = userSvc.execute({
      methodName: "Create",
      parameters: [{
              name: "id",
              value: 1
          },{
              name: "name",
              value: "jdoe"
          }
      ]
  });

It is as simple as that. The source code for the SDK can be found here. Each platform SDK has a set of Unit Tests that show how the SDK can be used.

If you have any questions don’t hesitate to ask questions in the forums.