Java and API-NG
This page contains some code snippets of Java interaction with API-NG. This example shows how to use Java to send requests to list the event types, find the next horse racing market and then placing a bet with an invalid stake to trigger an error. The code referred to here is available at https://github.com/betfair/API-NG-sample-code/tree/master/java.
...
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"); } |
Additional Error Handling Code - handling non HTTP 200 responses
The below guidance is for customers who are looking to handle additional HTTP responses other than the HTTP 200 success code when using Rescript requests.
1. In RescriptResponseHandler we need to modify the handleResponse method so that an HttpResponseException is thrown for unsuccessful response
Code Block | ||
---|---|---|
| ||
public class RescriptResponseHandler implements ResponseHandler<String> { private static final String ENCODING_UTF_8 = "UTF-8"; public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity,ENCODING_UTF_8); if (statusLine.getStatusCode() != 200 ) { throw new HttpResponseException(statusLine.getStatusCode(), responseString); } return entity == null ? null : responseString; } } |
2. In HttpUtil. sendPostRequest() we catch the HttpResponseException, convert the Json response and eventually throw an APINGException containing the error details:
Code Block | ||
---|---|---|
| ||
private String sendPostRequest(String param, String operation,
String appKey, String ssoToken, String URL, ResponseHandler<String>
reqHandler) throws APINGException {
String jsonRequest =
param;
..….
……
resp = httpClient.execute(post, reqHandler);
} catch
(UnsupportedEncodingException e1) {
//Do something
}
catch(HttpResponseException ex){
ResponseError container = JsonConverter.convertFromJson(ex.getMessage(), new
TypeToken<ResponseError>() {}.getType());
APINGException apingException=container.getDetail().getAPINGException();
if(apingException==null){
apingException= new APINGException();
apingException.setErrorCode(container.getFaultcode());
apingException.setErrorDetails(container.getFaultstring());
}
throw apingException;
}
catch
(ClientProtocolException e) {
//Do something
} catch (IOException
ioE){
//Do something
}
return resp;
} |
3. Notice that the error handling code from sendPostRequest uses a new class: ResponseError, which we are using to deserialize the error response
Code Block | ||
---|---|---|
| ||
public class ResponseError {
private Detail detail;
private String faultcode;
private String faultstring;
public String getFaultstring() {
return faultstring;
}
public void setFaultstring(String faultstring)
{
this.faultstring =
faultstring;
}
public String getFaultcode() {
return faultcode;
}
public void setFaultcode(String faultcode) {
this.faultcode =
faultcode;
}
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
}
public class Detail {
private APINGException APINGException;
public APINGException getAPINGException() {
return APINGException;
}
public void setAPINGException(APINGException
aPINGException) {
APINGException =
aPINGException;
}
|