Skip to content
ADP
API Design PrincipleBETA

[ADP-767] 重複使用共享的常見 HTTP 標頭

概述

本 ADP 是 ADP-761的實踐之一。

指導原則

  • 應該(SHOULD)在 API 定義中重複使用常見的 HTTP 標頭。

  • 要添加新的架構,請遵循以下步驟:

    1. 複製當前的 models/headers-1.x.x.yaml 文件。
    2. 在複製的文件中添加新的架構。
    3. 確保遵循 ADP-4 中建議的語義化版本指南。
  • 應該(SHOULD)使用 ADP-758: OpenAPI $ref 來引用共享組件。

引用常見標頭

在 API 規範中使用 $ref 來引用常見標頭:

yaml
paths:
  /users:
    get:
      responses:
        '200':
          headers:
            X-Request-ID:
              $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/headers-1.0.0.yaml#/X-Request-ID'

引用常見問題或自訂

yaml
openapi: 3.1.0
info:
  title: Example API with HTTP Problems
  version: 1.0.0
  description: An API demonstrating the use of HTTP Problem Details referencing an external schema.
paths:
  /items/{itemId}:
    parameters:
      - name: itemId
        in: path
        required: true
        schema:
          type: string
    get:
      summary: Retrieve an item
      operationId: getItem
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  name:
                    type: string
                  description:
                    type: string
        '400':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/400'
        '401':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/401'
        '403':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/403'
        '404':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/404'
        'special-error':
          $ref: '#/components/schemas/SpecialError'
    put:
      summary: Update an item
      operationId: updateItem
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                description:
                  type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  name:
                    type: string
                  description:
                    type: string
        '400':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/400'
        '401':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/401'
        '403':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/403'
        '404':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/404'
        '409':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/409'
components:
  schemas:
    SpecialError:
      type: object
      allOf:
        - $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml'
        - type: object
          properties:
            status:
              type: integer
              example: 500
            title:
              type: string
              example: "Special Error"
            detail:
              type: string
              example: "This is a special customized error"
            extraType:
              type: string
              description: indicate the real problem type

引用雲事件及自訂負載

yaml
openapi: 3.1.0
info:
  title: CloudEvents API
  version: 1.0.0
  description: API for handling CloudEvents with extended data

paths:
  /events:
    post:
      summary: Create a new CloudEvent
      requestBody:
        content:
          application/cloudevents+json:
            schema:
              $ref: '#/components/schemas/ExtendedCloudEvent'
      responses:
        '201':
          description: CloudEvent created successfully
          content:
            application/cloudevents+json:
              schema:
                $ref: '#/components/schemas/ExtendedCloudEvent'

    get:
      summary: Get a list of CloudEvents
      responses:
        '200':
          description: List of CloudEvents
          content:
            application/cloudevents+json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ExtendedCloudEvent'

  /events/{id}:
    get:
      summary: Get a CloudEvent by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Single CloudEvent
          content:
            application/cloudevents+json:
              schema:
                $ref: '#/components/schemas/ExtendedCloudEvent'
        '404':
          $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/problem-1.0.0.yaml#/404'

components:
  schemas:
    ExtendedCloudEvent:
      allOf:
        - $ref: 'https://vivotek-it.github.io/api-design-principle-beta/models/event-1.0.0.yaml#/CloudEvent'
        - type: object
          properties:
            data:
              type: object
              properties:
                customField1:
                  type: string
                  description: Custom field 1 for extended data
                customField2:
                  type: integer
                  description: Custom field 2 for extended data
              required:
                - customField1
                - customField2

相關 ADP

參考資料