route variables and location_by_id

45 views
Skip to first unread message

Anne Ogborn

unread,
Jan 19, 2019, 1:27:48 AM1/19/19
to SWI-Prolog Mailing List


doing some termerized html and want to do


a(href(location_by_id(login(resend/User))), \local('Resend activation email'))]).


Ooops, location_by_id seems unknown. it's red and reports no_location_for_id -

Jan Wielemaker

unread,
Jan 19, 2019, 3:00:51 AM1/19/19
to Anne Ogborn, SWI-Prolog Mailing List
Because the argument is not a path definition, but a handler id. That
is either
defined using an id(Id) option in the handler declaration or using as
default the predicate name

At the moment the only option is

http_link_to_id(login, path_postfix(User), HREF),
...
html(a(href(HREF), ...)
...

It probably does make sense to support something along the way you
describe. Need to think about that

Cheers --- Jan

P.s. This surely appeared on the list ...




Anne Ogborn

unread,
Jan 19, 2019, 6:40:59 PM1/19/19
to SWI-Prolog Mailing List
Strange - just that one message got 'eaten'.
Yahoo says it went out.

Anyway, the path_postfix thing is a fix, but yes, there should be some
way to use route variables.

Also, I'm using two such 'REST type' extensions -


I need UName and Key

http://example.com/api/activate/UName/Key

While in retrospect this case is best handled by inferring the UName,
in general this case will come up, and there's no way to do it currently.
One click away from your upgraded Inbox

Raivo Laanemets

unread,
Jan 21, 2019, 2:00:34 AM1/21/19
to SWI-Prolog
I could see a solution with a predicate like location_by_path(api(activate/UName/Key)). The
predicate should be easy to implement with a similar code that's in location_by_id.

Anne Ogborn

unread,
Jan 22, 2019, 2:14:07 PM1/22/19
to Raivo Laanemets, SWI-Prolog
This is a good idea for a lot of reasons. I often find myself fighting the whole 'id' thing.

A more Prolog-y 'give me the answer from what I give you' approach might be


location_by_id(path(api(activate/UName/Key))) or
location_by_id(api(activate/UName/Key) (simpler but then namespace conflicts)

Jan Wielemaker

unread,
Jan 25, 2019, 4:32:07 AM1/25/19
to Anne Ogborn, Raivo Laanemets, SWI-Prolog
Hi,

Pushed something ready for evaluation. See examples/demo_rest.pl in the
comment. The brief summary is:

- Restricted ID in id(ID) to be an atom. Used to be an arbitrary
ground term. My guess is that this will break not much. Hope
I'm right here.
- If a handler is of the form

:- http_handler(PathAlias(a/B/c), ..., [id(x)])

we may use:

+ http_location_by_id(x, Path)
Returns the known prefix path, i.e., the materialization of
PathAlias('a/') in this case. This is as it used to be.
+ http_location_by_id(x(V), Path)
Where the number of arguments matches the number of variables
in the
path segments. In the above case this materializes
PathAlias(a/V/c), after encoding V.

From http_location_by_id/2, this functionality is available in
http_link_to_id/3, which allows using

http_link_to_id(x(aap), [name('Bob')], URL)

binding URL to the materialization of PathAlias(a/aap/c) followed
by ?name=Bob

This can also used in html_write as

a(href(location_by_id(x(aap)+[name('Bob')])), ...)

or, abbreviating `location_by_id` as `#`:

a(href(#(x(aap)+[name('Bob')])), ...)

Another place that accepts these notations is http_redirect/2.

The code is in the devel master. Note that this may still change if
somone comes with a better plan.

Does this satisfy the requirements and is this sufficiently natural?


Cheers --- Jan

Raivo Laanemets

unread,
Jan 25, 2019, 4:40:09 AM1/25/19
to SWI-Prolog
This is nice sweet API!

http_location_by_id(x(V), Path) 
Where the number of arguments matches the number of variables in the  path segments.

Thanks

Anne Ogborn

unread,
Jan 26, 2019, 12:38:14 AM1/26/19
to Jan Wielemaker, Anne Ogborn, Raivo Laanemets, SWI-Prolog
Looks good!

I'll return to pack(identity) as soon as I can.

Right now have just arrived in my new home city of Bangalore!
Reply all
Reply to author
Forward
0 new messages