Posted Sun, 28 Feb 2016 10:57:04 GMT in reply to
Andrew Chapman
Reply
On Sun, 28 Feb 2016 10:29:44 GMT, Andrew Chapman wrote:
Hi guys, just wondering how I can access Vibe.d's HTTPServerRequest and HTTPServerResponse objects from an automatically generated Rest API method.
E.g.
interface UserAPI {
EntityUser getUser(BsonObjectID id);
}
...
EntityUser getUser(BsonObjectID id) {
// How do I access request / response objects here?
}
Thanks!
The idea is that this should be a last resort approach and thus it's not directly possible. However, you can use the @before
annotation to access them and return an arbitrary value (could even be the request/response objects themselves):
T extractT(HTTPServerRequest req, HTTPServerResponse res)
{
// ... the return value will be passed to the data parameter of getUser ...
}
interface UserAPI {
@before!extractT("data")
getUser(BsonObjectID id, T data)
}
If you can, however, you should prefer a semantically more explicit annotation, such as @headerParam("data", "My-Header")
in conjunction with a string data
parameter. This will then also work correctly when using the RestInterfaceClient
, while @before
will require hand crafted code to set up the request object.