简体   繁体   中英

Batch HTTP requests in Play!Framework

I have the current set of of routes implemented (for example):

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.~~~~~

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:

POST    /api/:version/batch                 my.controllers.~~~~~

and the body should look like this:

[
    {
        "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"
    }
]

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:

@Test
public void badRoute() {
  Result result = play.test.Helpers.routeAndCall(fakeRequest(GET, "/xx/Kiki"));
  assertThat(result).isNull();
} 

by going into the source code of routeAndCall() , you find something like this:

 /**
 * 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<? extends play.core.Router.Routes>)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<? extends play.core.Router.Routes> 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);
    }
}

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 Routes using the class loader would be problematic then?

You can use the following method call to route your fake requests: Play.current.global.onRouteRequest . Please see this post for a full example: http://yefremov.net/blog/play-batch-api/

您可能可以为此使用WS API ,但就我个人而言,我只是创建一个私有方法来收集数据并从“单个”动作和“批处理”动作中使用它们,这肯定会更快。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM