Jitbit Integration with .NET

Deliver awesome support to your customers. Jitbit Helpdesk ticketing system is a simple, powerful and easy to deal with. We can either host it in on-premise or cloud-host.There was a recent requuest from one of my client to come up with a simple ticketing system where business can start operating quickly and integrate it with there clinet facing .NET application.The real beauty was , the interface it expose where we can integrate with our Entraprise Apllications. I am going to share few code samples that will be helpful.

Please see some helper methods first:

        public string BuildRequest(string method, IEnumerable<KeyValuePair<string, string>> urlParameters)
        {
            var returnData = "{}";
            var querystring = new StringBuilder("?");
            if (urlParameters != null && urlParameters.Count() > 0)
            {
                foreach (var item in urlParameters)
                {
                    querystring.Append(item.Key + "=" + item.Value + "&");

                }
                querystring.Remove(querystring.Length - 1, 1);
            }
            else { querystring.Clear(); }
            return returnData;
        }
        private string ConstructHeader()
        {
            string credentials = new StringBuilder(_Username + ":" + _Password).ToString();
            return new StringBuilder("Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials))).ToString();
        }
        private HttpWebRequest ConstructRequest(string querystring = "")
        {
            HttpWebRequest ClientWebRequest = (HttpWebRequest)WebRequest.Create(_BaseAdress + querystring);
            IWebProxy proxy = ClientWebRequest.Proxy;
            if (proxy != null)
            {
                NetworkCredential cred = System.Net.CredentialCache.DefaultNetworkCredentials;
                proxy.Credentials = cred;
                WebRequest.DefaultWebProxy = proxy;
            }
            AddAuthenticationHeader(ClientWebRequest);
            ClientWebRequest.Accept = "Application/Json";
            ClientWebRequest.ContentType = "Application/Json";
            ClientWebRequest.Method = WebRequestMethods.Http.Get;
            return ClientWebRequest;
        }
        private HttpWebRequest ConstructRequest(string querystring = "")
        {
            var ClientWebRequest = (HttpWebRequest)WebRequest.Create(_BaseAdress + querystring);
            var proxy = ClientWebRequest.Proxy;
            if (proxy != null)
            {
                NetworkCredential cred = System.Net.CredentialCache.DefaultNetworkCredentials;
                proxy.Credentials = cred;
                WebRequest.DefaultWebProxy = proxy;
            }
            ClientWebRequest.Accept = "Application/Json";
            ClientWebRequest.ContentType = "Application/Json";
            ClientWebRequest.Method = WebRequestMethods.Http.Get;
            return ClientWebRequest;
        }

        private string ReadResponse(HttpWebResponse response)
        {
            string ResponseContent = null;
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                ResponseContent = reader.ReadToEndAsync().Result;
            }
            return ResponseContent;
        }

Now Lets Make some API Calls:

         public JToken GetEntitiesByEmail(string userName)
        {
            var querystring = new Dictionary<string, string> { { "email", userName } };
            try
            {
                var response = this._helpDeskService.BuildRequest("UserByEmail", querystring);
                var result = JToken.Parse(response);
                return result;
            }
            catch (Exception ex)
            {
            }
        }
        public JToken GetEntitiesByUser( string userEmail)
        {
            var querystring = new Dictionary<string, string> { { "email", userEmail } };
            try
            {
                var response = this._helpDeskService.BuildRequest("UserByEmail", querystring);
                var result = JToken.Parse(response);
                var user = result.ToObject<HelpDeskUser>();
                querystring.Clear();
                querystring.Add("handledByUserID", user.UserID.ToString());
                return JToken.Parse(this._helpDeskService.ForwardGetRequest(entity, querystring));
            }
            catch (Exception ex)
            {
            }
        }
        public JToken GetHelpDeskTickets( string categoryid)
        {
            var querystring = new Dictionary<string, string>
            {
                { "mode", "unclosed"},
                { "categoryid", categoryid },
                { "count", "100" } 
            };
            try
            {
                var response = this._helpDeskService.BuildRequest("tickets", querystring);
                var result = JToken.Parse(response);
                return result;
            }
            catch (Exception ex)
            {
            }
        }
        public string GetCreatedUser()
        {
            var querystring = new Dictionary<string, string>
            {
                { "email", "jijo@me.com" },
                { "firstName", "Jijo" } ,
                { "lastName", "vg" } ,
                { "userName", "jijo" } ,
                { "sendWelcomeEmail", "true" }
            };
            try
            {
                var response = this._helpDeskService.HttpPostRequest("CreateUser", querystring);
                return response;
            }
            catch (Exception ex)
            {

            }
        }
Advertisements

Visual Studio Code

‘Hello World’ with Node.js and Visual Studio Code
Visual Studio Code is the first code editor, and first cross-platform development tool – supporting OS X, Linux, and Windows – in the Visual Studio family. At its heart, Visual Studio Code is a code editor. Like many other code editors, VS Code adopts a common UI and layout.
Recently there was a request from the Business team to evaluate Visual Studio Code as a part of project proposal. This has ended up coming up with a sample Hello World Application with Node.js and Visual Studio Code.
Prerequisites: Download Visual Studio Code and Node.Js
Step 1:
As stated above, VSC is directory structure-based. For my tutorial, I’m going to create the folder C:\Samples\HelloWorld.From Windows Explorer, right-click on it and Click on “Open with Code”.
Create Working Folder
Step 2:
Node.js is a JavaScript runtime library. In simple words it is something that we are going to replace IIS. I need some dll to be installed to handle server requests. Go to Admin Command Prompt and inside HelloWorld directory type below commands one after another:
1. npm install express
2. npm install body-parser
3. npm install jquery
After successfully completed this command you will see some files downloaded and added to project folder.
Step 3:
At any administrator command prompt type the following:
1. npm install -g tsd
Step 4:
At the command prompt, inside of the HelloWorld directory, type in the following:
1. npm install tsd
Once you’ve completed these steps, your HelloWorld VSC window should look like the following:
aaaa
Step 5:
Visual Studio Code executes “tasks” for building, deploying, etc. What we must do is configure our own task runner to run our application with Node.js.
1. Click on Ctrl+P will display VCS command prompt , type > Configure Task Runner
2. Upon choosing this option, a .settings folder is created with a file named tasks.json. Delete/Comment everything on it and paste below line:
{
“version”: “0.1.0”,
“command”: “node”,
“isShellCommand”: false,
“args”: [
“app.js”
]
}
Step 6:
While at the command prompt (in your project folder), type the following:
1. node_modules\.bin\tsd install express
2. node_modules\.bin\tsd install body-parser
3. node_modules\.bin\tsd install jquery
Step 7:
Now that the environment has been setup successfully, we need to create a web server configuration file. Node.js will launch a web server instance based on the configuration we specify. If you remember from the previous page, we created a task to execute Node.js and specified an argument of app.js.

/// <reference path="typings/node/node.d.ts" />
/// <reference path="typings/express/express.d.ts" />
/// <reference path="typings/body-parser/body-parser.d.ts" />
var express = require('express');
var app = express();
app.use('/', express.static('./'));
app.get('/', function(req, res){
    res.sendFile('default.html', { root: __dirname });
});
app.listen(8081);

Step 8:
Finally, let’s create our content page. Create a page named default.html in the home folder. Then, add the following contents:

<html>
  <head>
    <title>Node.js</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    Sample POC code. 
  </body>
</html>

Run the task
Because the task has already been configured previously, we simply need to press Ctrl+Shift+B (for Build). This will execute our task. Provided that we don’t get any errors in our Output window, Node.js will have created a background process in Windows.
Open a web browser and go to the URL configured above (in this case, http://localhost:8081/). Again, provided there are no errors reported in your browser or VSC’s Output window, you should see the contents of your default.html page.
Cancelling the Task
Keep in mind that once I start the Node.js server, it is an child task of VSC meaning that, even though I may not see it, Node.js is running in the background.
To cancel the task in VSC:
Press Ctrl+P
Begin typing: >terminate

Reference Read:
1. https://code.visualstudio.com/Docs
2. https://www.visualstudio.com/en-us/products/code-vs.aspx
3. http://jdav.is/2015/08/05/hello-world-with-node-js-and-visual-studio-code/