forums Albums groups
Go Back   Betfair Developers Program Forum > Blogs > Java and the Betfair API
Open A Betfair Account
 Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Since I mainly access the API using Java, thats what I'll be blogging about. I am most definitely not an expert java programmer, but I am getting very good at reading the Javadoc.

If I post something which is incorrect or something which could be made more efficient somehow, post a comment. I really don't have a problem with criticism of any kind. Even cruel criticism has something to be learned from and since you get a finite number of days allocated to you, any day you don't learn something is one of those days wasted.
Rate this Entry

Logging into Betfair using Netbeans pt II(b)

Posted 02-09-2009 at 07:29 PM by ThanksFish
Updated 02-09-2009 at 07:44 PM by ThanksFish (Tagging...)
Part two finishes the class off and shows the completed class at the end.

______________________

In the code which has just been created for you, find the lines

Code:
com.betfair.publicapi.types.global.v3.LoginReq request = new com.betfair.publicapi.types.global.v3.LoginReq();
            // TODO process result here
Highlight the '// TODO...' line and hit delete, we don't need that bit.

Now, the request is what we are sending, the result is what we want. If you have spent any time looking at the API documentation, you will see there are parameters which we have to supply in the request in order to get logged in. So, hit return once and type the following:
Code:
request.setProductId(this.prodID);
 request.setPassword(String.valueOf(this.password));
 request.setUsername(this.username);
What you will have noticed while you were doing that is that netbeans gives you really big hints as to what it thinks you are going to type next. This is incredibly useful, however sometimes it gets things wrong, so if you take its advice, make sure it puts the correct variable in place. For example, when I typed 'request.setPassword()' it tried to put the username in between the brackets. Obviously, this wouldn't have worked. Use the hinting, but keep an eye on it.

Ok, the next line you should see is the one which actually does the work.

What this line does is create the LoginResp object by placing the request object into the port object created earlier. Since the port object was made with the BFGlobalService_Service object, we have a clear process which shows us how the login works. The BFGlobalService_Service object is the object which actually handles the connection to the API. The port object opens a sort of window into the BFGlobalService_Service object, which will only accept objects of type LoginReq. When you are making other calls, like MarketPricesReq and so on, the port will be configured to only take objects shaped like those calls. Once you get yourself up and running properly, you will find yourself able to take advantage of this system, by creating an interface to the port object. Look up Java Interfaces if you don't know what I am talking about here - they are extremely useful things.

Directly below the line
Code:
com.betfair.publicapi.types.global.v3.LoginResp result = port.login(request);
you will see a line
Code:
System.out.println("Result = " + result);
which is useful for knowing whether or not you actually managed to connect to the API, but not terribly useful for anything else. So we will sort ourselves out with something else to let us know what has happened.

In order to do that, we have to interrogate the LoginResp object. Directly underneath that last line of code, put the following in:

Code:
            System.out.println("Logged in: " + result.getErrorCode().value());
            System.out.println("Currency: " + result.getCurrency());
            System.out.println("Time of request: " + result.getHeader().getTimestamp().toString());
            System.out.println("SessionToken: " + result.getHeader().getSessionToken());
            if (result.getErrorCode().value().equals("OK"))
            {
                this.loggedIn = true;
            }
            log = result;
Now, underneath all that, you should have a closing brace, followed by a little block of code which looks like this:

Code:
        catch (Exception ex)
        {
            // TODO handle custom exceptions here
        }
Highlight the '//TODO...' line and hit backspace, we don't need it right now. Replace it with the following:

Code:
ex.printStackTrace();
Now, directly beneath that, type the following:

Code:
return log;
Now, go back up to where the instance variables are (where it says 'private String username;' and so on) and add the following variable:

Code:
private LoginResp login;
In the constructor - where we initialised the username and password variables - add the following line:

Code:
this.login = this.doLogin();
Now, to finish off the Login class, we'll add some getters.

Underneath the last closing brace of the doLogin() method but above the final closing brace of the class, hit enter a couple of times to get some blank space.

Right click on one of those lines and choose 'Insert Code...'. This time, on the sub-menu, choose 'Getter...'. You will be presented with a dialog which shows the instance variables of the class. The ones we want to be able to get at are the boolean variable loggedIn and the LoginResp login. Put a tick in the boxes next to those variables and click the generate button.

Two small blocks of code are generated, with public access modifiers rather than private ones.

With that, the login class is finished. It should look a bit like this:

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wsops;

import com.betfair.publicapi.types.global.v3.LoginResp;

/**
 *
 * @author alan
 */
public class Login
{

    private String username;
    private char[] password;
    private final int prodID = 82;
    private boolean loggedIn;
    private LoginResp login;

    public Login(String username, char[] password)
    {
        this.username = username;
        this.password = password;
        this.login = this.doLogin();

    }

    private LoginResp doLogin()
    {
        LoginResp log = new LoginResp();


        try
        { 
            com.betfair.publicapi.v3.bfglobalservice.BFGlobalService_Service service = new com.betfair.publicapi.v3.bfglobalservice.BFGlobalService_Service();
            com.betfair.publicapi.v3.bfglobalservice.BFGlobalService port = service.getBFGlobalService();
            
            com.betfair.publicapi.types.global.v3.LoginReq request = new com.betfair.publicapi.types.global.v3.LoginReq();
            
            request.setProductId(this.prodID);
            request.setPassword(String.valueOf(this.password));
            request.setUsername(this.username);
            
            com.betfair.publicapi.types.global.v3.LoginResp result = port.login(request);
            
            System.out.println("Result = " + result);
            System.out.println("Logged in: " + result.getErrorCode().value());
            System.out.println("Currency: " + result.getCurrency());
            System.out.println("Time of request: " + result.getHeader().getTimestamp().toString());
            System.out.println("SessionToken: " + result.getHeader().getSessionToken());
            if (result.getErrorCode().value().equals("OK"))
            {
                this.loggedIn = true;
            }
            log = result;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return log;
    }

    public boolean isLoggedIn()
    {
        return loggedIn;
    }

    public LoginResp getLogin()
    {
        return login;
    }
    
    
}
Posted in Java, Betfair API
Comments 0 Email Blog Entry
Total Comments 0

Comments

 

All times are GMT. The time now is 03:05 PM.

BETFAIR® and the BETFAIR LOGO are registered trade marks of The Sporting Exchange Limited. Data on Betfair website(s) (including pricing data) is protected by © and database rights. It may not be used for any purpose without a licence. © The Sporting Exchange Limited. All rights reserved.