如何定义API端点

时间:2019-02-18 21:53:42

标签: rest api endpoint

我正在设计一个API,以请求索引中包含符合某些JSON格式的数据的信息。

目前,我只有具有类型构建的数据,但是其他类型也可能会出现,例如::

example data #1 :
"type": "building",
"id": "1",
"address" : "1 test",
...
"buildingType": "private",
"buildingSubType: "house"

example data #2 :
"type": "building",
"id": "2",
"address" : "100 test",
...
"buildingType": "corporate",
"buildingSubType: "restaurant"

example data #3 :
"type": "building",
"id": "3",
"address" : "200 test",
...
"buildingType": "government",
"buildingSubType: "hr services"

当我开始时,数据变得更简单,类型和子类型更少,因此构建API端点很简单,但是随着时间的流逝,我想将自己的api定义为对新数据的使用更具前瞻性

关于REST和将来的API文档,这是我得到的并且想验证的内容:

/buildings - Returns all buildings entries
/buildings/{id} - Returns a specific building entry
/buildings/buildingTypes - Returns all buildingTypes
/buildings/buildingTypes/{name} - Returns building entries with the specified buildingType
/buildings/buildingTypes/{name}/buildingSubTypes - Returns all buildingSubTypes for the specified buildingType
/buildings/buildingTypes/{name}/buildingSubTypes/{name} - Returns building entries with the specified buildingType and buildingSubType

对于2,我将添加查询字符串参数以进行更精确的过滤,例如:     / buildings / buildingTypes / corporate?states = NY     / buildings / buildingTypes / corporate / buildingSubTypes / restaurant?state = NY

1 个答案:

答案 0 :(得分:0)

  

关于REST和将来的API文档,这是我得到的并且想要验证的

REST不在乎您的URL使用什么拼写,也不在乎如何组织资源层次。

URL拼写约定仅是为了方便人类使用-它们对API的正确性没有比对变量名的拼写更重要。