2

我正在为我们正在寻求开发的 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}&region=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" }
    ]
}

}

4

1 回答 1

1

这感觉像是过早的优化。我当然不会用负载大小来换取客户端处理的复杂性……尤其是使用 REST API 时,无论如何都会压缩 HTTP 1.1 zip。

您是否真正衡量过这种更“有效的方法”的好处?我的猜测是,这种方法最终会比发送所有内容更慢!

于 2013-09-26T13:05:41.710 回答