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.
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.
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...)
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
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:
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
you will see a line
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:
Now, underneath all that, you should have a closing brace, followed by a little block of code which looks like this:
Highlight the '//TODO...' line and hit backspace, we don't need it right now. Replace it with the following:
Now, directly beneath that, type the following:
Now, go back up to where the instance variables are (where it says 'private String username;' and so on) and add the following variable:
In the constructor - where we initialised the username and password variables - add the following line:
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:
______________________
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
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);
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);
Code:
System.out.println("Result = " + result);
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;
Code:
catch (Exception ex)
{
// TODO handle custom exceptions here
}
Code:
ex.printStackTrace();
Code:
return log;
Code:
private LoginResp login;
Code:
this.login = this.doLogin();
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;
}
}
Total Comments 0
Comments
Recent Blog Entries by ThanksFish
- Timers and the like (05-05-2009)
- Logging out - it's the polite thing to do. (04-05-2009)
- Concurrently adding and removing components (03-07-2009)
- Using properties and settings in Java (02-18-2009)
- Logging into Betfair using Netbeans pt II(c) (02-09-2009)







