or rescript example: mvn exec:java -Dexec.mainClass="com.betfair.aping.ApiNGDemo" -Dexec.args="myAppKey mySessionToken json-rpc"
Code SnippetCreating the request - JSON-RPC Code Block |
---|
| String requestString;
//Handling the JSON-RPC request
JsonrpcRequest request = new JsonrpcRequest();
request.setId("1");
request.setMethod(ApiNGDemo.getProp().getProperty("SPORTS_APING_V1_0") + operation);
request.setParams(params);
requestString = JsonConverter.convertToJson(request);
if(ApiNGDemo.isDebug())
System.out.println("\nRequest: "+requestString);
//We need to pass the "sendPostRequest" method a string in util format: requestString
HttpUtil requester = new HttpUtil();
return requester.sendPostRequestJsonRpc(requestString, operation, appKey, ssoToken); |
Creating the request - Rescript Code Block |
---|
| String requestString;
//Handling the Rescript request
params.put("id", 1);
requestString = JsonConverter.convertToJson(params);
if(ApiNGDemo.isDebug())
System.out.println("\nRequest: "+requestString);
//We need to pass the "sendPostRequest" method a string in util format: requestString
HttpUtil requester = new HttpUtil();
String response = requester.sendPostRequestRescript(requestString, operation, appKey, ssoToken);
if(response != null)
return response;
else
throw new APINGException(); |
Calling API-NG Code Block |
---|
| String jsonRequest = param;
HttpPost post = new HttpPost(URL);
String resp = null;
try {
post.setHeader(HTTP_HEADER_CONTENT_TYPE, ApiNGDemo.getProp().getProperty("APPLICATION_JSON"));
post.setHeader(HTTP_HEADER_ACCEPT, ApiNGDemo.getProp().getProperty("APPLICATION_JSON"));
post.setHeader(HTTP_HEADER_ACCEPT_CHARSET, ApiNGDemo.getProp().getProperty("ENCODING_UTF8"));
post.setHeader(HTTP_HEADER_X_APPLICATION, appKey);
post.setHeader(HTTP_HEADER_X_AUTHENTICATION, ssoToken);
post.setEntity(new StringEntity(jsonRequest, ApiNGDemo.getProp().getProperty("ENCODING_UTF8")));
HttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, new Integer(ApiNGDemo.getProp().getProperty("TIMEOUT")).intValue());
HttpConnectionParams.setSoTimeout(httpParams, new Integer(ApiNGDemo.getProp().getProperty("TIMEOUT")).intValue());
resp = httpClient.execute(post, reqHandler);
} catch (UnsupportedEncodingException e1) {
//Do something
} catch (ClientProtocolException e) {
//Do something
} catch (IOException ioE){
//Do something
}
return resp;
|
Find Horse Racing event type id Code Block |
---|
| MarketFilter marketFilter;
marketFilter = new MarketFilter();
Set<String> eventTypeIds = new HashSet<String>();
System.out.println("1.(listEventTypes) Get all Event Types...\n");
List<EventTypeResult> r = jsonOperations.listEventTypes(marketFilter, applicationKey, sessionToken);
System.out.println("2. Extract Event Type Id for Horse Racing...\n");
for (EventTypeResult eventTypeResult : r) {
if(eventTypeResult.getEventType().getName().equals("Horse Racing")){
System.out.println("3. EventTypeId for \"Horse Racing\" is: " + eventTypeResult.getEventType().getId()+"\n");
eventTypeIds.add(eventTypeResult.getEventType().getId().toString());
}
}
|
Get next available horse races: Code Block |
---|
| System.out.println("4.(listMarketCataloque) Get next horse racing market in the UK...\n");
TimeRange time = new TimeRange();
time.setFrom(new Date());
Set<String> countries = new HashSet<String>();
countries.add("GB");
Set<String> typesCode = new HashSet<String>();
typesCode.add("WIN");
marketFilter = new MarketFilter();
marketFilter.setEventTypeIds(eventTypeIds);
marketFilter.setMarketStartTime(time);
marketFilter.setMarketCountries(countries);
marketFilter.setMarketTypeCodes(typesCode);
Set<MarketProjection> marketProjection = new HashSet<MarketProjection>();
marketProjection.add(MarketProjection.COMPETITION);
marketProjection.add(MarketProjection.EVENT);
marketProjection.add(MarketProjection.EVENT_TYPE);
marketProjection.add(MarketProjection.MARKET_DESCRIPTION);
marketProjection.add(MarketProjection.RUNNER_DESCRIPTION);
String maxResult = "1";
List<MarketCatalogue> marketCatalogueResult = jsonOperations.listMarketCatalogue(marketFilter, marketProjection, MarketSort.FIRST_TO_START, maxResult,
applicationKey, sessionToken);
|
Get list of runners in the market: Code Block |
---|
| System.out.println("5. Print static marketId, name and runners....\n");
printMarketCatalogue(marketCatalogueResult.get(0));
/**
* ListMarketBook: get list of runners in the market, parameters:
* marketId: the market we want to list runners
*
*/
System.out.println("6.(listMarketBook) Get volatile info for Market including best 3 exchange prices available...\n");
String marketIdChosen = marketCatalogueResult.get(0).getMarketId();
PriceProjection priceProjection = new PriceProjection();
Set<PriceData> priceData = new HashSet<PriceData>();
priceData.add(PriceData.EX_ALL_OFFERS);
priceData.add(PriceData.EX_BEST_OFFERS);
priceData.add(PriceData.EX_TRADED);
priceData.add(PriceData.SP_AVAILABLE);
priceData.add(PriceData.SP_TRADED);
//In this case we don't need these objects so they are declared null
OrderProjection orderProjection = null;
MatchProjection matchProjection = null;
String currencyCode = null;
List<String> marketIds = new ArrayList<String>();
marketIds.add(marketIdChosen);
List<MarketBook> marketBookReturn = jsonOperations.listMarketBook(marketIds, priceProjection,
orderProjection, matchProjection, currencyCode, applicationKey, sessionToken);
|
Place a bet: Code Block |
---|
| long selectionId = 0;
if ( marketBookReturn.size() != 0 ) {
Runner runner = marketBookReturn.get(0).getRunners().get(0);
selectionId = runner.getSelectionId();
System.out.println("7. Place a bet below minimum stake to prevent the bet actually " +
"being placed for marketId: "+marketIdChosen+" with selectionId: "+selectionId+"...\n\n");
List<PlaceInstruction> instructions = new ArrayList<PlaceInstruction>();
PlaceInstruction instruction = new PlaceInstruction();
instruction.setHandicap(0);
instruction.setSide(Side.BACK);
instruction.setOrderType(OrderType.LIMIT);
LimitOrder limitOrder = new LimitOrder();
limitOrder.setPersistenceType(PersistenceType.LAPSE);
//API-NG will return an error with the default size=0.01. This is an expected behaviour.
//You can adjust the size and price value in the "apingdemo.properties" file
limitOrder.setPrice(getPrice());
limitOrder.setSize(getSize());
instruction.setLimitOrder(limitOrder);
instruction.setSelectionId(selectionId);
instructions.add(instruction);
String customerRef = "1";
PlaceExecutionReport placeBetResult = jsonOperations.placeOrders(marketIdChosen, instructions, customerRef, applicationKey, sessionToken);
// Handling the operation result
if (placeBetResult.getStatus() == ExecutionReportStatus.SUCCESS) {
System.out.println("Your bet has been placed!!");
System.out.println(placeBetResult.getInstructionReports());
} else if (placeBetResult.getStatus() == ExecutionReportStatus.FAILURE) {
System.out.println("Your bet has NOT been placed :*( ");
System.out.println("The error is: " + placeBetResult.getErrorCode() + ": " + placeBetResult.getErrorCode().getMessage());
System.out.println("Sorry, more luck next time\n\n");
}
} else {
System.out.println("Sorry, no runners found\n\n");
} |
|