Subject: [apps-discuss] Question on URI-Templates - How to make non-variable segments optional?

From: Jan Algermissen <[email-masked]>
Date: Sat, 21 Jul 2012 12:35:38 +0200

Hi,

I have a question on RFC 6570 [1].

Is it possible to define optional non-variable path segments?

For example, there are customers with IDs and each customer can have 1-N accounts with accountIds.

The service I have provides customer information including all accounts and customer information including just one account:

http://example.org/svc/customers/{custId}

and

http://example.org/svc/customers/{custId}/accounts/{accountId}

Is there a way to write a template that handles both productions in one template? What I am looking for is a way to express that /accounts/{accountId} is optional as a whole, not just {accountId}.

Jan

[1] http://tools.ietf.org/html/rfc6570

---

From: Ioseb Dzmanashvili <[email-masked]>
Date: Sat, 21 Jul 2012 15:24:26 +0400

Hello Jan,

In recent weeks i was doing extensive URI Template spec related work(
https://github.com/ioseb/uri-template) and as far as i can tell there is a
possibility to control optional path segments with template like this:

http://example.org/svc/customers/{custId}{/account*}

In the example above {/account*} path segment expansion(i.e. "/") and
explode operator(i.e. "*") achieves desired result without problems. Of
course result depends on input variables. Below are  examples:

|  $template  = "http://example.org/svc/customers/{custId}{/account*}";
|  $variables = array(
|    'custId' => 21
|  );
|
|  echo uri_template($template, $variables);
|
|  // produces http://example.org/svc/customers/21

In the example above {/account*} part is ignored entirely as far as
"account" variable was not provided.

|  $variables = array(
|    'custId'  => 21,
|    'account' => array('accounts', 31)
|  );
|
|  echo uri_template($template, $variables);
|
|  // produces http://example.org/svc/customers/21/accounts/31

In the example above, $variables array contains "account" variable which
value is initialized to list(as per RFC) which forces URI Template
processor to expand this variable to: /accounts/31

Hope this helps!

Cheers,
ioseb

On Sat, Jul 21, 2012 at 2:35 PM, Jan Algermissen <[email-masked]
> wrote:

> The service I have provides customer information including all accounts
> and customer information including just one account:
>
>
> Is there a way to write a template that handles both productions in one
> template? What I am looking for is a way to express that
> /accounts/{accountId} is optional as a whole, not just {accountId}.
>
>
> Jan
>
>
> _______________________________________________
> apps-discuss mailing list
> [email-masked]
> https://www.ietf.org/mailman/listinfo/apps-discuss
>

-- 
Ioseb Dzmanashvili
Lead Architect
Picktek LLC
24b Khazbegi ave.
Tbilisi, 0177, Georgia
T (+995 32) 39.58.56, F (+1 202) 315.3934
picktek.com
code.ge
github.com/ioseb
twitter.com/iosebi

---

From: Jan Algermissen <[email-masked]>
Date: Sun, 22 Jul 2012 22:36:40 +0200

Hi Ioseb,

On Jul 21, 2012, at 1:24 PM, Ioseb Dzmanashvili wrote:

> Hello Jan,
> 
> In recent weeks i was doing extensive URI Template spec related work(https://github.com/ioseb/uri-template) and as far as i can tell there is a possibility to control optional path segments with template like this:
> 
> In the example above {/account*} path segment expansion(i.e. "/") and explode operator(i.e. "*") achieves desired result without problems. Of course result depends on input variables. Below are  examples:
> 
> In the example above {/account*} part is ignored entirely as far as "account" variable was not provided.
>  
> |  $variables = array(
> |    'custId'  => 21,
> In the example above, $variables array contains "account" variable which value is initialized to list(as per RFC)

Yeah, that was on my mind, too. Unfortunately, I am using the templates in a situation where mapping a variable to a constant string is totally out of the question :-)
(I am 'documenting' legacy APIs using Marks; json-home documents[1])

But thanks!

Jan

[1] http://tools.ietf.org/html/draft-nottingham-json-home-01

> which forces URI Template processor to expand this variable to: /accounts/31
> 
> Hope this helps!
> 
> Cheers,
> ioseb
> 
> On Sat, Jul 21, 2012 at 2:35 PM, Jan Algermissen <[email-masked]> wrote:
> Hi,
>

---

From: Ioseb Dzmanashvili <[email-masked]>
Date: Mon, 23 Jul 2012 10:13:56 +0400

Hi Jan, 

Unfortunately for this case i do not have idea except including both templates separately in service description.

Cheers,
ioseb

On Monday, July 23, 2012 at 12:36 AM, Jan Algermissen wrote:

> > On Sat, Jul 21, 2012 at 2:35 PM, Jan Algermissen <[email-masked] (mailto:[email-masked])> wrote:
> > Hi,
> > 
> > [email-masked] (mailto:[email-masked])
> > picktek.com (http://picktek.com)
> > code.ge
> > github.com/ioseb (http://github.com/ioseb)
> > twitter.com/iosebi (http://twitter.com/iosebi)
> > 
> 
> 
>

---

From: Mike Kelly <[email-masked]>
Date: Mon, 23 Jul 2012 10:30:48 +0100

On Mon, Jul 23, 2012 at 7:13 AM, Ioseb Dzmanashvili
<[email-masked]> wrote:
> Hi Jan,
>
> Unfortunately for this case i do not have idea except including both
> templates separately in service description.

Right, I was wondering why this isn't an option?