public HttpResponse sendBulkRequest ( Iterable < PiwikRequest > requests , String authToken ) throws IOException { if ( authToken != null && authToken . length ( ) != PiwikRequest . AUTH_TOKEN_LENGTH ) { throw new IllegalArgumentException ( authToken + " is not " + PiwikRequest . AUTH_TOKEN_LENGTH + " characters long." ) ; } JsonObjectBuilder ob = Json . createObjectBuilder ( ) ; JsonArrayBuilder ab = Json . createArrayBuilder ( ) ; for ( PiwikRequest request : requests ) { ab . add ( "?" + request . getQueryString ( ) ) ; } ob . add ( REQUESTS , ab ) ; if ( authToken != null ) { ob . add ( AUTH_TOKEN , authToken ) ; } HttpClient client = getHttpClient ( ) ; HttpPost post = new HttpPost ( uriBuilder . build ( ) ) ; post . setEntity ( new StringEntity ( ob . build ( ) . toString ( ) , ContentType . APPLICATION_JSON ) ) ; try { return client . execute ( post ) ; } finally { post . releaseConnection ( ) ; } }
Send multiple requests in a single HTTP call . More efficient than sending several individual requests . Specify the AuthToken if parameters that require an auth token is used .