JSON API - 从外部源获取数据时如何形成 URI 段

JSON API - How to form URI segments when fetching data from external source

我正在编写符合 https://jsonapi.org/format/ 标准的 api 规范。我无法确定要在以下场景中实施的正确 uri 格式。

概览:

公司库存系统包含产品和价格信息。一种产品有多种价格。库存系统通过 API 将数据推送到本地系统,但使用库存系统主键。本地系统应将提供的数据捕获到 API 并在本地数据库中更新或创建价格记录

例如:

POST/产品/.....??或 POST /产品/

{
   "data":{
       "externalId":"EIR-32432",
       "externalPriceId":xxx,
       "price":"xxx.xx",
       "currency":"USD"
   }
}

您可以在此处检查 URI 的一些命名:https://restfulapi.net/resource-naming/

您必须向代表该资源集合的 URL 发送 POST 请求,以创建符合 JSON:API 规范的资源:

A resource can be created by sending a POST request to a URL that represents a collection of resources. The request MUST include a single resource object as primary data. The resource object MUST contain at least a type member.

规范本身与 URL 命名无关,但在所有示例中都使用复数资源类型。如果您遵循相同的约定,则创建产品的请求应为 POST /products.

如上引述所述,请求必须包含 resource object。您问题中的示例不是有效的资源对​​象,因为它缺少 type 并且属性不在 attributes 键下。创建产品的有效资源对象如下所示:

{
    "data":{
        "type": "products",
        "attributes": {
            "externalId": "EIR-32432",
            "externalPriceId": "xxx",
            "price": "xxx.xx",
            "currency": "USD"
        }
    }
}

另请注意,关系不应显示为属性:

Although has-one foreign keys (e.g. author_id) are often stored internally alongside other information to be represented in a resource object, these keys SHOULD NOT appear as attributes.

从你的例子中看不出 externalIdexternalPriceId 是否属于你的 API 范围内的关系。

请在规范本身的 JSON:API 中找到有关创建资源的更多详细信息:https://jsonapi.org/format/#crud-creating 它还附带一个示例。