RejectedSoftware Forums

Sign up

Dynamic URL Question

According to the documentation, we can have URL that have at least one part of it is dynamic. E.g. if I have "/api/19/foo", the "19" could be any integer. Then, in my code, it looks like I would have something like this:

void getFoo(int id)
{
...
}

Can I do this with strings? E.g. if I have a URL like this: "/api/foo/bar/action", can I have "foo" and "bar" be accessible the same way? Or, am I forced to use URL variables? Or, is there a workaround for this?

The associated method would look like this:

void getAction(string id1, string id2)
{
...
}

Re: Dynamic URL Question

On Wed, 28 Nov 2012 22:06:10 GMT, Casey wrote:

According to the documentation, we can have URL that have at least one part of it is dynamic. E.g. if I have "/api/19/foo", the "19" could be any integer. Then, in my code, it looks like I would have something like this:

void getFoo(int id)
{
...
}

Can I do this with strings? E.g. if I have a URL like this: "/api/foo/bar/action", can I have "foo" and "bar" be accessible the same way? Or, am I forced to use URL variables? Or, is there a workaround for this?

The associated method would look like this:

void getAction(string id1, string id2)
{
...
}

This is currently not possible in the REST interface generator (you can have one string id parameter, but not two). The main problem is how to declare such constructs in the source code without interfering with normal parameters - all that is available are the parameter names and they are already used for normal parameters, the id parameter, _custom parameters and the unfortunate _dummy parameter, that can sometimes be necessary. If user defined attributes get in the final dmd 2.061 release, those would probably provide some good opportunities to implement this in a clean way.

However, if "foo" and "bar" are from a small fixed set of strings, you should be able to do something like this, though:

interface ApiInterface {
    @property FooInterface foo();
}

interface FooInterface {
    @property BarInterface bar();
}

interface BarInterface {
    void action();
}

action() is then accessible as POST /foo/bar/action.

Re: Dynamic URL Question

Thanks, however I ended up reworking my code to look like the code that's
under the Routing section of the docs and it works great. The URLs are
exactly how I want them and I figured out how to manipulate the responses to
get them the way I like them. Guess I should have re-read the main docs page
first...