Accessing the Kiva Api with Visual Basic

Kiva list a Dot Net Api class in C# so writing your own in Visual Basic is a tat obsolete were it not that I already had a complete database to process the data dump and I wanted a basic API class that only consumes XML and exposes it as a DataSet to update the main database, and add more user-oriented functionality, so I wrote a quick basic API-client. There, I said it.

The example project is on the server :  KivaVBLibrary Kiva Api example in VB (coded in Visual Basic Express 2010)

About the API

The Kiva API is a HTTP Rest API  using Basic HTTP Auth. I noticed Kiva do not check on credentials on the function I list below here, but I included a basic http authentication anyway. I do expect Kiva to switch to using OAuth somewhere along the line, but for now it is Basic HTTP Auth.

Rate limiting and TOS

Kiva allow 60 calls per minute for unregistered and 500 calls a minute per registered account, Kiva terms are general but Kiva emphasize you should respect the privacy of site users and not misrepresent the data.

The sample

On with the sample, I am going to make a Visual Basic Class to access the Kiva API, retrieve an XML response file, load that into a DataSet object and retrieve and display the data from the DataSet object on a Form.

I took the UrlFactory Class from the Kiva Api Library C# example, and recoded part of it in Visual Basic, one class URLFactory to compose the URL’s accor :

Namespace KivaVBLibrary
  2.     Public Class URLFactory
  4.         Public baseURL As String = ""
  5.         Public urlSuffix As String = ".xml"
  6.         //The API returns either html, xml or json depending on the suffix you send in the url.
  8.         Public Function GetLenderURL(ByVal UID As String) As String
  9.             Return baseURL + "lenders/" + UID + urlSuffix
  10.         End Function
  11.     End Class
  12. End NameSpace

With that URL available, let’s get some XML, in this case the basic lender record of Matt (one of the co founders of kiva), retrieved from //

The GetLender function of the KivaAPI class gets the XML data from the API and returns it as Dataset :

Imports System.Net
  1. Imports System.Text
  3. Namespace KivaVBLibrary
  5. Public Class KivaAPI
  7.     Private _UrlFactory As New KivaVBLibrary.URLFactory
  9.     Private _AppID As String = "org.juust.kivareader"
  10.     Private _UserAgent As String = "KivaReader ( appid:" & _AppID
  12.     Private _userName As String = ""
  13.     Private _userPassword As String = ""
  15.     Public Property AppID As String
  16.         Get
  17.             Return _AppID
  18.         End Get
  19.         Set(ByVal value As String)
  20.             _AppID = value
  21.         End Set
  22.     End Property
  24.     Public Property userName As String
  25.         Get
  26.             Return _userName
  27.         End Get
  28.         Set(ByVal value As String)
  29.             _userName = value
  30.         End Set
  31.     End Property
  33.     Public Property userPassword As String
  34.         Get
  35.             Return _userPassword
  36.         End Get
  37.         Set(ByVal value As String)
  38.             _userPassword = value
  39.         End Set
  40.     End Property
  42.     Public Sub New()
  43.     End Sub
  45.     Public Sub SetBasicAuthHeader(ByVal request As HttpWebRequest)
  47.         Dim authInfo As String = Me.userName + ":" + Me.userPassword
  48.         authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
  49.         With request
  50.             .Headers("Authorization") = "Basic " + authInfo
  51.             .Accept = "application/xml"
  52.             .UserAgent = "KivaReader ("
  53.             .AllowAutoRedirect = False
  54.             .Timeout = 5000
  55.             .KeepAlive = False
  56.         End With
  57.     End Sub
  59.     Public Function GetLender(ByVal LenderID As String) As DataSet
  61.         Dim MyRequest As HttpWebRequest
  63.         MyRequest = HttpWebRequest.Create(_UrlFactory.GetLenderURL(LenderID))
  64.         With MyRequest
  65.             .Method = "GET"
  66.         End With
  68.         SetBasicAuthHeader(MyRequest)
  70.         Dim DataSetLender As DataSet
  71.         DataSetLender = New DataSet()
  72.         Using MyResponse As HttpWebResponse = MyRequest.GetResponse()
  73.             DataSetLender.ReadXml(MyResponse.GetResponseStream())
  74.         End Using
  75.         MyRequest = Nothing
  77.         Return DataSetLender
  79.     End Function
  81. End Class
  82. End NameSpace

Now we have the code to retrieve the DataSet object from the Kiva API, so let’s make a quick Form, with three texboxes (username, password and the id of the lender you want to retrieve data for), two rich format boxes to display data from the dataset and a button to run the API Test :

…and in the form module we add some basic code that creates an instance of KivaVBLibrary.KivaAPI, sets our basic authorization, and calls the GetLender function to retrieve the Lender data for the ID we fill in in the form :

Public Class Form1
  2.     Private myApi As New KivaVBLibrary.KivaAPI
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntestApi.Click
  5.         myApi.userName = TxtUsername.Text
  6.         myApi.userPassword = TxtPassword.Text
  8.         Dim ds As DataSet
  9.         ds = myApi.GetLender(TxtLender.Text)
  11.         PrintDataRecords(ds)
  12.         PrintDataSet(ds)
  14.     End Sub

Click the button on the form, and after a second, we get our first data :

The Dataset is processed twice, one for general Table set data, and one for the content using the two rich text boxes (RtLender and RTTableSet) to display data on the form :

Public Sub PrintDataRecords(ByVal ds As DataSet)
  2.         // Print out all tables and their columns
  3.         Dim iRow As DataRow
  4.         Dim iCol As DataColumn
  5.         For Each table As DataTable In ds.Tables
  6.             For Each iRow In table.Rows
  7.                 For Each iCol In table.Columns
  8.                     RTLender.AppendText(iCol.ToString)
  9.                     RTLender.AppendText(vbTab & iRow(iCol.ToString).ToString)
  10.                     RTLender.AppendText(vbCrLf)
  11.                 Next
  12.             Next
  13.         Next  // For Each table  
  15.     End Sub
  17.     Public Sub PrintDataSet(ByVal ds As DataSet)
  19.         // Print out all tables and their columns
  20.         For Each table As DataTable In ds.Tables
  21.             RTTableSet.AppendText(vbCrLf & "TABLE " & table.TableName)
  22.             RTTableSet.AppendText(vbCrLf & "Total # of rows: " & table.Rows.Count)
  23.             RTTableSet.AppendText(vbCrLf & "—————————————————————")
  25.             For Each column As DataColumn In table.Columns
  26.                 RTTableSet.AppendText(vbCrLf & column.ColumnName & " " & column.DataType.ToString())
  27.             Next  // For Each column
  28.             RTTableSet.AppendText(vbCrLf)
  30.         Next  // For Each table  
  32.         // Print out table relations
  33.         For Each relation As DataRelation In ds.Relations
  34.             RTTableSet.AppendText(vbCrLf & "RELATION: " & relation.RelationName)
  35.             RTTableSet.AppendText(vbCrLf & "—————————————————————")
  36.             RTTableSet.AppendText(vbCrLf & "Parent: " & relation.ParentTable.TableName)
  37.             RTTableSet.AppendText(vbCrLf & "Child: " & relation.ChildTable.TableName)
  38.             RTTableSet.AppendText(vbCrLf)
  39.         Next  // For Each relation  
  41.     End Sub

さよなら (=goodbye)

Posted in juust and tagged , , .


  1. The prostate related is an essential portion of a male’s reproductive :. It secretes fluids that assisted in the transportation and activation of sperm. The men’s prostate is located just before the rectum, below the bladder and surrounding the urethra. When there is prostate problem, it is usually really really irritating and inconvenient for that patient as his urinary method is directly affected.

    The common prostate health issues are prostate infection, enlarged prostate and prostate cancer.

    Prostate infection, often known as prostatitis, is the most common prostate-related symptom in men younger than 55 yrs . old. Infections with the prostate gland are classified into four types – acute bacterial prostatitis, chronic bacterial prostatitis, chronic abacterial prostatitis and prosttodynia.

    Acute bacterial prostatitis could be the least common of most varieties of prostate infection. It is caused by bacteria based in the large intestines or urinary tract. Patients can experience fever, chills, body aches, back pains and urination problems. This condition is treated by utilizing antibiotics or non-steroid anti-inflammatory drugs (NSAIDs) to relieve the swelling.

    Chronic bacterial prostatitis is really a condition associated with a particular defect inside the gland and also the persistence presence of bacteria within the urinary tract. It can be caused by trauma on the urinary tract or by infections from other regions in the body. A patient can experience testicular pain, small of the back pains and urination problems. Although it is uncommon, it is usually treated by removal of the prostate defect accompanied by the utilization antibiotics and NSAIDs to treat the soreness.

    Non-bacterial prostatitis is the reason for approximately 90% of prostatitis cases; however, researchers have not even to create the sources of these conditions. Some researchers think that chronic non-bacterial prostatitis occur due to unknown infectious agents while other believe that intensive exercise and high lifting may cause these infections.

    Maintaining a Healthy Prostate

    To prevent prostate diseases, a suitable diet is important. These are some from the actions to maintain your prostate healthy.

    1. Drink sufficient water. Proper hydration is critical for overall health and it will also maintain the urinary track clean.

    2. Some studies claim that a few ejaculations each week will assist you to prevent prostate cancer.

    3. Eat steak without excess. It has been shown that consuming a lot more than four meals of beef every week will increase the chance of prostate diseases and cancer.

    4. Maintain a suitable diet with cereals, vegetable and fruits to be sure sufficient intake of nutrients necessary for prostate health.

    The most critical measure to adopt to be sure a normal prostate would be to opt for regular prostate health screening. If you are forty years and above, you should select prostate examination at least once annually.

Leave a Reply

Your email address will not be published. Required fields are marked *