Remove this ad
avatar

askee

rookie botter

Posts: 28 Member Since: November 3, 2008

Lead

November 3, 2008 22:06:23

Tags : :

Hi all,

I've decided to learn a programming language and went with java in the end. I'm looking at starting a small bot project up to monitor my progress and incorperate new ideas as i learn them (I find having an active intrest in a subject stimulates learning). However I have come across the unsightly place that is the betfair API website. The documentation there is completely useless and does nothing but confuse me even the sample java bot is useless as its poorly documentated. I was hoping you guys could point me to a web site that better explains a very simple java implementation of the betfair api. Hopefully somethin like a login and show balance kind of thing. This would really help me understand the whole web service call area which is where i'm really having a hard time.

Anyway cheers for all the good ideas and hopefully future help guys.

Jay

Quote    Reply   
Remove this ad
Remove this ad
avatar

birchy

Betfair Elite

Posts: 591 Member Since:May 11, 2008

#2 [url]

November 4, 2008 18:42:07

Stefan is correct. You do not need to learn how to use the Betfair API. For a nominal fee, you can rent access to a purpose built library . Stefan offers such a service.Money mouth

Personally, i prefer the world of all things free and learning how to use the API is a good learning curve that significantly imroves your understanding of how the internet works. I'm not a Java expert but look into methods of importing the WSDL file as this will often auto-generate the code for you.

A quick Google has given me http://teaching.cs.uml.edu/~heines/tools/JRun4/docs/html/Programmers_Guide/ws_wsdl5.html which looks useful. You can also achieve auto code generation with Netbeans, and maybe Eclipse (not sure??). I know there are a few Java programmers on this site so i'm surprised they've not stepped forward....

www.bespokebots.com

"This time next year Rodney, we'll be millionaires!"

Quote    Reply   
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#4 [url]

November 4, 2008 21:09:17

cheers birchy,

i'm of the same mind as you. My main effort is not to produce a bot but to greatly improve my programming and increase my understanding of all things oo. I'm currently using netbeans and have now got an extremely extremely simple login working with a single getaccountfunds call. I was hoping i could post links to my simple code which i will well document as a start point for other extreme beginers and in the hope you guys can look at it and teach me a few things :)

cheers again

Jay

Quote    Reply   
avatar

birchy

Betfair Elite

Posts: 591 Member Since:May 11, 2008

#5 [url]

November 4, 2008 22:13:52

I've since discovered that you need the Eclipse JEE version in order to have the web services option:

http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/ganymedesr1

I'm also a complete noob to Java, although i've tried it before (see BDP forum, i have several posts there) but wasn't exactly enthralled by the language. I still like the idea of writing multi platform code and i guess that i'll adopt Java when i get my head around all these dots and stupidly long names.

I might write a simple Login tutorial for Eclipse users...IF i ever get my login to work. Embarassed

www.bespokebots.com

"This time next year Rodney, we'll be millionaires!"

Quote    Reply   
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#6 [url]

November 4, 2008 22:20:08

well ive just got my login and get balance working well and would be hapy to send it to you well documented so you can take a look. As i said im no guru but im learning slow and sure.

Quote    Reply   
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#8 [url]

November 4, 2008 23:00:57

package betfairbasic;

import com.betfair.publicapi.v3.bfglobalservice.*;
import com.betfair.publicapi.types.global.v3.*;
import com.betfair.publicapi.v5.bfexchangeservice.*;
import com.betfair.publicapi.types.exchange.v5.*;

public class Bot
{
BFGlobalService_Service gService;
BFGlobalService globalServ;
BFExchangeService_Service eService;
BFExchangeService exchangeServ;

TokenStore store;

double availBalance;

Bot()
{
gService = new BFGlobalService_Service();
globalServ = gService.getBFGlobalService();
eService = new BFExchangeService_Service();
exchangeServ = eService.getBFExchangeService();

store = new TokenStore();

logIn();

availBalance = getAvailableBalance();

System.out.println(Double.toString(availBalance));

}

public void logIn()
{
String userName = "username";
String passWord = "password";
final int productId = 82;

LoginReq request = new LoginReq();

request.setUsername(userName);
request.setPassword(passWord);
request.setProductId(productId);

LoginResp responce = globalServ.login(request);

store.setToken(responce.getHeader().getSessionToken());
}

public double getAvailableBalance()
{
GetAccountFundsReq request = new GetAccountFundsReq();
com.betfair.publicapi.types.exchange.v5.APIRequestHeader theHeader = Headers.makeHeader();
theHeader.setSessionToken(store.getToken());
request.setHeader(theHeader);

GetAccountFundsResp responce = exchangeServ.getAccountFunds(request);
store.setToken(responce.getHeader().getSessionToken());

return responce.getAvailBalance();

}

}

Quote    Reply   
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#10 [url]

November 4, 2008 23:53:01

package betfairbasic;
/**
* the below import the necessary the exchange classes into the package allowing
* me to access them directly. *
*/
import com.betfair.publicapi.v3.bfglobalservice.*;
import com.betfair.publicapi.types.global.v3.*;
import com.betfair.publicapi.v5.bfexchangeservice.*;
import com.betfair.publicapi.types.exchange.v5.*;

/**
* This is the actual bot which is instanced from the main method in the Main
* class. I hope this is in keeping with an object orientated approach.
*
*/
public class Bot
{
/**
* Instance variables declared here
*/
BFGlobalService_Service gService;
BFGlobalService globalServ;
BFExchangeService_Service eService;
BFExchangeService exchangeServ;

TokenStore store;

double availBalance;

/**
* The constructor below is automaticly called at creation of any instance
* of a Bot object. It assignes objects to the instance variables declared
* in the Bot class.
*/
Bot()
{
// makes an instance of the global service.
gService = new BFGlobalService_Service();
// uses the above instance to create a local proxy of all the global service methods.
globalServ = gService.getBFGlobalService();
//makes an instance of the exchange service.
eService = new BFExchangeService_Service();
//uses the above instance to create a local proxy of all the exchange methods.
exchangeServ = eService.getBFExchangeService();

/**
* creates a TokenStore object to hold all the session token data for the Bot object.
* Then allows the new object to be accessed by the reference type variable store.
*/
store = new TokenStore();

//calls the login method.
logIn();

//sets the availBalance variable to the responce of the getAvailableBalance method call.
availBalance = getAvailableBalance();
/**
* prints the value held by the availBalance instance variable to output converting
* the double type to a string type.
*/
System.out.println(Double.toString(availBalance));

}

/**
* the login method will as the names states login the bot to the api as well as
* retrieving and setting the session token from the returned header to the TokenStore
* object instanced in the constructor and referenced via the store instanve variable
*/
public void logIn()
{
String userName = "username";
String passWord = "password";
final int productId = 82;

//creates a new login request object
LoginReq request = new LoginReq();

//sets the login requests instance variables.
request.setUsername(userName);
request.setPassword(passWord);
request.setProductId(productId);

/**
* creates a login responce referenced by the variable name responce
* then assignes the result of the gService.getBFGlobalService().login(request)
* call to it
*/
LoginResp responce = globalServ.login(request);

/**
* passes the session token recieved in the header of the login responce from the api
* to the setToken method of the Token Store object referenced by the variable store
*/
store.setToken(responce.getHeader().getSessionToken());
}

/**
* gets available balance from the betfair server and returns it.
*
*/
public double getAvailableBalance()
{
//creates a new getaccount funds request and assigns it the reference 'request'.
GetAccountFundsReq request = new GetAccountFundsReq();
/**
* creates a new header and assigns it the reference 'theHeader' then calls the
* makeHeader method of the Headers class to build the header.
*/
com.betfair.publicapi.types.exchange.v5.APIRequestHeader theHeader = Headers.makeHeader();
//assigns the session token stored in 'store' to the header created above
theHeader.setSessionToken(store.getToken());
//assigns the header created above to the GetAccountFundsReq instance variable via the setHeader method
request.setHeader(theHeader);

/**
* creates a getaccountfundsresponce object and assigns it the reference responce.
* then populates its atributes via the getaccountfunds method call.
*/
GetAccountFundsResp responce = exchangeServ.getAccountFunds(request);
store.setToken(responce.getHeader().getSessionToken());

//returns the accounts available balance.
return responce.getAvailBalance();

}

}

Quote    Reply   
Remove this ad
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#11 [url]

November 4, 2008 23:54:33

hope its helpful if anyone has any suggestions please let me know as this is my first ever attempt at a large program let alone a bot.

cheers all

jay

Quote    Reply   
avatar

bighands

rookie botter

Posts: 8 Member Since:November 4, 2008

#12 [url]

November 5, 2008 00:08:23

askee - if your using eclipse can you zip up you project and send ? Would be alot easier to help that way (rather than Copy paste). OBVIOUSLY remove any logic ;-)

Quote    Reply   
avatar

birchy

Betfair Elite

Posts: 591 Member Since:May 11, 2008

#13 [url]

November 5, 2008 00:19:26

ASKEE:
Thanks for that, i'll have a dabble with it.

BIGHANDS:
I'm planning on writing a step by step tutorial for Eclipse. It will only be a basic guide, but will show complete JAVA noobs (like me), how to setup this darn WSDL stuff and work with the auto-generated code. My previous experience was in VB6 and i hard coded each XML string...which was actually a nice learning curve and gave me enough experience to dump the free API and move onto scraping the website instead. Course, now that i'm a Ubuntu convert, VB6 aint much use unless i start hacking about in WINE... Laughing

www.bespokebots.com

"This time next year Rodney, we'll be millionaires!"

Quote    Reply   
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#14 [url]

November 5, 2008 00:20:37

bighands im using netbeans and what you see there iss basicly most of my code obviously there are some other classes like the header, and main classes that kick everything off but as i said im extremely new eg started tonight and that is all ive got so far.

Im more than happy to send you the netbeans project though. not sure if its easy to pull into eclipse as ive never used it.

Quote    Reply   
avatar

bighands

rookie botter

Posts: 8 Member Since:November 4, 2008

#15 [url]

November 5, 2008 00:35:13

sorry all - java born and bred (with a little c sharp experience that i want to never repeat). I've got the basic log in log out working, but I don't have a partcularly advanced session token yet so not sure I can be bothered to develop one. If you want my code to get you started let me kow (will take a week or so to get together). but think an open surce platform could be a nice new addittion to the forum.

Quote    Reply   
avatar

austinpodhorzer

bot addict

Posts: 131 Member Since:May 11, 2008

#17 [url]

November 5, 2008 01:49:45

One hint askee ... start using interfaces right away to abstract the main concepts away from the specifics of your implementation against the betfair API. Ideally you should abstract away from anything betfair specific at all so that when you come to expand your botting to other exchanges life will be a LOT easier.
All of your high level code such as your main bot class, selection logic classes, staking plan classes etc. should operate against interfaces, not concrete implementations. I'd say you ought to be writing almost as many interfaces as classes in the early stages of development. Doing this early will pay huge dividends in the future.

Quote    Reply   
avatar

birchy

Betfair Elite

Posts: 591 Member Since:May 11, 2008

#18 [url]

November 5, 2008 02:08:58

I'm having some weird problems. For instance: import com.betfair.publicapi.v3.bfglobalservice.*; is not available in Eclipse, despite the fact that i've successfully created a web service from the WSDL. The Eclipse equivalent seems to be import com.betfair.www.publicapi.v3.BFGlobalService.*;. Also, i can declare the variable BFGlobalService_Service serv; but am unable to initiate it. My understanding of Java is weak but i don't understand why BFGlobalService_Service serv = new BFGlobalService_Service(); will not work. Eclipse complains that it cannot instaniate BFGlobalService_Service, yet it will accept BFGlobalService_Service serv; without complaining??

My code shows no errors in the Eclipse IDE, but returns a NullPointerException which i believe is due to the fact that serv has not been set as an object...

package betfairapi;

import com.betfair.www.publicapi.types.global.v3.*;
import com.betfair.www.publicapi.v3.BFGlobalService.*;

public class BetfairAPI {
    BFGlobalService_Service serv;
    
    public String Login(String username, String password) {
        try {
           
            BFGlobalService_PortType port = serv.getBFGlobalService();
            LoginReq req = new LoginReq();
            LoginResp resp = new LoginResp();
           
            // setup the header info
            req.setProductId(82); // free api
            req.setUsername(username);
            req.setPassword(password);
           
            // send the request
            resp = port.login(req);
           
            // return the server response
            return resp.getErrorCode().toString();
        }
        catch(Exception e) {
            return e.toString();
        }
    }
}

www.bespokebots.com

"This time next year Rodney, we'll be millionaires!"

Quote    Reply   
avatar

askee

rookie botter

Posts: 28 Member Since:November 3, 2008

#19 [url]

November 5, 2008 08:41:42

@ austinpodhorzer,
hi thankyou for the suggestion I will look into interfaces' as ive not come across them yet in my java course. But i think i get what you mean and yes it makes a lot of sence thank you.

@ Birchy
Hi birchy firstly i really recommend netbeans apparently since version 6 it is on par with eclipse and some would say better but i know ide choice is a personal thing.

I looked through your code and appart from the not being able to initilise the serv instance variable it looks ok. Although I'm not sure about the line
'BFGlobalService_PortType port = serv.getBFGlobalService();'
as i dont know why yours has the extra _PortType that mine dosent require.

Apart from that, are you creating an instance of the BetfairAPI class from the main method entry point when you run your program? As the class is not declared abstract it requires an instance to exist for the variable serv to be created. Then you would use a constructor to initialise the instance variables within the class eg serv and all should work.

I think as i said im very new.

i believe your main entry point should have something like

BetfairAPI bot = new BetfairAPI();

this would then create an object call bot that held all the instance variables including serv.

unless you dont want to create objects from your calss in which case i think you need the abstract keyword in you BetfairAPI class statement.

hope this helps (sorry if its a little confusing)

Jay

Quote    Reply   
avatar

birchy

Betfair Elite

Posts: 591 Member Since:May 11, 2008

#20 [url]

November 5, 2008 12:36:18

Well i have:

    public static void main(String[] args) {
        BetfairAPI oAPI = new BetfairAPI();
        String s = oAPI.Login("username", "password");
       
        System.out.println(s);       
    }

 Which should be fine? The reason for me having is because it appears to me that serv.getBFGlobalService() returns the data type BFGlobalService_PortType, although i've probably misunderstood that. I previously tried Netbeans 6.01 which was part of the reason why i gave up on Java (again). The "betbirchy" posts in the BDP forum are mine...

www.bespokebots.com

"This time next year Rodney, we'll be millionaires!"

Quote    Reply   
Remove this ad
Add Reply

Quick Reply

bbcode help