开发者

Yet more questions on RESTful URIs

开发者 https://www.devze.com 2023-02-12 00:51 出处:网络
Numerical IDs vs names As an example, which of these would you choose for identifying a single transaction, from a single bank account, for a single company:

Numerical IDs vs names

As an example, which of these would you choose for identifying a single transaction, from a single bank account, for a single company:

  1. /companies/freds-painting-ltd/accounts/savings/transactions/4831
  2. /companies/freds-painting-ltd/accounts/1/transactions/4831
  3. /companies/62362/accounts/1/transactions/4831
  4. You idiot, something totally different! Crikey, did you even READ Fielding's dissertation?

Now, I think the 1st one is the most readable. If I have more than one company, or if I'm someone like an accountant managing multiple companies, it's immediately clear which company, and which account, I'm looking at. It's also more bookmarkable/emailable and would prevent 'fishing' for other companies by changing the company ID. I would want transaction IDs to be unique to an account (I.e. Both 'savings' and 'current' accounts could have transaction '1'

A 'company' will be my 'top-level', or 'first class' resource. Nothing at all would ever be shared between companies. As such, it would be the ideal candidate for a shard (or 'ancestor'/'namespace' in Google App Engine parlance). So I'd only have to wo开发者_开发技巧rry about the account names being unique within one company. Every company could have an account called 'savings'.

Not sure what the situation in the rest of the world is, though LTDs or PLCs in UK would have a unique name, there could be many 'Dave's Window Cleaning' businesses (what's know as a trading name).

The business owner(s) could potentially opt for the top level /company/company-name URI to be public, and contain some basic details like their website, contact details etc, but everything below that would NEVER be accessible by search engines.

So my thoughts/concerns are:

1) Is it reasonable, when someone signs in to add their business, to say "Sorry, 'Dave's Window Cleaning' business is taken. How about 'Dave's Window Cleaning Portsmouth' (Having taken their location in another field)? My worry with this is that, for a more well known company, you're giving away the fact that they have an account with you. Or that someone could use that form to search for names. Perhaps not a biggie.

2) The size of the company name. Would it be reasonable for a name like 'Dave's Window cleaning, gardening, and loads of other stuff'? Thus creating a URL like 'daves-window-cleaning-gardening-and-loads-of-other-stuff/'

3) How to deal with someone changing their business name - I would approach it by creating a new company with that string ID, copying over everything, then deleting the old resource. The original URI would return 404 rather than redirecting - as you can't guarantee someone else won't want to take the now unused name, or even if more than one person has used the same name in the past.

4) Should the 'real' unique ID be an number in the back end, and for every request to be handled by first doing a query for what company ID this name actually related to.

5) The impact of searching for a transaction in the persistence layer.

6) The possibility of URL rewriting, but then that wouldn't work cleanly in GAE, nor would it solve the issue of ensuring company names are unique.

RESTful webservice vs RESTful website

So, we potentially have this lovely RESTful webservice that the latest snazzy iphone/android app can use (delusions of grandeur). But what about the main website itself? I note, right now, that the URL I see at the top of my page is not 'RESTful': /questions/ask is an action. There is no 'ask' resource on the server. It's more the state of the page, the preparation for POSTing to /questions/ - or if I'm editing, PUTing to /questions/{id}

I also note that Stackoverflow has URIs like /questions/362352/name-of-the-question, and that the latter part can be omitted, and one will be redirected to it.

Should I host a completely separate webapp that consumes my lovely webservice (from the same domain)? Do I even need a separate REST server, or can I rely on content negotiation (JSON/XML) and HTTP verb to select the right method (I'm using Jersey), and return the right representation?

So I could have /companies/aboxo/ return the whole HTML page (using stringtemplate.org) if it's a GET /,text/plain or test/html, and JSON/XML for others?

But what happens for 'add/edit/delete' transaction? Would GET / /companies/freds-painting-ltd/savings/transactions/?template=add be ok (or GET ../transactions/352?template=edit), and that would return the right HTML?

Thinking about this last detail is driving me mad for some reason.

Comments, suggestions, outright ridicule - all welcome!

Marcos


Rails solves the "id vs name" problem by displaying both in the URL but using only the id to actually identify eg:

/companies/62362-freds-painting-ltd/accounts/1-savings/transactions/4831

ie - for the ones that have a "pretty url" the function that generates your path write both the id and the name... but for your router, where relevant: you strip off everything thats not the id.

incidentally, it means your customer could actually write whatever they like into the URL and it'd make no difference:

/companies/62362-i_luv_blue_turtles/accounts/1-your_mum/transactions/4831

and your router still just sees:

/companies/62362/accounts/1/transactions/4831

:)


For a cannonical URI I suggest just /transactions/{id} as I presume the transaction knows what the company and account is. Therefore, #4 :-)

Is SEO a concern? I presume you don't want random folks off the internet googling for X company's transactions?! Therefore, I would just keep names (which may change) out of the URI.

0

精彩评论

暂无评论...
验证码 换一张
取 消