.NET VB Integration
.NET VB can be integrated using standard libraries, this example code was created using .NET framework 4.5.
The following name spaces were required to create the demo Windows application:
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Net
Imports System.Net.Security
Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Web.Script.Serialization
Making a GET Request
This example uses the the API to load the name associated with an email address, the HandleAPIRequest function is used for code simplification.
' Creates and initializes a new NameValueCollection.
Dim data As NameValueCollection = System.Web.HttpUtility.ParseQueryString("")
data.Add("api_id", "cDSJwf6hECIjy_3XgoGv7q8PsgI.")
data.Add("api_key", "3Mu6rpQY6n0IQDUYVG5SyFRtIA1uOf3gpOuAdk9Uaa4.")
data.Add("email_address", "example@demo.com")
'Run the API request and unpack the results
Dim response = HandleAPIRequest("GET", "control.instiller.co.uk/rest/users/details", data)
Dim Results = response.Item1
Dim User = response.Item2
' Check the response to get the details
Dim Message = ""
If (Results.Get("valid") = "false") Then
' Your error handling code
If (Results.Get("http_code") = "false") Then
' Problem connecting to the API
Message = "Connection error"
Else
' Show why we failed to load the details
Message = "Error: " + User("code").ToString() + " - " + User("reason")
End If
Else
' Your User processing code
Message = "Your contacts name is: " + User("first_name") + " " + User("last_name")
End If
' Display the results of the search
GETResponse.Text = Message
Making a POST Request
This example uses the the API to add or update a User and capture the unique ID allocated by the system, the HandleAPIRequest function is used for code simplification.
' Creates and initializes a new NameValueCollection.
Dim data As NameValueCollection = System.Web.HttpUtility.ParseQueryString("")
data.Add("api_id", "cDSJwf6hECIjy_3XgoGv7q8PsgI.")
data.Add("api_key", "3Mu6rpQY6n0IQDUYVG5SyFRtIA1uOf3gpOuAdk9Uaa4.")
data.Add("email_address", "example@demo.com")
data.Add("first_name", "Eric")
data.Add("last_name", "Example")
data.Add("reference_code", "MY-CUSTOMER-REF")
'Run the API request and unpack the results
Dim response = HandleAPIRequest("POST", "control.instiller.co.uk/rest/users/add_or_update", data)
Dim Results = response.Item1
Dim User = response.Item2
' Check the response to get the details
Dim Message = ""
If (Results.Get("valid") = "false") Then
' Your error handling code
If (Results.Get("http_code") = "false") Then
' Problem connecting to the API
Message = "Connection error"
Else
' Show why we failed to load the details
Message = "Error: " + User("code").ToString() + " - " + User("reason")
End If
Else
' Your User processing code
Message = "Your User has been created or updated our ID is: " + User("user_id").ToString()
End If
' Display the results of the search
POSTResponse.Text = Message
Request Wrapper
Creating a single function to handle your API requests will simplify your code and allow you to centralise error handling.
Private Function HandleAPIRequest(method As String, url As String, Data As NameValueCollection, Optional ResponseType As String = "application/json") As Tuple(Of NameValueCollection, Object)
' Initialise the results of the API request
Dim Results = New NameValueCollection()
Results.Add("valid", "false")
Results.Add("response", "")
' Initialise a blank result we can pass back
Dim response_data As Object
' Work out what request we need to process
Dim request As WebRequest
If (method.Equals("GET")) Then
' Create the web request
request = WebRequest.Create(url + "?" + Data.ToString())
Else
' Create the web request
request = WebRequest.Create(url)
' Set type to POST
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
' Create a byte array of the data we want to send
Dim byteData = UTF8Encoding.UTF8.GetBytes(Data.ToString())
' Set the content headers and write data
request.ContentLength = byteData.Length
Dim postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
End If
' Get the response from the remove server
Try
' Extract the response from the server
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As Stream = response.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(reader, encode)
Dim raw_response = readStream.ReadToEnd()
Results.Add("http_code", response.StatusCode.ToString())
If (response.StatusCode = HttpStatusCode.OK) Then
' We have got a 200 response from the server request is valid
Results.Set("valid", "true")
End If
' Check to see if we need decode the response data
If (ResponseType.Equals("application/json")) Then
' Use the native JS serializer to create dynamic object to avoid creating class declarations
Dim serializer = New JavaScriptSerializer()
response_data = serializer.Deserialize(Of Object)(raw_response)
Else
response_data = raw_response
End If
Catch ex As WebException
' Check to see if we can get the error code from the response
If (ex.Status = WebExceptionStatus.ProtocolError) Then
' Cast the exception as as HTTPResponse and grab the returned code
Dim response As HttpWebResponse = ex.Response
Results.Add("http_code", response.StatusCode.ToString())
' Get the response stream
Dim reader As Stream = response.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(reader, encode)
Dim raw_response = readStream.ReadToEnd()
' Check to see if we need decode the response data
If (ResponseType.Equals("application/json")) Then
' Use the native JS serializer to create dynamic object to avoid creating class declarations
Dim serializer = New JavaScriptSerializer()
response_data = serializer.Deserialize(Of Object)(raw_response)
Else
response_data = raw_response
End If
End If
End Try
' Return the information from the request and decoded data in a Tuple
Return New Tuple(Of NameValueCollection, Object)(Results, response_data)
End Function
What Next?
This example code can be customised to access any API endpoint:
- Locate the API endpoint you require using the API explorer
- Change the URL used to request the API
- Change the fields used in the request to match the API documentation