No announcement yet.

Visual Basic API to PB

  • Filter
  • Time
  • Show
Clear All
new posts

  • Visual Basic API to PB

    Need this code to be translated to PB.

    I have found description how it works in VB, maybe this helps:

    The Betfair Sports Exchange API 6 Reference Guide (API Guide) informs us that the Login, Logout and KeepAlive functions are on the Global server, so we will add a reference to this service. On the Project menu select "Add Service Reference". Click "Advanced", then click "Add Web Reference" to show the “Add Web Reference” dialogue form. In the URL box type the URL of the WSDL for the Global service:

    Click "Go". VB2008 now searches. If all goes well, the BFGlobalService is found. Change the Web reference name from “com.betfair.api” to “BFGlobal”. Click "Add Reference". VB2008 now adds the reference to the project and the BFGlobal icon should appear in the Solution Explorer (this can take a bit of time).

    Let’s reflect on what has just happened. Display the Object Browser. Expand the node "BetfairX.BFGlobal" and click on the "BFGlobalService" member. Here you will see all the classes, objects, properties, enums, etc. that are listed in the API Guide. This means that the entire resources of the Global API are now available to our project. We have achieved this very easily, without having to know anything about the underlying technologies (Soap, Wsdl, Xml, Http, etc.). I find this quite amazing.

    We now add the code for the login, logout and keepAlive functions. Refer to the API Guide for full details of each call. Add this code within the TestForm class, at the top:

    Public Class TestForm
        Dim oHeaderGL As New BFGlobal.APIRequestHeader
        Dim BetfairGL As New BFGlobal.BFGlobalService
        Dim WithEvents BetFairUK As New BFUK.BFExchangeService     'The UK ExchangeService object
        Private StateCount As Integer    'The unique state object
        Const SessTokFile = "C:\Betfair\SessToken.txt"
        Function oHeaderUK() As BFUK.APIRequestHeader
            Dim Header As New BFUK.APIRequestHeader
            Header.sessionToken = oHeaderGL.sessionToken
            Return Header
        End Function
        Sub CheckHeader(ByVal Header As BFUK.APIResponseHeader)
            With Header
                Print("HeaderCode = " & .errorCode.ToString)
                oHeaderGL.sessionToken = .sessionToken
            End With
        End Sub
        Sub CheckHeader(ByVal Header As BFGlobal.APIResponseHeader)
            With Header
                Print("HeaderCode = " & .errorCode.ToString)
                oHeaderGL.sessionToken = .sessionToken
            End With
        End Sub
        Sub Print(ByVal Message As String)
            With TextBox200
                .SelectionStart = .Text.Length
                .SelectedText = vbCrLf & Message
            End With
        End Sub
        Sub Printt(ByVal Message As String)
            With tlogg
                .SelectionStart = .Text.Length
                .SelectedText = vbCrLf & Message
            End With
        End Sub
        Private Sub bLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLogout.Click
            Printt("*** Logout ***")
            Dim oLogoutReq As New BFGlobal.LogoutReq
            Dim oLogoutResp As New BFGlobal.LogoutResp
            oLogoutReq.header = oHeaderGL
            oLogoutResp = BetfairGL.logout(oLogoutReq)     'Call the API
            With oLogoutResp
                Print("ErrorCode = " & .errorCode.ToString)
            End With
        End Sub
        Private Sub bKeepAlive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bKeepAlive.Click
            Print("*** KeepAlive ***")
            Dim oKeepAliveReq As New BFGlobal.KeepAliveReq
            Dim oKeepAliveResp As New BFGlobal.KeepAliveResp
            oKeepAliveReq.header = oHeaderGL
            oKeepAliveResp = BetfairGL.keepAlive(oKeepAliveReq)     'Call the API
        End Sub
        Private Sub bLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLogin.Click
            Print("*** Login ***")
            Dim oLoginReq As New BFGlobal.LoginReq
            Dim oLoginResp As New BFGlobal.LoginResp
            With oLoginReq
                .username = "xxxxxxx"
                .password = "xxxxxxx"
                .productId = 82           'For free API
            End With
            oLoginResp = BetfairGL.login(oLoginReq)     'Call the API
            With oLoginResp
                Print("ErrorCode = " & .errorCode.ToString)
            End With
        End Sub
        Private Sub TestForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            My.Computer.FileSystem.WriteAllText(SessTokFile, oHeaderGL.sessionToken, False)
        End Sub
        Private Sub TestForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            oHeaderGL.sessionToken = My.Computer.FileSystem.ReadAllText(SessTokFile)
        End Sub
        Private Sub bEvents_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bEvents.Click
            Print("*** Events ***")
            Dim oEventsReq As New BFGlobal.GetEventTypesReq             'Create request object
            Dim oEventsResp As New BFGlobal.GetEventTypesResp           'Create response object
            oEventsReq.header = oHeaderGL                               'Load request parameters
            oEventsResp = BetfairGL.getActiveEventTypes(oEventsReq)     'Call the API
            With oEventsResp
                CheckHeader(.header)                                    'Check response header
                Print("ErrorCode = " & .errorCode.ToString)
                If .errorCode = BFGlobal.GetEventsErrorEnum.OK Then     'Check the response errorcode
                    For i = 0 To .eventTypeItems.Length - 1             'Process the received data
                        With .eventTypeItems(i)
                            Print(.name & " (" & .id & ")")
                        End With
                End If
            End With
        End Sub
    End class
    Module AllMarkets
        Class MarketDataType           'For getAllMarkets data
            Public marketId As Integer
            Public marketName As String
            Public marketType As String
            Public marketStatus As String
            Public eventDate As DateTime
            Public menuPath As String
            Public eventHeirachy As String
            Public betDelay As Integer
            Public exchangeId As Integer
            Public countryCode As String
            Public lastRefresh As DateTime
            Public noOfRunners As Integer
            Public noOfWinners As Integer
            Public totalAmountMatched As Double
            Public bspMarket As Boolean
            Public turningInPlay As Boolean
            Public marketDisplayTime As DateTime
        End Class
        Class UnpackAllMarkets       'For getAllMArkets
            Public marketData As MarketDataType() = {}  'The returned array of market data
            Private Const BaseDate As DateTime = #1/1/1970#
            Private Const ColonCode = "&%^@"  'The substitute code for "\:"
            Sub New(ByVal MarketString As String)
                Dim n As Integer, Mdata, Field As String()
                Mdata = MarketString.Replace("\:", ColonCode).Split(":") 'Get array of Market substrings
                n = UBound(Mdata) - 1
                ReDim marketData(n)
                For i = 0 To n
                    Field = Mdata(i + 1).Replace("\~", "-").Split("~") 'Get array of data fields
                    marketData(i) = New MarketDataType
                    With marketData(i)
                        .marketId = Field(0)   'Load the array items
                        .marketName = Field(1).Replace(ColonCode, ":")
                        .marketType = Field(2)
                        .marketStatus = Field(3)
                        .eventDate = BaseDate.AddMilliseconds(Field(4))
                        .menuPath = Field(5).Replace(ColonCode, ":")
                        .eventHeirachy = Field(6)
                        .betDelay = Field(7)
                        .exchangeId = Field(8)
                        .countryCode = Field(9)
                        .lastRefresh = BaseDate.AddMilliseconds(Field(10))
                        .noOfRunners = Field(11)
                        .noOfWinners = Field(12)
                        .totalAmountMatched = Val(Field(13))
                        .bspMarket = (Field(14) = "Y")
                        .turningInPlay = (Field(15) = "Y")
                        .marketDisplayTime = BaseDate.AddMilliseconds(Field(10))
                    End With
            End Sub
        End Class
    End Module
    Last edited by Martin Dorfinger; 4 Oct 2009, 06:18 AM.