RejectedSoftware Forums

Sign up

Need help with tiny REST example

I am trying to strip REST example to get it's minimal version to better understand how it's work.

import vibe.d;
import std.stdio;
import std.file;

class Example1
{
	string getSomeInfo()
	{
		return "Some Info!";
	}
}

shared static this()
{

	auto router = new URLRouter;
	registerRestInterface(router, new Example1());
	auto routes = router.getAllRoutes();

	auto settings = new HTTPServerSettings;
	settings.port = 8080;

	assert (routes[0].method == HTTPMethod.GET && routes[0].pattern == "/example1_api/some_info");

}

The problem that I can't compile it.

ta\traits.d(111): Error: tuple index 0 exceeds 0
C:\Users\Dima\AppData\Roaming\dub\packages\vibe-d-0.7.23\source\vibe\web\rest.d(74): Error: template instance ibe.internal.meta.traits.baseInterface!(Example1)
 error instantiating
source\app.d(18):        instantiated from here: registerRestInterface!(Example1)
FAIL .dub\build\application-debug-windows-x86-dmd_2067-628BC163223933EC087AB9597
F021CBC\ app executable```



Could anybody show minimal example of rest that do one function?

Re: Need help with tiny REST example

On Thu, 14 May 2015 20:01:29 GMT, Suliman wrote:

I am trying to strip REST example to get it's minimal version to better understand how it's work.

import vibe.d;
import std.stdio;
import std.file;

class Example1
{
	string getSomeInfo()
	{
		return "Some Info!";
	}
}

shared static this()
{

	auto router = new URLRouter;
	registerRestInterface(router, new Example1());
	auto routes = router.getAllRoutes();

	auto settings = new HTTPServerSettings;
	settings.port = 8080;

	assert (routes[0].method == HTTPMethod.GET && routes[0].pattern == "/example1_api/some_info");

}

The problem that I can't compile it.

The problem is that you didn't specify the API interface.
If you take a look at the examples ( https://github.com/rejectedsoftware/vibe.d/blob/master/examples/rest/source/app.d#L50 ), you'll see that every class inherit from an interface.
In your case, a simple "interface IAPI { string getSomeInfo(); }" will do.
I'll add a check for this error.

Re: Need help with tiny REST example

The problem is that you didn't specify the API interface.

Why I should to specify API interface? It's element of good design, but why I can't create simple class without it?

Re: Need help with tiny REST example

Am 16.05.2015 um 14:21 schrieb Suliman:

The problem is that you didn't specify the API interface.

Why I should to specify API interface? It's element of good design, but why I can't create simple class without it?

This restriction has been chosen to ensure a working REST server/client
duality. By always working on an interface, both are guaranteed to work
and to result in exactly the same REST protocol. For cases where only
the server is planned to be used, the restriction could in theory be
lifted...

Re: Need help with tiny REST example

Thanks, but what mean:

@rootPathFromName in example?

Re: Need help with tiny REST example

Am 17.05.2015 um 09:37 schrieb Suliman:

Thanks, but what mean:

@rootPathFromName in example?

It will define a URL prefix that is generated from the interface name:

@rootPathFromName
interface Test {
   string getFoo();
}

is equivalent to:

@path("/test")
interface Test {
   string getFoo();
}

Both will result in a route "GET /test/foo" to be added.

Re: Need help with tiny REST example

On Sat, 16 May 2015 15:20:01 +0200, Sönke Ludwig wrote:

Am 16.05.2015 um 14:21 schrieb Suliman:

The problem is that you didn't specify the API interface.

Why I should to specify API interface? It's element of good design, but why I can't create simple class without it?

This restriction has been chosen to ensure a working REST server/client
duality. By always working on an interface, both are guaranteed to work
and to result in exactly the same REST protocol. For cases where only
the server is planned to be used, the restriction could in theory be
lifted...

In theory, but in practice it'll add some complexity to the REST generator, because an interface defines exactly what you want to expose.
If you only rely on the object, you'll have way more things exposed than you intend to. For example, should you expose the base class' methods ? When the base class is Object, you surely don't want to expose 'opCmp' / 'opEquals' / 'toString'. But when it's not, what should you expose ? What if you do want to expose an overriden 'toString' in a base interface ?

And the killer of all: Depending on the stage of compilation, __traits(allMembers) might not returns all members (For example issue #1040 is not possible anymore because of a change in the Fronend...)

I tried to lift this constraint at some point, but realized it was not worth the trouble.

Re: Need help with tiny REST example

This restriction has been chosen to ensure a working REST server/client
duality. By always working on an interface, both are guaranteed to work
and to result in exactly the same REST protocol. For cases where only
the server is planned to be used, the restriction could in theory be
lifted...

Could you add mention about it in docs? It's very important part.