![]() CATEGORIES: BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism |
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() 'Set request parameters here End With RespAU = BetfairAU.getAllMarkets(ReqAU) 'Call AU exchange With RespAU CheckHeader(.header) If .errorCode = BFAU.GetAllMarketsErrorEnum.OK Then 'Process response from AU exchange here 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() 'Set request parameters here End With RespUK = BetfairUK.getAllMarkets(ReqUK) 'Call UK exchange With RespUK CheckHeader(.header) If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then 'Process response from UK exchange here 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() '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
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).
Date: 2014-12-21; view: 1140
|