ListClearedOrders code in VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IainMack
    Junior Member
    • Oct 2010
    • 19

    #1

    ListClearedOrders code in VB

    I used to program in VB prior to the .NET extensions, I've done a course but I can't connect the dots. I accept that it must be simple, but so far I haven't cracked it.

    That said: I have a VB program, written by someone I've lost touch with, that does some simple things, and I want to add a check for completed wagers: ie ListClearedOrders.

    I can't figure out how to define any of the required fields, nor how to call the API. Does anyone have VB.NET code that defines and calls the ListCleared function? And that you would be happy to share, that is.

    I believe that supplying an existing WagerId should give me a single response, and what I am looking for is the status of that wager (win or loss).

    Any advice or guidance is appreciated.

    IainMacK
  • only1jake
    Junior Member
    • Nov 2016
    • 29

    #2
    Hey there, welcome back to the forums!
    I believe this is the kind of code you are after!

    Code:
     Dim strrequest As String = "{""jsonrpc"": ""2.0"", ""method"": ""SportsAPING/v1.0/listClearedOrders"", ""params"": {""betStatus"":""SETTLED"", ""betIds"":[""" & mybetid & """]}, ""id"": 1}"
               
                Dim json As String = createrequest(BFAppKey, BfSessionID, strrequest)  '~~>Use Directly
                Dim serb As JObject = JObject.Parse(json)
    I use a function called createrequest which sends off the request to the API and gets a response in JSON, which we then parse so we can get the parameters and result.

    You need to have these at the top of your code:

    Code:
    Imports HtmlAgilityPack
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    From there you can do things like this to get the return details:
    Keep in mind this if from my code where I am requesting clearedorders by market and runner.
    Code:
                If serb.ToString.Contains("eventTypeId") Then
    
                    Dim ser As JObject = serb.SelectToken("result")
                    Dim clearedorders As JArray = ser.SelectToken("clearedOrders")
    end if
    The createrequest function:

    Code:
     Function createrequest(AppKey As String, SessToken As String, postData As String)
            Dim Url As String = "https://api.betfair.com/exchange/betting/json-rpc/v1/"
            Dim request As HttpWebRequest = Nothing
            
            Dim strResponseContent As String = ""
            Try
                Dim TimerStart As New DateTime()
                TimerStart = Now
                request = HttpWebRequest.Create(New Uri(Url))
                With request
                    .Accept = "application/json-rpc"
                    .Method = "POST"
                    .KeepAlive = True
                    .AutomaticDecompression = DecompressionMethods.Deflate
                    .AutomaticDecompression = DecompressionMethods.GZip
                    '.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8")
                    .Headers.Add("Accept-Encoding", "gzip,deflate")
                    .Headers.Add("X-Authentication", SessToken)
                    If Not String.IsNullOrEmpty(AppKey) Then .Headers.Add("X-Application", AppKey)
                End With
    
    
                Using dataStream As Stream = request.GetRequestStream()
                    Using writer As New StreamWriter(dataStream)
                        writer.Write(postData)
                    End Using
                End Using
    
                Using stream As Stream = DirectCast(request.GetResponse(), HttpWebResponse).GetResponseStream()
                    Using reader As New StreamReader(stream)
                        strResponseContent = reader.ReadToEnd()
    
                    End Using
                End Using
    
              
                Return strResponseContent
            Catch ex As Exception
                
            End Try
        End Function
    Note that this is all quite crude but it should get you going, are you able to login to your account through your program and get your SessionID?

    Keep in mind you can also group by Market:""groupBy"":""MARKET"" which returns different to the above.

    This is similar to what you could expect, without some of the empty fields which i removed:

    Code:
    '            {
                '  "jsonrpc": "2.0",
                '  "result": {
                '    "clearedOrders": [
                '      {
                '        "eventTypeId": "4339",
                '        "eventId": "",
                '        "marketId": "",
                '        "selectionId": ,
                '        "handicap": 0.0,
                '        "betId": "",
                '        "placedDate": "2017-06-14T11:04:02Z",
                '        "persistenceType": "LAPSE",
                '        "orderType": "LIMIT",
                '        "side": "BACK",
                '        "betOutcome": "WON",
                '        "priceRequested": 2.78,
                '        "settledDate": "2017-06-14T11:06:47Z",
                '        "lastMatchedDate": "2017-06-14T11:04:02Z",
                '        "betCount": 1,
                '        "priceMatched": 2.78,
                '        "priceReduced": false,
                '        "sizeSettled": ,
                '        "profit": 
                '      },
    I know some people do it differently with classes etc, but this is the straightforward way. You could create classes where the response gets passed to a class which holds all your variables!

    EDIT:
    Imports System.Net
    Last edited by only1jake; 27-06-2017, 02:36 AM.

    Comment

    • IainMack
      Junior Member
      • Oct 2010
      • 19

      #3
      ListCleared solution

      Thanks Only1Jake, that looks good so far. I do however have an issue. I get the error "HttpWebRequest not defined". I realise that means there is something about my environment that is missing, but I don't know what that is. Do you know what I'm missing and how to add it? I tried Dr Google but everything I found seemed to assume that the environment was as required.

      I appreciate your help.

      Iain

      Comment

      • only1jake
        Junior Member
        • Nov 2016
        • 29

        #4
        Sorry about that! I think you also need to add:

        Imports System.Net
        I didn't realise I had this for these requests!

        Comment

        • IainMack
          Junior Member
          • Oct 2010
          • 19

          #5
          List Cleared

          Thanks Just1Jake, all compile errors have gone, I appreciate your help.

          Iain

          Comment

          Working...
          X