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 methodRecognized prefixes
GETget, query
PUTset, put
POSTadd, create, post
DELETEremove, erase, delete
PATCHupdate, 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.

  1. URLRouter registerRestInterface(URLRouter router, TImpl instance, RestInterfaceSettings settings)
  2. URLRouter registerRestInterface(URLRouter router, TImpl instance, MethodStyle style)
    URLRouter
    registerRestInterface
    (
    TImpl
    )
    (
    URLRouter router
    ,
    TImpl instance
    ,
    MethodStyle style
    )
  3. URLRouter registerRestInterface(URLRouter router, TImpl instance, string url_prefix, MethodStyle style)

Parameters

router URLRouter

The HTTP router on which the interface will be registered

instance TImpl

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

Examples

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.

1 @path("/")
2 interface IMyAPI
3 {
4 	@safe:
5 	// GET /api/greeting
6 	@property string greeting();
7 
8 	// PUT /api/greeting
9 	@property void greeting(string text);
10 
11 	// POST /api/users
12 	@path("/users")
13 	void addNewUser(string name);
14 
15 	// GET /api/users
16 	@property string[] users();
17 
18 	// GET /api/:id/name
19 	string getName(int id);
20 
21 	// GET /some_custom_json
22 	Json getSomeCustomJson();
23 }
24 
25 // vibe.d takes care of all JSON encoding/decoding
26 // and actual API implementation can work directly
27 // with native types
28 
29 class API : IMyAPI
30 {
31 	private {
32 		string m_greeting;
33 		string[] m_users;
34 	}
35 
36 	@property string greeting() { return m_greeting; }
37 	@property void greeting(string text) { m_greeting = text; }
38 
39 	void addNewUser(string name) { m_users ~= name; }
40 
41 	@property string[] users() { return m_users; }
42 
43 	string getName(int id) { return m_users[id]; }
44 
45 	Json getSomeCustomJson()
46 	{
47 		Json ret = Json.emptyObject;
48 		ret["somefield"] = "Hello, World!";
49 		return ret;
50 	}
51 }
52 
53 // actual usage, this is usually done in app.d module
54 // constructor
55 
56 void static_this()
57 {
58 	import vibe.http.server, vibe.http.router;
59 
60 	auto router = new URLRouter;
61 	router.registerRestInterface(new API());
62 	listenHTTP(new HTTPServerSettings(), router);
63 }

See Also

RestInterfaceClient class for a seamless way to access such a generated API

Meta