PART 2 Continued......
The Full code is avalable at the bottom of this post
Step 13
So now we have 2 projects but TestForm1 still doesn't really know of the BetfairAPI projects
existence and so we add a reference to it so that the Betfair Class can be accessed from the
TestForm1 Project.
In Solution Explorer, Select the TestForm1 by left Clicking on it.
Then Right Click on TestForm1's "references" and select the "Add Reference" option.
This will pop up the "Add Reference" form, Slect the "Projects" Tab and hopefully you will
see the BetfairAPI project Sitting there ready for you. Select it and Click OK

if you now expand TestForms1's references you should see BetfairAPI sitting there like this

Step 14
Next I want you to add a button to testForm1 called butLogOn, And also create the click event.
I'm assuming everyone who has got this far knows how to do this.

Step 15
Now we can start Coding.
in the Form1.cs file
Add the following below the existing "using" statements
Code:
using System.Diagnostics; using BetfairAPI;
Add the following within the Form1 Class
Code:
public CBetfairAPI MyBetfair;
public bool bLoggedOn;
Change the Form1 Constructor to look like this
Code:
public Form1()
{
InitializeComponent();
MyBetfair = new CBetfairAPI();
bLoggedOn = false;
}
and then add the code for the ButLogOn_Click() event
Code:
private void ButLogOn_Click( object sender, EventArgs e )
{
string uuu = "YourUserName"; // Change as required
string ppp = "YourPassword";
if( bLoggedOn == false )
{
if( MyBetfair.Login( uuu, ppp ) == true )
{
bLoggedOn = true;
this.Text = "Logged On To Betfair";
}
else
{
this.Text = "Log on FAILED, Check Debug Messages";
}
}
}
So there we have it, Compile and run in debug mode and see what happens
If it worked then lets go on to enhancing the BetfairAPI class to add a LogOut() function
Step 19
Start off by Selecting the CBetfairAPI.cs file for editing in the BetfairAPI project.
And then create the LogOut() function below the Login() function
Code:
public bool LogOut()
{
bool bRetCode;
const string serviceName = "LogOut";
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1}", DateTime.Now, serviceName ) );
var request = new LogoutReq();
request.header = _globReqHdr;
LogoutResp response = _bfGlobal.logout( request );
bRetCode = CheckResponse( serviceName,
Convert.ToString( response.header.errorCode ),
Convert.ToString( response.errorCode ),
response.header.sessionToken );
if( bRetCode == false )
{
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1} - FAILED", DateTime.Now, serviceName ) );
return false;
}
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1} - OK", DateTime.Now, serviceName ) );
return true;
}
If you look closely and compare the Login() and LogOut() function you will see they are almost
exactly the same and follow the same format. The only major difference is that login() doesn't
require a 'Request Header' i.e. no session token, currency etc. where as all the other calls do,
and therefore in Logout() ( and all the other calls yet to be created) A request Header is assigned
the global ( or Exchange ) header which already contains a valid session token by:
"request.header = _globReqHdr;"
This 'Same Format' for each call is a good theme to maintain for almost every call to betfair. It does change a little for getting prices or placing bets but not much.
Keeping to similar formats enables quick intuitive programming which becomes easier to read, use, understand and maintain.
Step 20
Now go back and ammend the ButLogOn_Click() event code in TestForm1
Code:
private void ButLogOn_Click( object sender, EventArgs e )
{
string uuu = "YourUserName";
string ppp = "YourPassword";
if( bLoggedOn == false )
{
if( MyBetfair.Login( uuu, ppp ) == true )
{
bLoggedOn = true;
ButLogOn.Text = "Log Out";
this.Text = "Logged On To Betfair";
}
else
{
this.Text = "Log on FAILED, Check Debug Messages";
}
}
else
{
if( MyBetfair.LogOut() == true )
{
bLoggedOn = false;
ButLogOn.Text = "Log On";
this.Text = "NOT Logged On To Betfair";
}
else
{
this.Text = "Log Out FAILED, Check Debug Messages";
}
}
}
Compile, then run in debug mode, you will be able to log in and out of betfair to your
hearts content

Below is the Full Code For:
CBetfairAPI
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using BetfairAPI.BFGlobal;
using BetfairAPI.BFExchange;
namespace BetfairAPI
{
public class CBetfairAPI
{
#region Variables
private BFGlobalService _bfGlobal;
private BFExchangeService _bfExchange;
private BFGlobal.APIRequestHeader _globReqHdr;
private BFExchange.APIRequestHeader _exchReqHdr;
private string _username;
private string _password;
private int _productId;
private int _vendorSoftwareId;
private string _sessionToken;
private string _currency;
#endregion
#region Constuctor
public CBetfairAPI()
{
Debug.WriteLine( string.Format( "{0} - Initializing CBetfairAPI", DateTime.Now ) );
_bfGlobal = new BFGlobalService();
_bfGlobal.EnableDecompression = true;
_bfExchange = new BFExchangeService();
_bfExchange.EnableDecompression = true;
_globReqHdr = new BFGlobal.APIRequestHeader();
_exchReqHdr = new BFExchange.APIRequestHeader();
_username = "";
_password = "";
_sessionToken = "";
_productId = 82;
_vendorSoftwareId = 0;
_currency = "";
}
#endregion
public bool Login( string UserName, string PassWord )
{
bool bRetCode;
const string serviceName = "Login";
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1}", DateTime.Now, serviceName ) );
_username = UserName;
_password = PassWord;
var request = new LoginReq { username = _username,
password = _password,
productId = _productId,
vendorSoftwareId = _vendorSoftwareId };
LoginResp response = _bfGlobal.login( request );
bRetCode = CheckResponse( serviceName,
Convert.ToString( response.header.errorCode ),
Convert.ToString( response.errorCode ),
response.header.sessionToken );
if( bRetCode == false )
{
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1} - FAILED", DateTime.Now, serviceName ) );
return false;
}
_currency = response.currency;
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1} - OK", DateTime.Now, serviceName ) );
return true;
}
public bool LogOut()
{
bool bRetCode;
const string serviceName = "LogOut";
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1}", DateTime.Now, serviceName ) );
var request = new LogoutReq();
request.header = _globReqHdr;
LogoutResp response = _bfGlobal.logout( request );
bRetCode = CheckResponse( serviceName,
Convert.ToString( response.header.errorCode ),
Convert.ToString( response.errorCode ),
response.header.sessionToken );
if( bRetCode == false )
{
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1} - FAILED", DateTime.Now, serviceName ) );
return false;
}
Debug.WriteLine( string.Format( "{0} - CBetfairAPI - {1} - OK", DateTime.Now, serviceName ) );
return true;
}
private bool CheckResponse( string serviceName, string hdrErrCd, string respErrCd, string sessionToken )
{
if( ! string.IsNullOrEmpty( sessionToken ) )
{
_sessionToken = sessionToken;
_globReqHdr.sessionToken = sessionToken;
_exchReqHdr.sessionToken = sessionToken;
}
if( hdrErrCd != "OK" )
{
Debug.WriteLine( string.Format( "{0} - FAILED: Response.Header.ErrorCode = {1}", serviceName, hdrErrCd ) );
return ( false );
}
if( respErrCd != "OK" )
{
Debug.WriteLine( string.Format( "{0} - FAILED: Response.ErrorCode = {1}", serviceName, respErrCd ) );
return ( false );
}
return ( true );
}
}
}
Form1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using BetfairAPI;
namespace TestForm1
{
public partial class Form1 : Form
{
public CBetfairAPI MyBetfair;
public bool bLoggedOn;
public Form1()
{
InitializeComponent();
MyBetfair = new CBetfairAPI();
bLoggedOn = false;
}
private void ButLogOn_Click( object sender, EventArgs e )
{
string uuu = "YourUserName";
string ppp = "YourPassword";
if( bLoggedOn == false )
{
if( MyBetfair.Login( uuu, ppp ) == true )
{
bLoggedOn = true;
ButLogOn.Text = "Log Out";
this.Text = "Logged On To Betfair";
}
else
{
this.Text = "Log on FAILED, Check Debug Messages";
}
}
else
{
if( MyBetfair.LogOut() == true )
{
bLoggedOn = false;
ButLogOn.Text = "Log On";
this.Text = "NOT Logged On To Betfair";
}
else
{
this.Text = "Log Out FAILED, Check Debug Messages";
}
}
}
}
}












Leave a comment: