Skip to content

Commit 40189d1

Browse files
author
Dasmeet Doowa
committed
perl json-rpc client
1 parent 9394cc8 commit 40189d1

2 files changed

Lines changed: 177 additions & 0 deletions

File tree

perl/ApiNgDemoJsonRpc.pl

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/perl
2+
#
3+
# Program to do the obvious
4+
#
5+
use JSON::RPC::LWP;
6+
use HTTP::Headers;
7+
use Data::Dumper;
8+
use JSON::RPC::Common::Procedure::Return;
9+
use DateTime;
10+
11+
my $totalArgs = $#ARGV + 1;
12+
if ($totalArgs != 2 ) { #check two arguments have been passed
13+
print "wrong number of arguments\n";
14+
print "usage: perl $0 <appKey> <sessionToken>\n";
15+
exit;
16+
}
17+
18+
#Get the appKey
19+
my $appKey = $ARGV[0];
20+
print "App key being used: $appKey\n";
21+
22+
#Get the session token
23+
my $sessionToken = $ARGV[1];
24+
print "Session token being used: $sessionToken\n";
25+
26+
#prepare the json-rpc client
27+
my $apiClient = JSON::RPC::LWP->new;
28+
my $headers = HTTP::Headers->new(
29+
"X-Application" => "$appKey",
30+
"X-Authentication" => "$sessionToken");
31+
$apiClient->ua->default_headers($headers);
32+
my $url="https://api.betfair.com/exchange/betting/json-rpc/v1";
33+
34+
#make a call to listEventTypes
35+
my @eventTypeResults = getEventTypes();
36+
37+
38+
#Extract the result and extract eventTypeId for horse racing.
39+
my $horseRacingEventId = getEventId('Horse Racing', @eventTypeResults);
40+
print "Got eventTypeId: $horseRacingEventId \n";
41+
#Get a merketCatalogue for the next GB WIN horse race market.
42+
my $marketCatalogue = getMarketCatalogueForNextGBWin($horseRacingEventId);
43+
#Extract the marketId from the catalogue
44+
my $marketId = $marketCatalogue -> {marketId};
45+
#Get and print prices for the runner
46+
my $marketBook = getMarketBook($marketId);
47+
print Dumper($marketBook);
48+
printPrices($marketBook);
49+
50+
#SelectionId of the firstRunner in the market
51+
my $selectionId = @{$marketCatalogue -> {runners}}[0] -> {selectionId};
52+
#place failing bet
53+
placeFailingBet($marketId, $selectionId);
54+
55+
56+
sub placeFailingBet{
57+
my ($marketId, $selectionId) = @_;
58+
my $placeExecutionReport = callAPI("SportsAPING/v1/placeOrders",
59+
{
60+
marketId => $marketId,
61+
instructions => [
62+
{
63+
selectionId => $selectionId,
64+
handicap => "0",
65+
side => "BACK",
66+
orderType => "LIMIT",
67+
limitOrder => {
68+
size => "0.01",
69+
price => "1.50",
70+
persistenceType => "LAPSE"
71+
}
72+
}
73+
],
74+
customerRef => "test1212121221212"
75+
}
76+
);
77+
print "Got execution report: \n";
78+
print Dumper($placeExecutionReport);
79+
80+
}
81+
82+
sub printPrices{
83+
my ($marketBook) = @_;
84+
my @runners = @{$marketBook -> {runners}};
85+
foreach my $runner (@runners){
86+
print "SelectionId of runner is: ", $runner -> {selectionId}, "\n";
87+
if ($runner -> {status} eq "ACTIVE") {
88+
print Dumper($runner -> {ex});
89+
} else {
90+
print "Runner is not active, current status is: ", $runner -> {status}, "\n";
91+
}
92+
}
93+
94+
}
95+
96+
sub getMarketBook {
97+
my (@marketIds) = @_;
98+
return @{
99+
callAPI("SportsAPING/v1/listMarketBook",
100+
{
101+
marketIds => \@marketIds,
102+
priceProjection => {
103+
priceData => ["EX_BEST_OFFERS"]
104+
}
105+
}
106+
)
107+
}[0];
108+
}
109+
110+
sub getMarketCatalogueForNextGBWin{
111+
my (@eventTypeIds) = @_;
112+
my $now=DateTime->now;
113+
return @{
114+
callAPI("SportsAPING/v1/listMarketCatalogue",
115+
{
116+
filter => {
117+
eventTypeIds => \@eventTypeIds,
118+
marketCountries => ["GB"],
119+
marketTypeCodes => ["WIN"],
120+
marketStartTime => {
121+
from => "$now"
122+
}
123+
},
124+
'sort' => 'FIRST_TO_START',
125+
maxResults => '1',
126+
marketProjection => ["RUNNER_METADATA"]
127+
}
128+
)
129+
}[0]; #we only asked for one market so grab the first one in the array response
130+
131+
132+
}
133+
134+
sub getEventId {
135+
my ($eventName, @eventTypeResults) = @_;
136+
for $eventTypeResult (@eventTypeResults) {
137+
if ($eventTypeResult -> {'eventType'} -> {'name'} eq $eventName) {
138+
return $eventTypeResult -> {'eventType'} -> {'id'};
139+
}
140+
}
141+
}
142+
143+
sub getEventTypes {
144+
return @{callAPI("SportsAPING/v1/listEventTypes",{filter => {}})};
145+
}
146+
147+
sub callAPI {
148+
my($method,$params) = @_;
149+
print "Calling api-ng method $method with params:\n";
150+
print Dumper($params);
151+
my $call = $apiClient->call($url, $method, $params);
152+
if ($call->has_error){
153+
handleError($call->error);
154+
}
155+
156+
return $call->result;
157+
}
158+
159+
sub handleError {
160+
my ($error) = @_;
161+
print Dumper($error);
162+
exit;
163+
}

perl/ReadMe.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Prequisites:
2+
Perl v5.14.2
3+
cpanm (to install perl dependencies) http://search.cpan.org/~miyagawa/App-cpanminus-1.7004/lib/App/cpanminus.pm#INSTALLATION
4+
5+
#for json-rpc demo
6+
sudo cpanm JSON::RPC::LWP
7+
8+
#for rest demo
9+
sudo cpanm REST::Client
10+
11+
#misc
12+
sudo cpanm DateTime
13+
14+

0 commit comments

Comments
 (0)