Pooja
Pooja

Reputation: 115

Rest api - Defining URL for hierarchical level data

I have concept of Account and sub-account.

Account
   |--->Subaccount1
            |----> Subaccount2
                       |---->Subaccount3
                             ....................... and so on
Account level rest url's:
POST - base_url/accounts
PUT - base_url/accounts
GET - base_url/accounts/$acc_name
DELETE - base_url/accounts/$acc_name
GET - base_url/accounts
Subaccount level rest url's - level 1
POST - base_url/accounts/$acc_name/subaccount
PUT - base_url/accounts/$acc_name/subaccount
GET - base_url/accounts/$acc_name/subaccount/$sub_acc
DELETE - base_url/accounts/$acc_name/subaccount/$sub_acc
GET - base_url/accounts/$acc_name/subaccount

If the sub-account level is 1 or known, i can define URL. But since i doesn't know the hierarchy level, How will i define URL for sub-accounts?

For defining routers, i m making use of vertx framework.

Upvotes: 0

Views: 80

Answers (2)

Alberto S.
Alberto S.

Reputation: 7649

In the Vertx Web module documentation is stated that you can use a regex to define how your URL will look like. The easiest way would be to define a route /accounts/{id}/subaccounts/{id}/* that takes care of handling nth level of depth

However, while possible, I strongly recommend you against this pattern. This will make your URLs highly nested and can bring you troubles in the future. A better (and much more simple) approach would be to consider a subaccount a first class citizen so you'd have

  • /accounts/{id}/subaccounts/{id} for "first" level nested subaccounts
  • /subaccounts/{id}/subaccounts/{id} for "nth" level nested subaccounts

Or even considering that a subaccount is an account itself, in that case you don't need the second URL

Upvotes: 1

maio290
maio290

Reputation: 6742

Frankly? I wouldn't do it via nesting, especially not, if you have a depth of n.

Just make the unique identifier of the sub-accounts usable on the /accounts endpoint.

In the end, a sub-account is a subset of an account. Therefore I think this is pretty acceptable.

If you really want to split it, rather do something like that:

/accounts/{id}/subaccounts/{id}

Upvotes: 0

Related Questions