Public Function LoadCountry() As List(Of Country)
Try
'Grab the data from Web API
Dim webClient As WebClient = New WebClient()
Dim URL As String = "https://api.covid19api.com/countries"
Dim json As String = webClient.DownloadString(New Uri(URL))
Dim obj = JsonConvert.DeserializeObject(Of List(Of Country))(json)
'Add a blank item
Dim blank As New Country
blank.Country = ""
blank.ISO2 = ""
blank.Slug = ""
obj.Add(blank)
'Sort the list
LoadCountry = obj.OrderBy(Function(x) x.Country).ToList()
Catch ex As Exception
LoadCountry = Nothing
End Try
End Function
Public Class Country
Public Property Country As String
Public Property Slug As String
Public Property ISO2 As String
End Class
Public Function LoadCountryStatus(Ctry As String, Dt As Date) As CountryStatus
Try
'Grab the data from Web API
Dim webClient As WebClient = New WebClient()
Dim URL As String = "https://api.covid19api.com/country/" & Ctry & "?from=" & Format(Dt, "yyyy-MM-dd") & "T00:00:00Z&to=" & Format(Dt, "yyyy-MM-dd") & "T00:00:00Z"
Dim json As String = webClient.DownloadString(New Uri(URL))
Dim obj = JsonConvert.DeserializeObject(Of List(Of CountryStatus))(json)
'Find the exact date's data
LoadCountryStatus = obj.Find(Function(p) p.Date = Dt.Date)
Catch ex As Exception
LoadCountryStatus = Nothing
End Try
End Function
Public Class CountryStatus
Public Property ID As String
Public Property Country As String
Public Property CountryCode As String
Public Property Province As String
Public Property City As String
Public Property CityCode As String
Public Property Lat As String
Public Property Lon As String
Public Property Confirmed As Integer
Public Property Deaths As Integer
Public Property Recovered As Integer
Public Property Active As Integer
Public Property [Date] As Date
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Binding
cbCountry.DataSource = COVID19.LoadCountry()
cbCountry.DisplayMember = "Country"
cbCountry.ValueMember = "Slug"
'Pre-select the Country I staying with
cbCountry.SelectedIndex = cbCountry.FindString("singapore")
'Trigger to get the data
btnGetData_Click(sender, e)
End Sub
Private Sub btnGetData_Click(sender As Object, e As EventArgs) Handles btnGetData.Click
'Something wrong with Web API? No data being populated
If cbCountry.Items.Count = 0 Then
Exit Sub
End If
'Simple validation to make sure a Country is selected
If cbCountry.SelectedValue = "" Then
MessageBox.Show("Please select a Country", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
'Get the expected data
Dim d As CountryStatus = COVID19.LoadCountryStatus(cbCountry.SelectedValue, dtDate.Value)
'Display the expected data
If d Is Nothing Then
lblConfirmed.Text = "Data not available"
lblDeaths.Text = "Data not available"
lblRecovered.Text = "Data not available"
lblActive.Text = "Data not available"
Else
lblConfirmed.Text = d.Confirmed.ToString
lblDeaths.Text = d.Deaths.ToString
lblRecovered.Text = d.Recovered.ToString
lblActive.Text = d.Active.ToString
End If
End Sub
Imports System.Net
Imports Newtonsoft.Json
Module COVID19
Public Function LoadCountry() As List(Of Country)
Try
'Grab the data from Web API
Dim webClient As WebClient = New WebClient()
Dim URL As String = "https://api.covid19api.com/countries"
Dim json As String = webClient.DownloadString(New Uri(URL))
Dim obj = JsonConvert.DeserializeObject(Of List(Of Country))(json)
'Add a blank item
Dim blank As New Country
blank.Country = ""
blank.ISO2 = ""
blank.Slug = ""
obj.Add(blank)
'Sort the list
LoadCountry = obj.OrderBy(Function(x) x.Country).ToList()
Catch ex As Exception
LoadCountry = Nothing
End Try
End Function
Public Function LoadCountryStatus(Ctry As String, Dt As Date) As CountryStatus
Try
'Grab the data from Web API
Dim webClient As WebClient = New WebClient()
Dim URL As String = "https://api.covid19api.com/country/" & Ctry & "?from=" & Format(Dt, "yyyy-MM-dd") & "T00:00:00Z&to=" & Format(Dt, "yyyy-MM-dd") & "T00:00:00Z"
Dim json As String = webClient.DownloadString(New Uri(URL))
Dim obj = JsonConvert.DeserializeObject(Of List(Of CountryStatus))(json)
'Find the exact date's data
LoadCountryStatus = obj.Find(Function(p) p.Date = Dt.Date)
Catch ex As Exception
LoadCountryStatus = Nothing
End Try
End Function
End Module
Public Class Country
Public Property Country As String
Public Property Slug As String
Public Property ISO2 As String
End Class
Public Class CountryStatus
Public Property ID As String
Public Property Country As String
Public Property CountryCode As String
Public Property Province As String
Public Property City As String
Public Property CityCode As String
Public Property Lat As String
Public Property Lon As String
Public Property Confirmed As Integer
Public Property Deaths As Integer
Public Property Recovered As Integer
Public Property Active As Integer
Public Property [Date] As Date
End Class
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)