Batch HTTP requests in Play!Framework

こ雲淡風輕ζ 提交于 2020-01-11 07:30:20

问题


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?


回答1:


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/




回答2:


You can probably use WS API for that, but personally I'd just create a private methods for collecting data and use them from both - 'single' and also 'batch' actions - it will be faster for sure.



来源:https://stackoverflow.com/questions/22625538/batch-http-requests-in-playframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!