RejectedSoftware Forums

Sign up

Internal requests

Hello !

I could not find anywhere mentions to how to execute internal requests.

I want to compose a response based one one or more calls to other urls on the same server (see http://en.wikipedia.org/wiki/Hierarchical_model-view-controller):

Obs.: Bellow is only to show what I want at high level

@path(/hello)
void sendHello(HTTPServerResponse res)
{
res.writeBody("Hello", "text/plain");
}

@path(/world)
void sendWorld(HTTPServerResponse res)
{
res.writeBody("World", "text/plain");
}

@path(/hello-world)
void sendHelloWorld(HTTPServerResponse res)
{
string result = getInternal("/hello");
result ~= getInternal("/world");
res.writeBody(result, "text/plain");
}

Re: Internal requests

On Fri, 17 Oct 2014 00:59:06 GMT, Domingo wrote:

Hello !

I could not find anywhere mentions to how to execute internal requests.

I want to compose a response based one one or more calls to other urls on the same server (see http://en.wikipedia.org/wiki/Hierarchical_model-view-controller):

Obs.: Bellow is only to show what I want at high level

@path(/hello)
void sendHello(HTTPServerResponse res)
{
res.writeBody("Hello", "text/plain");
}

@path(/world)
void sendWorld(HTTPServerResponse res)
{
res.writeBody("World", "text/plain");
}

@path(/hello-world)
void sendHelloWorld(HTTPServerResponse res)
{
string result = getInternal("/hello");
result ~= getInternal("/world");
res.writeBody(result, "text/plain");
}

You could use requestHTTP to make a local HTTP request and implement getInternal using that, but for efficiency reasons I'd rather move the functionality of "hello" and "world" to a separate private function and then call that.

Alternatively (this currently only works for JSON), instead of directly writing to the HTTP response object, you could use the return value of sendHello to return its result, or you could use an OutputStream parameter and write to that:

@contentType("text/plain")
void postHello(OutputStream output)
{
	output.write("Hello");
}

@contentType("text/plain")
void postWorld(OutputStream output)
{
	output.write("World");
}

@contentType("text/plain")
void postHelloWorld(OutputStream output)
{
	postHello(output);
	postWorld(output);
}

This would more or less be the most efficient way to do it.

However, depending on what exactly the overall structure is, it may also make sense to use a separate registerRestInterface call to register the low level routes and use a RestInterfaceClient to make the internal calls. This would allow to later easily distribute the internal calls among multiple servers. This is how I'd usually structure a web service.