title: Batch HTTP requests in Play!Framework
body: <p>I have the current set of of routes implemented (for example):</p>
  
  <pre><code>GET     /api/:version/:entity               my.controllers.~~~~~
  GET     /api/:version/:entity/:id           my.controllers.~~~~~
  POST    /api/:version/:entity               my.controllers.~~~~~
  POST    /api/:version/:entity/:id           my.controllers.~~~~~
  DELETE  /api/:version/:entity               my.controllers.~~~~~
  
  POST    /api/:version/search/:entity        my.controllers.~~~~~
  </code></pre>
  
  <p>And they work beautifully. Now let's say I want to implement a "batch endpoint" for the same API. It should look something like this:</p>
  
  <pre><code>POST    /api/:version/batch                 my.controllers.~~~~~
  </code></pre>
  
  <p>and the body should look like this:</p>
  
  <pre><code>[
      {
          "method": "POST",
          "call": "/api/1/customer",
          "body": {
              "name": "antonio",
              "email": "tonysmallhands@gmail.com"
          }
      },
      {
          "method": "POST",
          "call": "/api/1/customer/2",
          "body": {
              "name": "mario"
          }
      },
      {
          "method": "GET",
          "call": "/api/1/company"
      },
      {
          "method": "DELETE",
          "call": "/api/1/company/22"
      }
  ]
  </code></pre>
  
  <p>To do that I would like to know how I can call the play framework router to pass those requests? I was planning  to use something similar as what is advised for unit tests:</p>
  
  <pre><code>@Test
  public void badRoute() {
    Result result = play.test.Helpers.routeAndCall(fakeRequest(GET, "/xx/Kiki"));
    assertThat(result).isNull();
  } 
  </code></pre>
  
  <p>by going into the source code of <code>routeAndCall()</code>, you find something like this:</p>
  
  <pre><code> /**
   * Use the Router to determine the Action to call for this request and executes it.
   * @deprecated
   * @see #route instead
   */
  @SuppressWarnings(value = "unchecked")
  public static Result routeAndCall(FakeRequest fakeRequest) {
      try {
          return routeAndCall((Class&lt;? extends play.core.Router.Routes&gt;)FakeRequest.class.getClassLoader().loadClass("Routes"), fakeRequest);
      } catch(RuntimeException e) {
          throw e;
      } catch(Throwable t) {
          throw new RuntimeException(t);
      }
  }
  
  /**
   * Use the Router to determine the Action to call for this request and executes it.
   * @deprecated
   * @see #route instead
   */
  public static Result routeAndCall(Class&lt;? extends play.core.Router.Routes&gt; router, FakeRequest fakeRequest) {
      try {
          play.core.Router.Routes routes = (play.core.Router.Routes)router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null);
          if(routes.routes().isDefinedAt(fakeRequest.getWrappedRequest())) {
              return invokeHandler(routes.routes().apply(fakeRequest.getWrappedRequest()), fakeRequest);
          } else {
              return null;
          }
      } catch(RuntimeException e) {
          throw e;
      } catch(Throwable t) {
          throw new RuntimeException(t);
      }
  }
  </code></pre>
  
  <p>So my question is: Is there a less "hacky" way to do this with Play (I am not against mixing Scala and Java to get to it) than to copy the above code? I would have also like to give the option of executing the batched calls in parallel or in sequence ... I guess instantiating only one <code>Routes</code> using the class loader would be problematic then?</p>