如何在RAML 1.0中重用头定义

时间:2017-04-07 08:37:11

标签: api http-headers raml

我有一个RAML 1.0规范,我定义了多个资源。每个资源都有相同的标头集。

我需要使用哪些RAML构造来声明头文件并在各种资源定义中重用它们?

例如

/read/records:
  post:
    description: api for reading records
    queryParameters: 
      table_name: string
    headers: 
            Authorization: 
                displayName: Authorization
                description: Basic authentication base 64 encoded string
                type: string
                required: true
            Tenant-Id: 
                displayName: Tenant-Id
                description: Tenant id for multi-tenant environments
                type: string
                required: true
            Content-type: 
                displayName: Content-type
                description: either xml or json
                type: string
                required: true
/adjust/records:
  post:
    description: api for editing records
    headers: 
            Authorization: 
                displayName: Authorization
                description: Basic authentication base 64 encoded string
                type: string
                required: true
            Tenant-Id: 
                displayName: Tenant-Id
                description: Tenant id for multi-tenant environments
                type: string
                required: true
            Content-type: 
                displayName: Content-type
                description: either xml or json
                type: string
                required: true

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用traits

#%RAML 1.0
traits: 
  hasHeaders: 
    headers: 
      Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
      Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
      Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true
/read/records:
  post:
    is: ["hasHeaders"]
    description: api for reading records
    queryParameters: 
      table_name: string
/adjust/records:
  post:
    is: ["hasHeaders"]
    description: api for editing records

答案 1 :(得分:0)

特征必须表示行为,并且它适用于路线和动词。对于我来说,标题适用于您可以定义资源类型的任何路线/动词:

#%RAML 1.0 ResourceType
get?:
  headers: 
    Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
    Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
    Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true
post?:
  headers: 
    Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
    Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
    Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true