Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • supunsilva.
    Junior Member
    • Aug 2009
    • 32

    #271
    Im sorry. It is not available in Free API. Thanks for your support

    Thank you,
    Supun Silva

    Comment

    • Mumbles0
      Junior Member
      • Jan 2009
      • 240

      #272
      Step 25. Accessing Australian Markets.

      To access the Australian exchange you can add another web reference (as described in the note at the bottom of Step 5). This creates the BFAU namespace which contains an identical set of classes to BFUK. This is fine if you want to access the Australian exchange only. You simply create a BFAU web reference and use this (instead of BFUK) throughout your code.

      But an app may want access to both UK and AU exchanges. Because BFAU and BFUK are namespaces, not objects, they cannot be simply assigned to variables. This leads to much code duplication. To illustrate this, consider a sub which calls getAllMarkets on either exchange using the separate web references:

      Code:
      Sub GetAllMarkets(ByVal Ex As Boolean)  'Ex = True for AU, False for UK
      
        If Ex Then  'AU exchange
      
          Dim ReqAU As New BFAU.GetAllMarketsReq
          Dim RespAU As BFAU.GetAllMarketsResp
          With ReqAU
            .header = oHeaderAU()
            [I]'Set request parameters here[/I]
          End With
          RespAU = BetfairAU.getAllMarkets(ReqAU) 'Call AU exchange
          With RespAU
            CheckHeader(.header)
            If .errorCode = BFAU.GetAllMarketsErrorEnum.OK Then
             [I] 'Process response from AU exchange here[/I]
            Else
              Print("ErrorCode = " & .errorCode.ToString)
            End If
          End With
      
        Else   'UK exchange
      
          Dim ReqUK As New BFUK.GetAllMarketsReq
          Dim RespUK As BFUK.GetAllMarketsResp
          With ReqUK
            .header = oHeaderUK()
            [I]'Set request parameters here[/I]
          End With
          RespUK = BetfairUK.getAllMarkets(ReqUK) 'Call UK exchange
          With RespUK
            CheckHeader(.header)
            If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then
             [I] 'Process response from UK exchange here[/I]
            Else
              Print("ErrorCode = " & .errorCode.ToString)
            End If
          End With
        End If
      
      End Sub
      Here you can see that much of the code is duplicated. You also need the additional BetfairAU service object, the oHeaderAU function and another CheckHeader sub. Problems can also arise when you try to process the response from the two servers. Even though the class BFAU.GetAllMarketsResp is identical to BFUK.GetAllMarketsResp the compiler assumes they are different. This can give rise to all sorts of problems and your code can become very messy. Perhaps some OOP guru can show us a better way of handling the two namespaces.

      However, there is another way we can do this. I doubt that this is “best practice” but it works.

      What we can do is write the code as if we are accessing the UK exchange only. Only one web reference (BFUK) is required. If we examine the service object BetfairUK we see that it has a .Url property. This property holds the web address (URL) of the server that it will access. By default this is set to the UK exchange server. If we change this, we can “trick” this service object into accessing the AU exchange server.

      Code:
      Sub GetAllMarkets(ByVal Ex As Boolean)   'Ex = True for AU, False for UK
      
        SelectExchange(Ex)    'Select the desired exchange URL
      
        Dim ReqUK As New BFUK.GetAllMarketsReq
        Dim RespUK As BFUK.GetAllMarketsResp
        With ReqUK
          .header = oHeaderUK()
          [I]'Set request parameters here[/I]
        End With
        RespUK = BetfairUK.getAllMarkets(ReqUK)    'Call the exchange (AU or UK)
        With RespUK
          CheckHeader(.header)
          If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then
           [I] 'Process response here[/I]
          Else
            Print("ErrorCode = " & .errorCode.ToString)
          End If
        End With
      End Sub
      
      Sub SelectExchange(ByVal Ex As Boolean)   'Ex = True for AU, False for UK
        If Ex Then  'AU exchange
          BetfairUK.Url = "https://api-au.betfair.com/exchange/v5/BFExchangeService"   'URL of AU exchange
        Else        'UK exchange
          BetfairUK.Url = "https://api.betfair.com/exchange/v5/BFExchangeService"    'URL of UK exchange
        End If
      End Sub
      Sub SelectExchange selects either the AU or UK exchange by changing the value of the Betfair.Url property. Depending on the value of the parameter Ex the subsequent call to getAllMarkets returns data from the desired exchange.

      This "trick" should work for all API exchange calls (although I have not fully tested this).
      Last edited by Mumbles0; 23-11-2009, 11:36 AM.

      Comment

      • Aitor
        Junior Member
        • Nov 2009
        • 8

        #273
        Hi good afternoon! I have problems when viewing shares on the Australian market, it gained from the tree visualization of events, but I can not see the market share. Thank you for your atentción and manejais any information here seems very interesting. I have the following error

        Error 1 Value of type 'Betfair2._0.BFAU.GetMarketPricesReq' can not be converted to '1-dimensional array Betfair2._0.BFAU.GetMarketPricesReq '. C: \ Documents and Settings \ AITOR \ My Documents \ Visual Studio 2008 \ Projects \ Betfair2.0 \ Betfair2.0 \ TestForm.vb 158 16 Betfair2.0

        because I created the following function

        CuotasFavorContraToolStripMenuItem_Click Private Sub (ByVal sender As Object, ByVal e As System.EventArgs) Handles CuotasFavorContraToolStripMenuItem.Click
        Print ("*** shares for and against in Fractions ")
        ShowMprices (BetFairAU.getMarketPrices (MpricesReqAU))
        End Sub

        MpricesReqAU Function () As BFAU.GetMarketPricesReq ()
        OMPReq Dim As New BFAU.GetMarketPricesReq ()
        With oMPReq
        . oHeaderAU header = ()
        . market = ID '100985049 'titles from madrid to 3991990
        End With
        Return oMPReq
        End Function

        any solution?

        Comment

        • Mumbles0
          Junior Member
          • Jan 2009
          • 240

          #274
          Reply to Aitor

          I don’t know what version of VB you are using but mine won’t accept names before keywords as shown in the code you have posted. I have re-arranged your code to that it compiles OK:

          Code:
          Private Sub CuotasFavorContraToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CuotasFavorContraToolStripMenuItem.Click
              Print("*** shares for and against in Fractions ")
              ShowMprices(BetFairAU.getMarketPrices(MpricesReqAU))
            End Sub
          
            Function MpricesReqAU() As BFAU.GetMarketPricesReq  '<<< Dont put brackets () at end
              Dim oMPReq As New BFAU.GetMarketPricesReq
              With oMPReq
                .header = oHeaderAU()
                .marketId = 100146716   '<<< Use a current AU marketId
              End With
              Return oMPReq
            End Function
          If you add the brackets () at the end of the function statement:
          Function MpricesReqAU() As BFAU.GetMarketPricesReq()

          you are asking the function to return an array of objects of type GetMarketPricesReq, but you only require a single object of type GetMarketPricesReq. This causes the error “Value of type 'BetfairX.BFAU.GetMarketPricesReq' cannot be converted to '1-dimensional array of BetfairX.BFAU.GetMarketPricesReq”.

          Comment

          • MichaelRNye
            Junior Member
            • Aug 2009
            • 9

            #275
            Originally posted by Mumbles0 View Post
            However, there is another way we can do this. I doubt that this is “best practice” but it works.

            What we can do is write the code as if we are accessing the UK exchange only. Only one web reference (BFUK) is required. If we examine the service object BetfairUK we see that it has a .Url property. This property holds the web address (URL) of the server that it will access. By default this is set to the UK exchange server. If we change this, we can “trick” this service object into accessing the AU exchange server.
            I would suggest maintaining two exchange objects (BFUK & BFAU in your example) and using a function to select the exchange which returns a BFServiceExchange.

            I program in C# so am not sure how the betfair api looks in VB, but my C# code looks like:

            Code:
            private BFExchangeService GetService(Exchange exchange)
                {
                  BFExchangeService service = null;
                    switch (exchange)
                    {
                      case Exchange.AU:
                        service = _ExchangeServiceAU;
                        break;
                      case Exchange.UK:
                        service = _ExchangeServiceUK;
                        break;
                    }        
                  return service;
                }
            With Exchange being a simple Enum I created with two values (AU and UK). The advantage this has over your method is it can handle parallel queries to both the AU and UK exchanges if you wanted to do that. It also is more fault tolerant, as if you are in the middle of querying something on the AU exchange then start a query on the UK exchange it will work fine. In your example it will change the UK's url and possibly break stuff.

            And, by using an Enum rather than true/false, if a 3rd exchange comes along it is dead simple to add it in without breaking existing applications.

            That's how I do it anyway.

            Comment

            • Mumbles0
              Junior Member
              • Jan 2009
              • 240

              #276
              UK &amp; AU Exchange changeover

              Michael,

              I agree there must be a better way. You've got me scratching my head again. I should learn a bit more C#.

              A question: Where is Class BFExchangeService defined?


              Re: Exchange Enum: I used the Boolean parameter "to keep things simple". Your enum is a far more satisfactory way of doing it.

              Comment

              • MichaelRNye
                Junior Member
                • Aug 2009
                • 9

                #277
                Originally posted by Mumbles0 View Post
                Michael,

                I agree there must be a better way. You've got me scratching my head again. I should learn a bit more C#.

                A question: Where is Class BFExchangeService defined?


                Re: Exchange Enum: I used the Boolean parameter "to keep things simple". Your enum is a far more satisfactory way of doing it.
                It's defined by the betfair WSDL file @ https://api.betfair.com/exchange/v5/...geService.wsdl. Add it as a web reference to the project (in C# at least) and then it (among other betfair-defined classes/types) are available for use.

                I don't believe there is a neater way to do it over a method similar to mine. Ultimately neatness is a personal thing and I find my method neat and easy to understand.

                Comment

                • Mumbles0
                  Junior Member
                  • Jan 2009
                  • 240

                  #278
                  Understood. BFExchangeService is a class defined by the WSDL for the UK Exchange.

                  Another question: How are _ExchangeServiceAU and _ExchangeServiceUK defined?

                  Comment

                  • MichaelRNye
                    Junior Member
                    • Aug 2009
                    • 9

                    #279
                    Originally posted by Mumbles0 View Post
                    Understood. BFExchangeService is a class defined by the WSDL for the UK Exchange.
                    Um, I don't believe it's specific to the UK exchange. betfair.api.exchange.BFExchangeService is the reference (no mention of UK there) and none of the functions are UK specific. I believe that's the entire idea - a single exchange service object which can be used for either UK or AU exchanges depending on how it is initialised

                    Originally posted by Mumbles0 View Post
                    Another question: How are _ExchangeServiceAU and _ExchangeServiceUK defined?
                    These are my own private variables. I have these defined in the class:

                    Code:
                        public const string UKENDPOINT = "https://api.betfair.com/exchange/v5/BFExchangeService";
                        public const string AUENDPOINT = "https://api-au.betfair.com/exchange/v5/BFExchangeService";
                    
                        private BFExchangeService _ExchangeServiceUK;
                        private BFExchangeService _ExchangeServiceAU;
                    and then in the constructor:

                    Code:
                          _ExchangeServiceUK = new BFExchangeService();
                          _ExchangeServiceAU = new BFExchangeService();
                    
                          _ExchangeServiceUK.Url = UKENDPOINT;
                    
                          _ExchangeServiceAU.Url = AUENDPOINT;

                    Comment

                    • Mumbles0
                      Junior Member
                      • Jan 2009
                      • 240

                      #280
                      I see what you are doing now.

                      We assume the Australian WSDL is identical to the UK WSDL in all respects (except for the endpoint URL). This implies the Australian WSDL is redundant.

                      (I am going on holiday for a week or so. I will consider this further when I return.)

                      Comment

                      • Aitor
                        Junior Member
                        • Nov 2009
                        • 8

                        #281
                        Hello, good morning! I wonder if there is any way to get to screen all such parties with their Spanish league Installments, ie to give me a button to display all the games this week with the share party.
                        Thanks for your attention

                        Comment

                        • Mumbles0
                          Junior Member
                          • Jan 2009
                          • 240

                          #282
                          Aitor,

                          I'm sure your English is better than my Spanish, but do you want a list of Spanish soccer match markets?

                          Comment

                          • Aitor
                            Junior Member
                            • Nov 2009
                            • 8

                            #283
                            yes, but now i can do that, thanks you for all the guide you give us, i learn a lot with you, you are my idol Mumbles0 jejej (lol), but i have a question. i couldn´t do to select automatically the uk market or de australian market, what can i do?

                            I am working with you example about todays horse race, but if i use thhis function
                            Sub GetAllMarkets(ByVal Ex As Boolean) 'Ex = True for AU, False for UK

                            SelectExchange(Ex) 'Select the desired exchange URL

                            Dim ReqUK As New BFUK.GetAllMarketsReq
                            Dim RespUK As BFUK.GetAllMarketsResp
                            With ReqUK
                            .header = oHeaderUK()
                            'Set request parameters here
                            End With
                            RespUK = BetFairUK.getAllMarkets(ReqUK) 'Call the exchange (AU or UK)
                            With RespUK
                            CheckHeader(.header)
                            If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then
                            'Process response here
                            Else
                            Print("ErrorCode = " & .errorCode.ToString)
                            End If
                            End With
                            End Sub

                            what i have to do to select australian or uk market if i use this function?where i put tur or false????

                            oMarketsResp = BetFairUK.getAllMarkets(oMarketsReq)
                            With BetFairUK.getAllMarkets(oMarketsReq) 'llamada a getAllMarkets
                            CheckHeader(.header)
                            Print("ErrorCode = " & .errorCode.ToString)

                            thanks you my friend

                            Comment

                            • Mumbles0
                              Junior Member
                              • Jan 2009
                              • 240

                              #284
                              Reply to Aitor (re: Calling subs)

                              Perhaps you need more understanding about calling Subs.

                              In Step 25, Sub GetAllMarkets has a calling parameter with name Ex. Ex is declared as type Boolean which can only have the values True or False. This Sub gets market data from either the UK or AU exchanges depending on the value of the Ex parameter you use when the sub is called.

                              For example, if you have 2 buttons, bUKmarkets and bAUmarkets, you can call Sub GetAllMarkets to return the market data from either exchange. The calls are shown in this code:

                              Code:
                              Private Sub bUKmarkets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUKmarkets.Click
                                GetAllMarkets(False)   'Get UK markets
                              End Sub
                              
                              Private Sub bAUmarkets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAUmarkets.Click
                                GetAllMarkets(True)   'Get AU markets
                              End Sub
                              Here you can see the main reason for using a Sub. You can execute the code contained in the Sub from different places within your project.

                              Note that you will have to put more code in Sub GetAllMarkets to set the request parameters and process the response (Steps 5 and 6 give examples of this).

                              (Note: I’ve used the simple Boolean parameter Ex to specify the desired exchange. MichaelRNye has made a good suggestion regarding this parameter. I will revise Step 25 to incorporate his suggestion soon.)

                              Comment

                              • Aitor
                                Junior Member
                                • Nov 2009
                                • 8

                                #285
                                Good morning!
                                I have a few days using the free API and these days I'm realizing that the next call does not work, the BetFairUK.getMarketPricesCompressedAsync (oMPCreq, StateCount), I wonder if you will work or not?
                                Sometines with some events it´s work, but for example if i select a spanish 2nd division one event, and then if i want to see correct scores odds, it´s all appears 0 0, 0 0, 0 0 .....
                                what is happening?

                                Ami I get the id of the forecast and next to 0 and 0, which is that?

                                thanks

                                Comment

                                Working...
                                X