Jump to content

Recommended Posts

Hey,

i´d like to get Informations about some Betfair Exchange Sports via JSON requests.

Unfortunately all the Example codes on their Website are Python codes... I´m a beginner in the coding world and i have no idea how to use the Python codes in AutoIT.

Could someone translate the Python code into a AutoIT code to make it more understandable to me ? If its not too much work, it would be awesome if u can add some informations in the Autoit code about whats happening in the it.

Example: (source)

Global Exchange

JSON-RPC https://api.betfair.com/exchange/betting/json-rpc/v1 <methodname> SportsAPING/v1.0/listMarketBook
JSON REST https://api.betfair.com/exchange/betting/rest/v1.0/   listMarketBook/

 

[
    {
        "jsonrpc": "2.0",
        "method": "SportsAPING/v1.0/listMarketCatalogue",
        "params": {
            "filter": {
                "eventIds": [
                    "27165685"
                ]
            },
            "maxResults": "200",
            "marketProjection": [
                "COMPETITION",
                "EVENT",
                "EVENT_TYPE",
                "RUNNER_DESCRIPTION",
                "RUNNER_METADATA",
                "MARKET_START_TIME"
            ]
        },
        "id": 1
    }
]

 

It would be awesome if someone is able to translate it :)

Thanks!

Fjallalingur

 

Share this post


Link to post
Share on other sites
49 minutes ago, Fjallalingur said:

Could someone translate the Python code into a AutoIT code to make it more understandable to me ?

Sure, does $200/hour sound also reasonable to you? 🙂 

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Don't know...didn't look, but in case you missing it: This is an AutoIt3 support forum, not a "dump your request and let other code it for you" forum.
I have no issue when you want that, but then you need to pay for it...  far enough right?
Other option is to do a search for JSON and you will find a few pretty nice  and simple examples how to code it yourself. 

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I´m trying to figure it out how to do it by myself now.

I´ve been looking around for over 7hours to find anything that could help me 😕

As i said i´m a beginner in this topic and I´m very lost :D

I downloaded the winhttp.au3 and found the _WinHttpSendRequest(*Sends the specified request to the HTTP server.). It looks like the function i need for my request, could you please double check it before i invest another 10 hours to figure out how to use this function ?

Thanks for your help

Share this post


Link to post
Share on other sites
21 minutes ago, Fjallalingur said:

I´ve been looking around for over 7hours to find anything that could help me 😕

I hope the 7 hours isn't around here, but show us what you have that isn't working yet and we will be happy to assist in making this happen.

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Posted (edited)

Based on curl examples https://github.com/betfair/API-NG-sample-code/blob/master/curl/rescript_curl.bash

this should get you started

But if you are a starter without knowledge on coding and REST API's it will no be much of help.

Below is a base start but probably better in AutoIt to use the WinHTTP udf.

$host="https://api.betfair.com/exchange/betting"

func Sendrequest($url, $appkey, $session, $data)
    $xhr=objcreate("MSXML2.XMLHTTP")
    $xhr.open("POST", $url & "/", False)
    $xhr.setrequestheader("X-Application", $appkey)
    $xhr.setrequestheader("Content-Type", "application/json")
    $xhr.setrequestheader("Accept", "application/json")

    if ($session <> "") Then
        $xhr.setrequestheader("X-Authentication", $session)
    EndIf

    $xhr.send($data)

    $responseText= $xhr.responsetext

    if $xhr.status <> 200 Then
        consolewrite("The call to API-NG was unsuccessful. Status code: " & $xhr.Status & " " & $xhr.statusText & ". Response was: " & $xhr.responseText & @CRLF)
    Else
        consolewrite($responsetext & @CRLF)
    EndIf
EndFunc

func list_all_event_types()
    sendrequest($host & "/rest/v1/listEventTypes", 0,0, '{"filter":{}}')
EndFunc

list_all_event_types()

I call the function with 0,0 as i do not have a appkey or session

Edited by junkew

Share this post


Link to post
Share on other sites

Since I dont know, how to edit my posts

why is it better to use WinHTTP udf in Autoit instead of how you´ve done the example ?

Share this post


Link to post
Share on other sites
Posted (edited)

okay, I think i need more help...

The first part is working fine now, I get a list of Event Types and i can change the Filters for it.

The 2nd part (List Available Races) should be the same, but I always get the same error : The call to API-NG was unsuccessful. Status code: 400 Bad Request. Response was: {"faultcode":"Client","faultstring":"DSC-0008","detail":{}}

 

$url = "https://api.betfair.com/exchange/betting"
$App_Key = "XXX"
$Session_Token = "XXX"

List_Available_Horse_Races()
Func List_Available_Horse_Races()
    Sendrequest_ListAvailableHorseRaces($url & "/rest/v1/listMarketCatalogue", '{"filter":{"eventTypeIds":["7"],"marketCountries":["GB"],"marketTypeCodes":["WIN"],"marketStartTime":["2019-05-0419:00+00:00"],"sort":["FIRST_TO_START"],"maxResults":["1"],"marketProjection":["COMPETITION"],"EVENT":["EVENT_TYPE"],"MARKET_DESCRIPTION","RUNNER_DESCRIPTION"}}')
EndFunc   ;==>List_Available_Horse_Races

Func Sendrequest_ListAvailableHorseRaces($url, $data)
    $xhr = ObjCreate("MSXML2.XMLHTTP")
    $xhr.open("POST", $url & "/", False)
    $xhr.setrequestheader("X-Application", $App_Key)
    $xhr.setrequestheader("Content-Type", "application/json")
    $xhr.setrequestheader("Accept", "application/json")

    If ($Session_Token <> "") Then
        $xhr.setrequestheader("X-Authentication", $Session_Token)
    EndIf

    $xhr.send($data)

    $responseText = $xhr.responsetext

    If $xhr.status <> 200 Then
        ConsoleWrite("The call to API-NG was unsuccessful. Status code: " & $xhr.Status & " " & $xhr.statusText & ". Response was: " & $xhr.responseText & @CRLF)
    Else
        ConsoleWrite($responseText & @CRLF)
    EndIf
EndFunc   ;==>Sendrequest_ListAvailableHorseRaces

 

I think its not a Problem because of the Filters, theres just 1 Filter required(maxResults). I get the same error if i just put the maxResults Filter in.

Edited by Fjallalingur

Share this post


Link to post
Share on other sites

I have no knowledge on the betting website api so you have to lokup the dsc errorcode in the api manual. Most likely your json string is incorrect. You should validate your json string for correctnes with a json validator.

Share this post


Link to post
Share on other sites

The JSON string was incorrect, because I didnt include the last filters in the filter "marketProjection".

I put the new filter into a json validator and it says "Valid JSON".

For some reason i still get the same faultcode back and i dont know why, because all other informations are the same as in example1(which works). I think I just have to change the filter+urlpage.

Share this post


Link to post
Share on other sites
OUT=`curl -s -X POST --header "Accept: application/json" --header "Content-Type: application/json" --header "X-Application: $APP_KEY" --header "X-Authentication:   $SESSION_TOKEN"  --data 
 "[{ \"jsonrpc\": \"2.0\",
 \"method\": \"SportsAPING/v1.0/listMarketCatalogue\",
 \"params\": 
 {\"filter\":{\"eventTypeIds\":[$HORSE_RACING_EVENT_TYPE_ID],
 \"marketCountries\" :[\"GB\"],
\"marketTypeCodes\" :[\"WIN\"],
\"marketStartTime\":{\"from\":\"$DATE\"}},
\"sort\":\"FIRST_TO_START\",
\"maxResults\":\"$MAX_RESULTS\",
\"marketProjection\":[\"RUNNER_DESCRIPTION\"]},
 \"id\": 1}]" 
 $HOST/json-rpc/v1`

And I see what you send as JSON data part it looks completely different besides some syntax differences for curl

'{"filter":{"eventTypeIds":["7"],
"marketCountries":["GB"],
"marketTypeCodes":["WIN"],
"marketStartTime":["2019-05-0419:00+00:00"],
"sort":["FIRST_TO_START"],
"maxResults":["1"],
"marketProjection":["COMPETITION"],
"EVENT":["EVENT_TYPE"],
"MARKET_DESCRIPTION",
"RUNNER_DESCRIPTION"}}')


So it seems you should study the different api examples in more detail

https://github.com/betfair/API-NG-sample-code/blob/master/curl/jsonrpc_curl.bash#L33

and compare with the other language examples

Maybe install curl on your system and start with those examples

 

Share this post


Link to post
Share on other sites
Posted (edited)

I´m a little confused now, because u sent 2 links with different codes.

I tried to translate the 2nd code:

OUT=`curl -s -X POST --header "Accept: application/json" --header "Content-Type: application/json" --header "X-Application: $APP_KEY" --header "X-Authentication:   $SESSION_TOKEN"  --data 
 "[{ \"jsonrpc\": \"2.0\",
 \"method\": \"SportsAPING/v1.0/listMarketCatalogue\",
 \"params\": 
 {\"filter\":{\"eventTypeIds\":[$HORSE_RACING_EVENT_TYPE_ID],
 \"marketCountries\" :[\"GB\"],
\"marketTypeCodes\" :[\"WIN\"],
\"marketStartTime\":{\"from\":\"$DATE\"}},
\"sort\":\"FIRST_TO_START\",
\"maxResults\":\"$MAX_RESULTS\",
\"marketProjection\":[\"RUNNER_DESCRIPTION\"]},
 \"id\": 1}]" 
 $HOST/json-rpc/v1`
List_Available_Horse_Races()
Func List_Available_Horse_Races()
    Sendrequest_ListAvailableHorseRaces($url & "/json-rpc/v1", '{"jsonrpc":["2.0"], "method":["SportsAPING/v1.0/listMarketCatalogue"], "params":[{"filter":{"eventTypeIds":["7"],"marketCountries":["GB"],"marketTypeCodes":["WIN"],"marketStartTime":["2019-05-0515:00+00:00"],"sort":["FIRST_TO_START"],"maxResults":["1"],"marketProjection":["RUNNER_DESCRIPTION"]},"id":["1"]}]}')
EndFunc   ;==>List_Available_Horse_Races

Func Sendrequest_ListAvailableHorseRaces($url, $data)
    $xhr = ObjCreate("MSXML2.XMLHTTP")
    $xhr.open("POST", $url & "/", False)
    $xhr.setrequestheader("X-Application", $App_Key)
    $xhr.setrequestheader("Content-Type", "application/json")
    $xhr.setrequestheader("Accept", "application/json")

    If ($Session_Token <> "") Then
        $xhr.setrequestheader("X-Authentication", $Session_Token)
    EndIf

    $xhr.send($data)

    $responseText = $xhr.responsetext

    If $xhr.status <> 200 Then
        ConsoleWrite("The call to API-NG was unsuccessful. Status code: " & $xhr.Status & " " & $xhr.statusText & ". Response was: " & $xhr.responseText & @CRLF)
    Else
        ConsoleWrite($responseText & @CRLF)
    EndIf
EndFunc   ;==>Sendrequest_ListAvailableHorseRaces

JSON:(valid)

{
    "jsonrpc": ["2.0"],
    "method": ["SportsAPING/v1.0/listMarketCatalogue"],
    "params": [{
        "filter": {
            "eventTypeIds": ["7"],
            "marketCountries": ["GB"],
            "marketTypeCodes": ["WIN"],
            "marketStartTime": ["2019-05-0515:00+00:00"],
            "sort": ["FIRST_TO_START"],
            "maxResults": ["1"],
            "marketProjection": ["RUNNER_DESCRIPTION"]
        },
        "id": ["1"]
    }]
}

 

The response was :"{"jsonrpc":"2.0","error":{"code":-32700,"message":"DSC-0008"}}"

Quote
code message meaning
-32700 Parse error Invalid JSON was received by the server.
An error occurred on the server while parsing the JSON text.

I´m lost now, it looks like my code is fine to me 😕

 

Edited by Fjallalingur

Share this post


Link to post
Share on other sites

A valid JSON is not neccesarily complying with what the api expects.

You send arrays to the api which it isn't expecting.

The examples from curl are most clean regarding straight forward url + json payload = response

Just replace from the curl example

  • \" with " only and you see your clean payload
  • add some crlf to make it human readable
  • properly check your opening and closing curly braces
  • Make sure you understand [] are json arrays
"[{ "jsonrpc": "2.0",
 "method": "SportsAPING/v1.0/listMarketCatalogue",
 "params": 
 {"filter":{"eventTypeIds":[$HORSE_RACING_EVENT_TYPE_ID],
 "marketCountries" :["GB"],
"marketTypeCodes" :["WIN"],
"marketStartTime":{"from":"$DATE"}},
"sort":"FIRST_TO_START",
"maxResults":"$MAX_RESULTS",
"marketProjection":["RUNNER_DESCRIPTION"]},
 "id": 1}]"

Apply it to your data

{
    "jsonrpc": "2.0",
    "method": "SportsAPING/v1.0/listMarketCatalogue",
    "params": [{
        "filter": {
            "eventTypeIds": ["7"],
            "marketCountries": ["GB"],
            "marketTypeCodes": ["WIN"],
            "marketStartTime": {from:"2019-05-0515:00+00:00"}
        },
        "sort": "FIRST_TO_START",
        "maxResults": "1",
        "marketProjection": ["RUNNER_DESCRIPTION"]
        },
        "id": "1"
    }]
}

 

Share this post


Link to post
Share on other sites

Its working :)

I´m gonna try to translate the other examples tomorrow, hopefully without help :)

Thanks a lot for your effort !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...