ALPS示例实现

时间:2015-07-06 13:50:48

标签: rest api semantics

我正在寻找使用ALPS的示例客户端实现(不是山脉,而是应用程序级配置文件语义)。

你呢!有一个?

我已经查看了相关的RFC draft和讨论,但仍然可以解决它。

具体来说,我想知道我的客户端应该如何知道描述符描述的内容,因为我的客户端应该对REST原理要求的REST API的结构和语义一无所知?

作为一个人,我知道一个名为“用户”的id标签的描述符可能会描述如何与用户互动,但如果没有我明确告诉他,我的客户怎么知道?

我知道我可以在描述符中插入某种关键字并告诉我的客户端匹配相应的关键字,但这似乎不是正确的方法。

我很乐意提供一个更详细的例子,因为有人愿意阅读它。

1 个答案:

答案 0 :(得分:1)

我也是第一次探索ALPS,而且我对RFC草案的理解也不是直接的。

以下是来自RFC作者的slideshow (166 slides, so it's not possible to copy it all into this answer),我认为可以更好地理解ALPS所扮演的角色。

  

作为一名人类,我知道名为用户descriptor标记的id可能会描述如何与用户进行互动,但我的客户如何知道这一点我明确地告诉他了吗?

从这张幻灯片中,我推断出你的问题的答案:他没有。

在幻灯片中,将样本ALPS配置文件与表单提交的等效HTML代码进行比较。浏览器知道如何将HTML呈现到屏幕上,但只有人知道使用该提交按钮使用这些输入字段POST表单意味着什么。

以下是来自alps.io完整JSON表示的示例

{ 
  "alps" : {
    "version" : "1.0",
    "doc" : {
      "href" : "http://example.org/samples/full/doc.html"
    },
    "descriptor" : [
      {
        "id" : "search", 
        "type" : "safe",
        "doc" : {"value" : 
          "A search form with a two inputs"
        },
        "descriptor" : [
          {
            "id" : "value",
            "name" : "search",
            "type" : "descriptor",
            "doc" : { "value" : "input for search" }
          },
          { "href" : "#resultType" }
        ]
      },
      {
        "id" : "resultType",
        "type" : "descriptor",
        "description" : {"value" : "results format"},
        "ext" : [
          {
            "href" : "http://alps.io/ext/range", 
            "value" : "summary,detail"
          }
        ]
      }
    ]  
  }
}

例如,一个通用的手机应用程序,它根据REST响应向用户显示屏幕。假设HAL+Json响应包含对搜索实体的引用。该应用程序可以在此ALPS文档中查找搜索实体是什么,并且可以对如何表示该实体进行编码。也就是说,搜索是具有name/value对(带有id)和href的对象。 href引用第二个descriptor,ID为 resultType ,可让应用知道搜索结果的格式。涉及的实际URL和数据将来自REST响应。

从2014年7月开始,这里有一个Spring blog article,用于描述管理"待办事项列表"的应用的ALPS。 ALPS文件描述

  1. 什么是 todo 实体
  2. 使用 todo 实体可以执行哪些操作
  3. 该小应用的ALPS配置文件的简略版本:

    {
      "version" : "1.0",
      "descriptors" : [ {
        "id" : "todo-representation",
        "descriptors" : [ {
          "name" : "description",
          "doc" : {
            "value" : "Details about the TODO item",
            "format" : "TEXT"
          },
          "type" : "SEMANTIC"
        }, {
          "name" : "title",
          "doc" : {
            "value" : "Title for the TODO item",
            "format" : "TEXT"
          },
          "type" : "SEMANTIC"
        }, {
          "name" : "id",
          "type" : "SEMANTIC"
        }, {
          "name" : "completed",
          "doc" : {
            "value" : "Is it completed?",
            "format" : "TEXT"
          },
          "type" : "SEMANTIC"
        } ]
      }, {
        "id" : "create-todos",
        "name" : "todos",
        "type" : "UNSAFE",
        "rt" : "#todo-representation"
      }, {
        "id" : "get-todos",
        "name" : "todos",
        "type" : "SAFE",
        "rt" : "#todo-representation"
      }, {
        "id" : "delete-todo",
        "name" : "todo",
        "type" : "IDEMPOTENT",
        "rt" : "#todo-representation"
      } ]
    }
    

    我想一种方法可以将其视为一种"架构",而不是数据库表,它描述了REST响应的范围。

相关问题