Function registerRestInterface
Registers a REST interface and connects it the the given instance
.
Each method of the given class instance
is mapped to the corresponing HTTP
verb. Property methods are mapped to GET/PUT and all other methods are
mapped according to their prefix verb. If the method has no known prefix,
POST is used. The rest of the name is mapped to the path of the route
according to the given method_style
. Note that the prefix word must be
all-lowercase and is delimited by either an upper case character, a
non-alphabetic character, or the end of the string.
The following table lists the mappings from prefix verb to HTTP verb:
HTTP method | Recognized prefixes |
---|---|
GET | get, query |
PUT | set, put |
POST | add, create, post |
DELETE | remove, erase, delete |
PATCH | update, patch |
If a method has its first parameter named 'id', it will be mapped to ':id/method' and 'id' is expected to be part of the URL instead of a JSON request. Parameters with default values will be optional in the corresponding JSON request.
Any interface that you return from a getter will be made available with the base url and its name appended.
Prototypes
URLRouter registerRestInterface(TImpl)(
URLRouter router,
TImpl instance,
RestInterfaceSettings settings = null
);
URLRouter registerRestInterface(TImpl)(
URLRouter router,
TImpl instance,
MethodStyle style
);
URLRouter registerRestInterface(TImpl)(
URLRouter router,
TImpl instance,
string url_prefix,
MethodStyle style = MethodStyle .lowerUnderscored
);
Parameters
Name | Description |
---|---|
router | The HTTP router on which the interface will be registered |
instance | Class instance to use for the REST mapping - Note that TImpl
must either be an interface type, or a class which derives from a
single interface |
settings | Additional settings , such as the MethodStyle, or the prefix.
See RestInterfaceSettings for more details. |
See Also
RestInterfaceClient
class for a seamless way to access such a generated API
Example
This is a very limited example of REST interface features. Please refer to the "rest" project in the "examples" folder for a full overview.
All details related to HTTP are inferred from the interface declaration.
@path("/")
interface IMyAPI
{
// GET /api/greeting
@property string greeting();
// PUT /api/greeting
@property void greeting(string text);
// POST /api/users
@path("/users")
void addNewUser(string name);
// GET /api/users
@property string[] users();
// GET /api/:id/name
string getName(int id);
// GET /some_custom_json
Json getSomeCustomJson();
}
// vibe.d takes care of all JSON encoding/decoding
// and actual API implementation can work directly
// with native types
class API : IMyAPI
{
private {
string m_greeting;
string[] m_users;
}
@property string greeting() { return m_greeting; }
@property void greeting(string text) { m_greeting = text; }
void addNewUser(string name) { m_users ~= name; }
@property string[] users() { return m_users; }
string getName(int id) { return m_users[id]; }
Json getSomeCustomJson()
{
Json ret = Json .emptyObject;
ret["somefield"] = "Hello, World!";
return ret;
}
}
// actual usage, this is usually done in app.d module
// constructor
void static_this()
{
import vibe .http .server, vibe .http .router;
auto router = new URLRouter;
router .registerRestInterface(new API());
listenHTTP(new HTTPServerSettings(), router);
}
Authors
Sönke Ludwig, Михаил Страшун
Copyright
© 2012-2013 RejectedSoftware e.K.
License
Subject to the terms of the MIT license, as written in the included LICENSE.txt file.