RejectedSoftware Forums

Sign up

REST and routing

Background - I am very new to d and haven't programmed anything for over 30 years when I used C so please excuse what might be a simple question.

I am working through the exercises in the D Web Development by Kai "redstar" Nacke and have made it to the chapter on REST interfaces. I was able to get both a server and client working using the redis examples.

Now I want to use mySql and I was successful in getting it working both with a server and client.

I need to add a method to my class and there is no way I can get the program to recognize the route to the method adduserId. It is my understanding that this should end up with a router.post("/userid",&adduserId(......)) which would mean my client would access it with a post to /userid.

Any hints will certainly be appreciated.

Thanks
John

class NoteStoreImplementation : NoteStore
{
uint myid = 1;
string anyThing;

Note[] getNotes(string id)
{
    Note toNote(Row r)
    {
        Note note;
        r.toStruct(note);
        return note;
    } 
    auto con = db.lockConnection();
    scope(exit) con.close();
    auto cmd = Command(con, "SELECT id, topic, content FROM Notes WHERE userid = ?");
    cmd.prepare();
    cmd.bindParameter(myid, 0);
    auto result = cmd.execPreparedResult();
    return array(map!(r => toNote(r))(result));
}

long addNote(string name, Note note)
{
auto con = db.lockConnection();
scope(exit) con.close();
auto cmd = Command(con, "INSERT INTO Notes (userid, topic, content) VALUES(?, ?, ?)");
cmd.prepare();
cmd.bindParameter(myid, 0);
cmd.bindParameter(note.topic, 1);
cmd.bindParameter(note.content, 2);
ulong changed;
cmd.execPrepared(changed);
    return changed;
}

string adduserId(string ud)
{

checkUser("yourid", "secret", myid);
return "anyThing";

}

}

module notestore;

struct Note
{

long id;
string topic;
string content;

}
struct userData
{

uint id;
string userName;
string password;

}

interface NoteStore
{
string adduserId(string ud);
Note[] getNotes(string name);
long addNote(string name, Note note);
}

Re: REST and routing

On Tue, 16 Feb 2016 18:24:36 GMT, John More wrote:

I need to add a method to my class and there is no way I can get the program to recognize the route to the method adduserId. It is my understanding that this should end up with a router.post("/userid",&adduserId(......)) which would mean my client would access it with a post to /userid.

Any hints will certainly be appreciated.

I think that according to the naming convention it should be "addUserId", with a capital U. The documentation [1] says:

"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."

[1]

/Jacob Carlborg

Re: REST and routing

On Wed, 17 Feb 2016 07:43:50 GMT, Jacob Carlborg wrote:

On Tue, 16 Feb 2016 18:24:36 GMT, John More wrote:

I need to add a method to my class and there is no way I can get the program to recognize the route to the method adduserId. It is my understanding that this should end up with a router.post("/userid",&adduserId(......)) which would mean my client would access it with a post to /userid.

Any hints will certainly be appreciated.

I think that according to the naming convention it should be "addUserId", with a capital U. The documentation [1] says:

"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."

[1]

/Jacob Carlborg

Jacob,
Thank you for your quick response.

I was able to get it working correctly but discovered some anomalies along the way; that, I am sure if pursued would lead me into the depths of the framework where a mere mortal as I should fear to tread.

The addUserId(userData ud) would not work with any form of path "/userId, /userid, /user". What did work was addUserid(userData ud) with a path of "/userid".

After finding your documentation reference I looked at the
URLRouter registerRestInterface(TImpl)(
URLRouter router,
TImpl instance,
RestInterfaceSettings settings = null
);
and in particular the settings.methodStyle where I think the default is likely to be lowerCase. Unfortunately figuring out how to set this on the call is beyond me at this point but I do have it working.

Thanks Again
John

Re: REST and routing

On 2016-02-17 16:54, John More wrote:

Jacob,
Thank you for your quick response.

I was able to get it working correctly but discovered some anomalies along the way; that, I am sure if pursued would lead me into the depths of the framework where a mere mortal as I should fear to tread.

The addUserId(userData ud) would not work with any form of path "/userId, /userid, /user". What did work was addUserid(userData ud) with a path of "/userid".

After finding your documentation reference I looked at the
URLRouter registerRestInterface(TImpl)(
URLRouter router,
TImpl instance,
RestInterfaceSettings settings = null
);
and in particular the settings.methodStyle where I think the default is likely to be lowerCase.

Seems I forgot to include the link for the documentation :)

I think the default method style is "lowerUnderscored" [1]. That would
mean "addUserId" would be mapped to "/user_id", unless it has some
special treatment of "id".

Unfortunately figuring out how to set this on the call is beyond me at this point but I do have it working.

I think you set it something like this:

auto settings = new RestInterfaceSettings;
settings.methodStyle = MethodStyle.lowerUnderscored; // or whatever
style you prefer [2]

auto router = new URLRouter;
router.registerRestInterface(new NoteStoreImplementation, settings);

[1] http://vibed.org/api/vibe.web.common/MethodStyle.lowerUnderscored
[2] http://vibed.org/api/vibe.web.common/MethodStyle

/Jacob Carlborg

Re: REST and routing

On Wed, 17 Feb 2016 20:37:20 +0100, Jacob Carlborg wrote:

On 2016-02-17 16:54, John More wrote:

Jacob,
Thank you for your quick response.

I was able to get it working correctly but discovered some anomalies along the way; that, I am sure if pursued would lead me into the depths of the framework where a mere mortal as I should fear to tread.

The addUserId(userData ud) would not work with any form of path "/userId, /userid, /user". What did work was addUserid(userData ud) with a path of "/userid".

After finding your documentation reference I looked at the
URLRouter registerRestInterface(TImpl)(
URLRouter router,
TImpl instance,
RestInterfaceSettings settings = null
);
and in particular the settings.methodStyle where I think the default is likely to be lowerCase.

Seems I forgot to include the link for the documentation :)

I think the default method style is "lowerUnderscored" [1]. That would
mean "addUserId" would be mapped to "/user_id", unless it has some
special treatment of "id".

Unfortunately figuring out how to set this on the call is beyond me at this point but I do have it working.

I think you set it something like this:

auto settings = new RestInterfaceSettings;
settings.methodStyle = MethodStyle.lowerUnderscored; // or whatever
style you prefer [2]

auto router = new URLRouter;
router.registerRestInterface(new NoteStoreImplementation, settings);

[1] http://vibed.org/api/vibe.web.common/MethodStyle.lowerUnderscored
[2] http://vibed.org/api/vibe.web.common/MethodStyle

/Jacob Carlborg

Jacob,
I was able to play with the different method styles and get them working.
As I said I am really a neophyte when it comes to object oriented programming and so tracing this has been very helpful.
I am now working on adding basic authentication to a Rest service using the registerRestInterface and of course after a couple of days of experimentation I am hopelessly lost. If you could point me in a direction or if you know of a sample I would really appreciate the knowledge.
If you think I should start a new thread I would be happy to do so.

Thanks
John

Re: REST and routing

On 2016-02-19 08:10, John More wrote:

I am now working on adding basic authentication to a Rest service using the registerRestInterface and of course after a couple of days of experimentation I am hopelessly lost. If you could point me in a direction or if you know of a sample I would really appreciate the knowledge.
If you think I should start a new thread I would be happy to do so.

There's documentation for HTTP basic authentication here [1]. If you
need more help I recommend starting a new thread. One topic per thread
is easier to handle.

[1] http://vibed.org/docs#http-authentication

/Jacob Carlborg