RejectedSoftware Forums

Sign up

Accessing Request and Response objects from automatically generated Rest interface

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!

Re: Accessing Request and Response objects from automatically generated Rest interface

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.

Re: Accessing Request and Response objects from automatically generated Rest interface

Lovely, thanks. Specifically I was looking for the session object, so using this approach I should be able to access it just fine.

Thank you!

On Sun, 28 Feb 2016 10:57:04 GMT, Sönke Ludwig wrote:

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.