{"id":3399,"date":"2013-12-19T17:37:48","date_gmt":"2013-12-19T15:37:48","guid":{"rendered":"http:\/\/www.juust.org\/?p=3399"},"modified":"2013-12-19T18:28:10","modified_gmt":"2013-12-19T16:28:10","slug":"accessing-the-kiva-api-with-visual-basic","status":"publish","type":"post","link":"https:\/\/www.juust.org\/index.php\/accessing-the-kiva-api-with-visual-basic\/2013\/12\/","title":{"rendered":"Accessing the Kiva Api with Visual Basic"},"content":{"rendered":"<p>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.<\/p>\n<p>The example project is on the server :\u00a0 <a href=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/KivaVBLibrary.zip\" rel=\"nofollow\">KivaVBLibrary Kiva Api example in VB<\/a> (coded in <a href=\"http:\/\/www.visualstudio.com\/downloads\/download-visual-studio-vs#DownloadFamilies_4\" rel=\"nofollow noopener\" target=\"_blank\">Visual Basic Express 2010<\/a>)<\/p>\n<h3>About the API<\/h3>\n<p>The Kiva API is a HTTP Rest API\u00a0 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.<\/p>\n<h3>Rate limiting and TOS<\/h3>\n<p>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.<\/p>\n<h3>The sample<\/h3>\n<p>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.<\/p>\n<p>I took the UrlFactory Class from the <a href=\"http:\/\/kivalibrary.codeplex.com\/\" rel=\"nofollow noopener\" target=\"_blank\">Kiva Api Library C#<\/a> example, and recoded part of it in Visual Basic, one class <strong>URLFactory<\/strong> to compose the URL&#8217;s accor :<\/p>\n<pre lang=\"vb\">Namespace KivaVBLibrary\r\n\r\n    Public Class URLFactory\r\n\r\n        Public baseURL As String = \"http:\/\/api.kivaws.org\/v1\/\"\r\n        Public urlSuffix As String = \".xml\"\r\n        \/\/The API returns either html, xml or json depending on the suffix you send in the url.\r\n\r\n        Public Function GetLenderURL(ByVal UID As String) As String\r\n            Return baseURL + \"lenders\/\" + UID + urlSuffix\r\n        End Function\r\n    End Class\r\nEnd NameSpace<\/pre>\n<p>With that URL available, let&#8217;s get some XML, in this case the basic lender record of Matt (one of the co founders of kiva), retrieved from <strong>\/\/api.kivaws.org\/v1\/lenders\/matt.xml<\/strong><\/p>\n<p><a href=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-003.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter  wp-image-3415\" title=\"kiva api example visual basic 003\" src=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-003-300x263.png\" alt=\"\" width=\"610\" height=\"406\" \/><\/a>The GetLender function of the KivaAPI class gets the XML data from the API and returns it as Dataset :<\/p>\n<pre lang=\"php\">Imports System.Net\r\nImports System.Text\r\n\r\nNamespace KivaVBLibrary\r\n\r\nPublic Class KivaAPI\r\n\r\n    Private _UrlFactory As New KivaVBLibrary.URLFactory\r\n\r\n    Private _AppID As String = \"org.juust.kivareader\"\r\n    Private _UserAgent As String = \"KivaReader (https:\/\/www.juust.org) appid:\" &amp; _AppID\r\n\r\n    Private _userName As String = \"\"\r\n    Private _userPassword As String = \"\"\r\n\r\n    Public Property AppID As String\r\n        Get\r\n            Return _AppID\r\n        End Get\r\n        Set(ByVal value As String)\r\n            _AppID = value\r\n        End Set\r\n    End Property\r\n\r\n    Public Property userName As String\r\n        Get\r\n            Return _userName\r\n        End Get\r\n        Set(ByVal value As String)\r\n            _userName = value\r\n        End Set\r\n    End Property\r\n\r\n    Public Property userPassword As String\r\n        Get\r\n            Return _userPassword\r\n        End Get\r\n        Set(ByVal value As String)\r\n            _userPassword = value\r\n        End Set\r\n    End Property\r\n\r\n    Public Sub New()\r\n    End Sub\r\n\r\n    Public Sub SetBasicAuthHeader(ByVal request As HttpWebRequest)\r\n\r\n        Dim authInfo As String = Me.userName + \":\" + Me.userPassword\r\n        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))\r\n        With request\r\n            .Headers(\"Authorization\") = \"Basic \" + authInfo\r\n            .Accept = \"application\/xml\"\r\n            .UserAgent = \"KivaReader 1.0.0.0 (juust.org)\"\r\n            .AllowAutoRedirect = False\r\n            .Timeout = 5000\r\n            .KeepAlive = False\r\n        End With\r\n    End Sub\r\n\r\n    Public Function GetLender(ByVal LenderID As String) As DataSet\r\n\r\n        Dim MyRequest As HttpWebRequest\r\n\r\n        MyRequest = HttpWebRequest.Create(_UrlFactory.GetLenderURL(LenderID))\r\n        With MyRequest\r\n            .Method = \"GET\"\r\n        End With\r\n\r\n        SetBasicAuthHeader(MyRequest)\r\n\r\n        Dim DataSetLender As DataSet\r\n        DataSetLender = New DataSet()\r\n        Using MyResponse As HttpWebResponse = MyRequest.GetResponse()\r\n            DataSetLender.ReadXml(MyResponse.GetResponseStream())\r\n        End Using\r\n        MyRequest = Nothing\r\n\r\n        Return DataSetLender\r\n\r\n    End Function\r\n\r\nEnd Class\r\nEnd NameSpace<\/pre>\n<p>Now we have the code to retrieve the DataSet object from the Kiva API, so let&#8217;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 :<\/p>\n<p><a href=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-001.png\"><img decoding=\"async\" class=\"aligncenter  wp-image-3406\" title=\"kiva api example visual basic 001\" src=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-001-300x206.png\" alt=\"\" width=\"534\" height=\"365\" srcset=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-001-300x206.png 300w, https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-001-150x103.png 150w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/a><\/p>\n<p>&#8230;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 :<\/p>\n<pre lang=\"php\">Public Class Form1\r\n\r\n    Private myApi As New KivaVBLibrary.KivaAPI\r\n\r\n    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntestApi.Click\r\n        myApi.userName = TxtUsername.Text\r\n        myApi.userPassword = TxtPassword.Text\r\n\r\n        Dim ds As DataSet\r\n        ds = myApi.GetLender(TxtLender.Text)\r\n\r\n        PrintDataRecords(ds)\r\n        PrintDataSet(ds)\r\n\r\n    End Sub<\/pre>\n<p>Click the button on the form, and after a second, we get our first data :<\/p>\n<p><a href=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-002.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-3407\" title=\"kiva api example visual basic 002\" src=\"https:\/\/www.juust.org\/wp-content\/uploads\/2013\/12\/kiva-api-example-visual-basic-002-300x196.png\" alt=\"\" width=\"400\" height=\"240\" \/><\/a><\/p>\n<p>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 :<\/p>\n<pre lang=\"php\">   Public Sub PrintDataRecords(ByVal ds As DataSet)\r\n\r\n        \/\/ Print out all tables and their columns\r\n        Dim iRow As DataRow\r\n        Dim iCol As DataColumn\r\n        For Each table As DataTable In ds.Tables\r\n            For Each iRow In table.Rows\r\n                For Each iCol In table.Columns\r\n                    RTLender.AppendText(iCol.ToString)\r\n                    RTLender.AppendText(vbTab &amp; iRow(iCol.ToString).ToString)\r\n                    RTLender.AppendText(vbCrLf)\r\n                Next\r\n            Next\r\n        Next  \/\/ For Each table  \r\n\r\n    End Sub\r\n\r\n    Public Sub PrintDataSet(ByVal ds As DataSet)\r\n\r\n        \/\/ Print out all tables and their columns\r\n        For Each table As DataTable In ds.Tables\r\n            RTTableSet.AppendText(vbCrLf &amp; \"TABLE \" &amp; table.TableName)\r\n            RTTableSet.AppendText(vbCrLf &amp; \"Total # of rows: \" &amp; table.Rows.Count)\r\n            RTTableSet.AppendText(vbCrLf &amp; \"---------------------------------------------------------------\")\r\n\r\n            For Each column As DataColumn In table.Columns\r\n                RTTableSet.AppendText(vbCrLf &amp; column.ColumnName &amp; \" \" &amp; column.DataType.ToString())\r\n            Next  \/\/ For Each column\r\n            RTTableSet.AppendText(vbCrLf)\r\n\r\n        Next  \/\/ For Each table  \r\n\r\n        \/\/ Print out table relations\r\n        For Each relation As DataRelation In ds.Relations\r\n            RTTableSet.AppendText(vbCrLf &amp; \"RELATION: \" &amp; relation.RelationName)\r\n            RTTableSet.AppendText(vbCrLf &amp; \"---------------------------------------------------------------\")\r\n            RTTableSet.AppendText(vbCrLf &amp; \"Parent: \" &amp; relation.ParentTable.TableName)\r\n            RTTableSet.AppendText(vbCrLf &amp; \"Child: \" &amp; relation.ChildTable.TableName)\r\n            RTTableSet.AppendText(vbCrLf)\r\n        Next  \/\/ For Each relation  \r\n\r\n    End Sub<\/pre>\n<p>\u3055\u3088\u306a\u3089 (=goodbye)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":5796,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4],"tags":[75,82],"class_list":["post-3399","post","type-post","status-publish","format-standard","hentry","category-juust","tag-microfinance","tag-visual-basic"],"_links":{"self":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts\/3399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/users\/5796"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/comments?post=3399"}],"version-history":[{"count":0,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts\/3399\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/media?parent=3399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/categories?post=3399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/tags?post=3399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}