我正在为我们正在寻求开发的 API 调查整个 Layer3/HATEOS/RESTful/HAL。
我们将公开可能被所有这些链接膨胀的数据列表。将链接模板化不是一个想法,这会被称为什么?我似乎找不到任何提及它,如果这不是一个好方法,为什么?
以一个假设的假期搜索 API 为例,入口点可以提供目的地和出发机场的列表,允许用户使用其中一个开始搜索。
像下面这样的方法会是更有效的方法吗?(松散地基于 HAL)如果不是,为什么?
{
"_links": {
"self": {
"href": "/"
},
"_templates": {
"airport": {
"self": { "href": "/airport/{IATA}" },
"destinations": { "href": "destinations?airport={IATA}" },
"parking": { "href": "/airport/{IATA}/parking" },
"hotels": { "href": "/hotels?airport={IATA}" },
"directions": { "href": "/guides?airport={IATA}" },
"search": [
{ "href": "/search?airport={IATA} title": "default" },
{ "href": "/search?airport={IATA}&type=CITY title": "citybreak" },
{ "href": "/search?airport={IATA}&type=LOW titel": "lowcost" }
]
},
"country": {
"self": { "href": "/destinations/{Code}/" },
"regions": { "href": "/destinations/{Code}/regions" },
"resorts": { "href": "/destinations/{Code}/resorts" },
"airports": { "href": "/destinations/{Code}/airorts" },
"search": { "href": "/search?country={Code}®ion=ANY&resort=ANY" }
}
}
},
"_embedded": {
"airport": [
{ "IATA": "LHR Name": "London Heathrow Airport" },
{ "IATA": "EMA Name": "East Midlands Airport" },
{ "IATA": "NWI Name": "Norwich International Airport" },
{ "IATA": "LTN Name": "London Luton Airport" },
{ "IATA": "STN Name": "London Stansted Airport" },
{ "IATA": "LCY Name": "London City Airport" },
{ "IATA": "LPL Name": "Liverpool John Lennon Airport" },
{ "IATA": "MAN Name": "Manchester Airport" },
{ "IATA": "LGW Name": "Gatwick Airport" }
],
"country": [
{ "Code": "CY Name": "Cyprus" },
{ "Code": "CZ Name": "Czech Republic" },
{ "Code": "ES Name": "Spain" },
{ "Code": "FO Name": "Faroe Islands" },
{ "Code": "GG Name": "Guernsey" },
{ "Code": "GR Name": "Greece" }
]
}
}