Yesterday i have posted a code snippet which shows how to get a response from the API using Visual C#. Today i have finished the Visual Basic version.
The project is a WinForm project and we should add in Form1 the following code using the LoadForm or a ButtonClick event:
The code should give us a list with all event types. My C# example is based on JSON (Rest). Now i will use JSON-RPC.
Then we add a new class which is called "ResponseClass". As next we should install Newtonsoft.Json and add the following Imports:
Under the ResponseClass we insert the following classes (this not "best practice" - but here usefull):
We need this code for our JSON string. More soon. Now we fill the Response Class with following snippet:
The ResponseClass consists of our "main" sub (which procces the request and gets the response) and a function which delivers the json string (based on the other class below). You should also add a breakpoint at the start of the ResponseClass because the code is more a prototype and should be only used with the debugger.
The most difficult part was for me to create the proper JSON string, esp: the "params" part. Eventually i understand this can be done with nested objects. Our main object is the JsonRequest class which provides all four elements for a JSON-RPC request. The "params" element is based on the class "Parameter" to define a filter which is done in a further class (Request Filter). As we need no filter for our request the Request_Filter class is here empty. Only with this construction plan Json.Net can build the correct json string.
Json.Net docu: http://james.newtonking.com/json/help/index.html
(Samples/Serializing Json/Serialize an Object)
The project is a WinForm project and we should add in Form1 the following code using the LoadForm or a ButtonClick event:
Code:
Dim Response As New ResponseClass()
Response.EventTypesList()
Then we add a new class which is called "ResponseClass". As next we should install Newtonsoft.Json and add the following Imports:
Code:
Imports System.IO Imports System.Net Imports System.Text Imports Newtonsoft.Json
Code:
Public Class JsonRequest
Public Property jsonrpc As String
Public Property method As String
Public Property params As Parameter
Public Property id As Integer
End Class
Public Class Parameter
Public Property filter As Request_Filter
End Class
Public Class Ident
Public Property id As Integer
End Class
Public Class Request_Filter
End Class
Code:
Public Class ResponseClass
Inherits WebClient
Public Sub EventTypesList()
Dim token As String = "your token"
Dim key As String = "your api key" ' delayed AppKey
Dim address As String = "https://api.betfair.com/exchange/betting/json-rpc/v1"
' Step 1: Creating Request Object (Headers)
Dim request As HttpWebRequest = CType(WebRequest.Create(address), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/json"
request.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8")
request.Headers.Add("X-Application", key)
request.Headers.Add("X-Authentication", token)
' Step 2: Creating JSON (RPC)
Dim postData As String = Json_Object()
' Step 3: Adding JSON as Body (?) to Request (Request = Headers + Body)
Dim bytes As Byte() = Encoding.GetEncoding("UTF-8").GetBytes(postData)
request.ContentLength = bytes.Length
Dim out_stream As Stream = request.GetRequestStream()
out_stream.Write(bytes, 0, bytes.Length)
' Step 4: Getting the Response
Dim response As WebResponse = GetWebResponse(request)
' Step 5: Transfer Response in a String
Dim in_stream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(in_stream, Encoding.UTF8)
Dim api_response = reader.ReadToEnd()
' Step 6: Processing Response String
' ...
End Sub
Function Json_Object() As String
Dim rpc_Req As New JsonRequest()
Dim rpc_Params As New Parameter()
rpc_Params.filter = New Request_Filter()
rpc_Req.jsonrpc = "2.0"
rpc_Req.method = "SportsAPING/v1.0/listEventTypes"
rpc_Req.params = rpc_Params
rpc_Req.id = 1
Return JsonConvert.SerializeObject(rpc_Req)
End Function
End Class
The most difficult part was for me to create the proper JSON string, esp: the "params" part. Eventually i understand this can be done with nested objects. Our main object is the JsonRequest class which provides all four elements for a JSON-RPC request. The "params" element is based on the class "Parameter" to define a filter which is done in a further class (Request Filter). As we need no filter for our request the Request_Filter class is here empty. Only with this construction plan Json.Net can build the correct json string.
Json.Net docu: http://james.newtonking.com/json/help/index.html
(Samples/Serializing Json/Serialize an Object)


. I have a newbie question that I hope someone will help me with. I can get the response and have desearlised into the nested classes as suggested but, what code would i use to display the event types in simple listbox e.g. Market name followed by the id ?
Comment