An intuitive API uses plural(复数的) rather than singular(单数的) nouns, and concrete(具体的) rather than abstract names.

  • API URL中应该尽量使用复数名词作为resource名称,这样有助于一致化URL的表达方式;另外名词应该尽量具体,可以明白指出该resource代表什么事物,例如用televisions代表电视,而尽量不要用electrics等较抽象的词来代表电视。

 

Associations : “Resource almost always have relationships to other resources.” The relationships can be complex. Remember that once you have the primary key for one level, you usually don’t need to include the levels above because you’ve already got your specific object.

  • Resource之间通常有关联(Associations),例如:API可以getID5678owner所养的狗,他的URL可能长这样: /owners/5678/dogs ,这代表dogsowners的从属关系。
  • URL应尽量保持关系的简化,不要让URL表达的关系超过两个Level。尽量让URL表达的关系保持在Level NLevel N+1之间: /Resource level N/{ Resource N:Identifier}/ Resource level N+1.

 

Most APIs have intricacies(错综复杂的事物) beyond the base level of a resource. Make it simple for developers to use the base URL by putting optional states and attributes behind the HTTP question mark.

  • Resource通常都有些属性或状态等复杂的描述,在使用URL时应该把这些复杂的描述放在question mark ‘?’之后,即query string的方式表达resource的特别属性或状态
  • For Example:

GET /dogs?color=red&state=running&location=park

  • API URL将会指定return所有正在公园里乱跑的红色的狗狗的资讯。

 

[参考] Web API Design - Crafting Interfaces tgat Develoers Love

      https://pages.apigee.com/rs/apigee/images/api-design-ebook-2012-03.pdf

 

201868日星期五

相关文章