durtal / betfaiR

R package for the Betfair API
http://durtal.github.io/betfaiR/
Other
18 stars 6 forks source link

facilitate logging into all betfair jurisdictions #16

Closed gamblebkb closed 7 years ago

gamblebkb commented 7 years ago

hi! can you make changes to let users access other betfair exchanges (.it, .es, .ro), alongside the global one (.com)? I've looked through the code, but I'm not really an advanced R user to do it myself without errors. I did add a jur argument to bf_login function, that changes the endpoint string (this is the only difference between jurisdictions), with "com" as default value, then making necessary changes to bf_account and betfair. this let me login no problem, but can't use the other methods. lol.

durtal commented 7 years ago

Hi @gambleBKB ,

I think the bf_post function would require changing slightly to account for the different jurisdictions (.it, .es, .ro instead of .com), but will have to double check.

Can you point me to any Betfair API documentation you looked at when trying to fix yourself?

I'll try have a look this week and let you know.

Cheers.

durtal commented 7 years ago

This page shows how to login to Italian exchange however it does say

Once a session token has been obtained via the .it login methods above, any further API requests should be sent to the UK Exchange endpoints indicated below.

But I'll have to do some more reading

gamblebkb commented 7 years ago

The betting/account endpoints are the same for all exchanges - italian/spanish/romanian/global, bar australian exchange (this was fully retired alongside aus wallet on jan. 4th this year anyway).

You addressed the issue with aus endpoints in bf_post, but there is no need for the other exchanges (nor aus now) since there is only one for betting API and one for accounts API.

The difference is URL definition when using login method. So, it should be like this: you login using the relevant API endpoint (bf_login) - which is different, you obtain a session token, then carry on making API requests to the same global exchange, as the page you mentioned says.

So, as I see it, changes must be made only to functions that require a login in order to get the related session token.

When I tried to test my fix, R wouldn't let me load the package - some error with NAMESPACE file. But making a POST request to login method is successful. The insides of building a pckg is chinese for me.

durtal commented 7 years ago

Thanks. I'll try take a look tonight.

I'm not sure I'll be able to test it myself (never tried to login to different jurisdiction) so I may create a new branch and see if you can try install and test that version.

durtal commented 7 years ago

Hi @gambleBKB,

I've added a new branch attempting to resolve this issue called jurisdictions with this commit as a first attempt.

Can you try installing it with the following:

devtools::install_github(repo = "durtal/betfaiR", ref = "jurisdictions")

I have tried to login to the Italian, Spanish and Romanian exchanges, I've not added more jurisdictions as I couldn't find them in the API documentation (if you can point out others let me know). I managed to login and didn't get any errors when running some very simple queries, as below:

library(betfaiR)
# login as normal, change jurisdiction from 'default', to 'spain', 'italy' or 'romania'
bf <- betfair(usr = 'username', pwd = 'password', key = 'api_key', jurisdiction = 'spain')
# looking for events
bf$events()
# or for 5 football markets
bf$marketCatalogue(filter = marketFilter(eventTypeIds = 1), maxResults = 5)

Let me know if this works for you and I'll merge with the master branch. I also removed the aus endpoint thanks for highlighting!

gamblebkb commented 7 years ago

In bf_login, at line 32, must be resp <- httr::POST(url = url, instead of resp <- httr::POST(url = "https://identitysso.betfair.com/api/login",, to let switch() do its job.

Also, in bf_account there needs to be added a jurisdiction argument - line 27 into bf_account <- function(usr, pwd, key, jurisdiction) { and line 30 into ssoid <- bf_login(usr = usr, pwd = pwd, key = key, jurisdiction = jurisdiction). Following this, we need to change bf_methods - line 110 into key = ssoid$usr$key, and adding a new line below jurisdiction = jurisdiction), to be able to use account methods.

I think that's all. I'm a github noob so I don't really know how to contribute; need to research it.

durtal commented 7 years ago

Sorry about that - sloppy from me! You pick up chinese fast :wink:

Have just pushed another update to same branch, this time when I try and log in to those jurisdictions I get errors - which is what I was expecting tbh:

> es <- betfair(Sys.getenv("bf_usr"), Sys.getenv("bf_pwd"), Sys.getenv("bf_key"), jurisdiction = "spain")
Warning message:
In bf_login(usr = usr, pwd = pwd, key = key, jurisdiction = jurisdiction) :
  Login failed: NOT_AUTHORIZED_FOR_DOMAIN_ES
> it <- betfair(Sys.getenv("bf_usr"), Sys.getenv("bf_pwd"), Sys.getenv("bf_key"), jurisdiction = "italy")
Warning message:
In bf_login(usr = usr, pwd = pwd, key = key, jurisdiction = jurisdiction) :
  Login failed: NOT_AUTHORIZED_FOR_DOMAIN_IT
> ro <- betfair(Sys.getenv("bf_usr"), Sys.getenv("bf_pwd"), Sys.getenv("bf_key"), jurisdiction = "romania")
Warning message:
In bf_login(usr = usr, pwd = pwd, key = key, jurisdiction = jurisdiction) :
  Login failed: NOT_AUTHORIZED_FOR_DOMAIN_RO

If this doesn't work for you then I might have to get you to go through a request step by step to see where it all goes pear shaped.

Cheers

gamblebkb commented 7 years ago

It works now. Account methods too. These are the only jurisdictions. Thanks! GG

durtal commented 7 years ago

Excellent - have deleted jurisdictions branch and merged back into master. So install as normal:

devtools::install_github("durtal/betfaiR")

If you need anything else (or have suggestions for possible improvements) let me know.

Cheers