C# Application Example

This shows one example of API application.

To use it, type in the command row:

# DMKExampleApi.exe URL USERNAME PUBLIC_KEY PRIVATE_KEY COMMAND

Where:

  • URL = URL of the platform that you wish to connect to
  • PRIVATE_KEY = The private key
  • PUBLIC_KEY = The public key
  • COMMAND = The examples command that should be followed, such as:
    • leads: Show 1 Lead, starting from the 100th lead
    • fields: Show the list of fields
    • leads_save: Save a generic lead

Use:

# DMKExampleApi.exe –help

To see an application message.

using System;
using System.Net;
using System.Linq;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace DMKApiExample
{
    class DMKApi
    {
        string baseUrl;
        string username;
        string publicKey;
        string privateKey;

        public DMKApi(string baseUrl, string username, string publicKey, string privateKey)
        {
            this.baseUrl        = baseUrl;
            this.username       = username;
            this.publicKey      = publicKey;
            this.privateKey     = privateKey;
        }

        static void Main(string[] args)
        {
            string baseUrl      = args[0] + "/z/dmkapi/";
            string username     = args[1];         //Username
            string publicKey    = args[2];            //Public Key: Can be used for direct access
            string privateKey   = args[3];           //Private Key: Only you should know it (only client and server)
            string command      = args[4];



            DMKApi dmkApi = new DMKApi(baseUrl, username, publicKey, privateKey);

            string response;

            /* READING THE LEAD LIST */
            if (command == "leads")
            {
                Dictionary<string, object> request = new Dictionary<string, object>
                {
                    {"limit","1"},
                    {"offset", "100"}
                };

                response = dmkApi.sendRequest("leads/leads", request);

                /* Print the lead list */
                Console.Write(response);
            }
            else if (command == "fields")
            {
                /* READING THE LIST OF LEAD'S FIELDS */
                response = dmkApi.sendRequest("leads/fields/fields", new Dictionary<string, object> { });

                //Pring the list of the fields
                Console.Write(response);
            }
            else if (command == "leads_save")
            {

                Dictionary<string, object> request = new Dictionary<string, object>
                {
                    {"lead",new
                        {
                            owner_id = "1",
                            firstname = "Enrico",
                            lastname = "Venezia",
                            email = "enrico.venezia@sefin.it"
                        }},

                };

                /* CREATE A NEW LEAD */
                response = dmkApi.sendRequest("leads/save", request);

                //Print the response
                Console.Write(response);
            }

            if (args[1] == "--help")
            {
                Console.Write("URL USERNAME PUBLIC_KEY PRIVATE_KEY COMMANDn");
                Console.Write("COMMANDS ARE: n");
                Console.Write("tleads: List of leadsn");
                Console.Write("tfields: List of fieldsn");
                Console.Write("tleads_save: Save/Update a leadn");
            }

            Console.ReadLine();
        }

        /*
        * Send a request
        */
        private string sendRequest(string action, Dictionary<string, object> other_data)
        {
            string realToken = this.getToken();

            Dictionary < string, object> request = new Dictionary<string, object>
            {
                {"token", realToken},
            };

            other_data.ToList().ForEach(x => request.Add(x.Key, x.Value));

            string response = postCryptedRequest(action, request);

            return response;
        }

        /*
        * Request a new Token code
        */
        private string getToken()
        {
            string realToken = this.postCryptedRequest("get-token", new Dictionary<string, object> { });
            return realToken;
        }

        /*
        * Send data using POST, but encrypt data using
        * AES-256, with private key
        */
        private string postCryptedRequest(string url, Dictionary<string, object> fields)
        {
            string jsonData = new JavaScriptSerializer().Serialize(fields);
            string data = this.Encrypt(jsonData);

            using (WebClient client = new WebClient())
            {
                byte[] response = client.UploadValues(this.baseUrl + url, new NameValueCollection(){
                       { "username", username},
                       { "key", publicKey},
                       { "data", Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(data))}
                   });

                return this.Decrypt(response, privateKey);
            }
        }

        /*
        * Encrypt string using AES-256
        */
        private string Encrypt(string text)
        {
            RijndaelManaged AesEncryption = new RijndaelManaged();

            AesEncryption.KeySize = 256;
            AesEncryption.BlockSize = 128;
            AesEncryption.Mode = CipherMode.CBC;
            AesEncryption.Padding = PaddingMode.PKCS7;
            AesEncryption.Key = ASCIIEncoding.UTF8.GetBytes(this.privateKey);
            AesEncryption.IV = ASCIIEncoding.UTF8.GetBytes("0000000000000000");

            byte[] plainText = ASCIIEncoding.UTF8.GetBytes(text);

            ICryptoTransform crypto = AesEncryption.CreateEncryptor();
            byte[] resultBytes = crypto.TransformFinalBlock(plainText, 0, plainText.Length);

            return Convert.ToBase64String(resultBytes);

        }

        /*
        * Encrypt a string using AES-256
        */
        private string Decrypt(byte[] text, string privateKey)
        {
            RijndaelManaged AesEncryption = new RijndaelManaged();

            AesEncryption.KeySize = 256;
            AesEncryption.BlockSize = 128;
            AesEncryption.Mode = CipherMode.CBC;
            AesEncryption.Padding = PaddingMode.PKCS7;
            AesEncryption.Key = ASCIIEncoding.UTF8.GetBytes(this.privateKey);
            AesEncryption.IV = ASCIIEncoding.UTF8.GetBytes("0000000000000000");

            ICryptoTransform decrypto = AesEncryption.CreateDecryptor();

            byte[] decodeTextBase64 = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(text));
            byte[] resultBytes = decrypto.TransformFinalBlock(decodeTextBase64, 0, decodeTextBase64.Length);

            return System.Text.Encoding.UTF8.GetString(resultBytes);
        }

    }
}