扶墙老师说

一个架构士的思考与沉淀 扶墙老师,辨证施“知”



Format 1A revision 7

1 I. API Blueprint Language

1.1 1. Introduction

This documents is full specification of the API Blueprint format. For a less formal introduction to API Blueprint visit the API Blueprint Tutorial or check some of the examples.

1.2 2. API Blueprint

API Blueprint is documentation-oriented web API description language. The API Blueprint is essentially a set of semantic assumptions laid on top of the Markdown syntax used to describe a web API.

In addition to the regular Markdown syntax API Blueprint conforms to the GitHub Flavored Markdown syntax.

1.3 3. API Blueprint document

An API Blueprint document – a blueprint is a plain text Markdown document describing a Web API or its part. The document is structured into logical sections. Each section has its distinctive meaning, content and position in the document.

General section definition and structure is discussed in detail later in the Blueprint section chapter.

All of the blueprint sections are optional. However, when present, a section must follow the API Blueprint document structure.

1.3.1 Blueprint document structure

NOTE: The number prior to a section name denotes the allowed number of the section occurrences.

NOTE: Refer to Sections Reference for description of a specific section type.

1.4 4. Blueprint section

Section represents a logical unit of API Blueprint. For example an API overview, a group of resources or a resource definition.

In general a section is defined using a keyword in a Markdown entity. Depending on the type of section the keyword is written either as a Markdown header entity or in a list item entity.

A section definition may also contain additional variable components such as its identifier and additional modifiers.

NOTE: There are two special sections that are recognized by their position in the document instead of a keyword: The Metadata section and the API Name & Overview section. Refer to the respective section entry for details on its definition.

1.4.0.1 Example: Header-defined sections

# <keyword> 

 ...

# <keyword> 

 ...

NOTE: While this specification uses “atx” -style headers (using #s) you can also use “Setext” header syntax interchangeably:

<keyword>
=========

...

<keyword>
=========

...

1.4.0.2 Example: List-defined sections

+ <keyword>

 ...

+ <keyword>

 ...

NOTE: While this specification uses pluses (+) as list markers you can use any Markdown list syntax using asterisks (*), pluses (+) and hyphens (-) interchangeably:

* <keyword>

...

- <keyword>

...

1.4.1 4.1 Section types

There are several types of API Blueprint sections. You can find the complete listing of the section types in the Section Reference.

The Blueprint section chapter discusses the section syntax in general. A specific section type may conform only to some parts of this general syntax. Always refer for respective section reference for details on its syntax.

1.4.2 4.2 Section structure

A general structure of an API Blueprint section defined by a keyword includes an identifier (name), section description and nested sections or a specifically formatted content.

1.4.2.1 Example: Header-defined section structure

# <keyword> <identifier>

<description>

<specific content>

<nested sections>

1.4.2.2 Example: List-defined section structure

+ <keyword> <identifier>

    <description>

    <specific content>

    <nested sections>

1.4.3 4.3 Keywords

Following reserved keywords are used in section definitions:

1.4.3.1 Header keywords

1.4.3.2 List keywords

NOTE: Avoid using these keywords in other Markdown headers or lists

NOTE: With the exception of HTTP methods keywords the section keywords are case insensitive.

1.4.4 4.4 Identifier

A section definition may or must include an identifier of the section. An identifier is any non-empty combination of any character except [, ], (, ) and newline characters.

An identifier must not contain any of the keywords.

1.4.4.1 Example

Adam's Message 42
my-awesome-message_2

1.4.5 4.5 Description

A section description is any arbitrary Markdown-formatted content following the section definition.

It is possible to use any Markdown header or list item in a section description as long as it does not clash with any of the reserved keywords.

NOTE: It is considered good practice to keep the header level nested under the actual section.

1.4.6 4.6 Nested sections

A section may contain another nested section(s).

Depending on the nested section type, to nest a section simply increase its header level or its list item indentation. Anything between the section start and the start of following section at the same level is considered to be part of the section.

What sections can be nested and where depends upon the section in case, as described in the relevant section’s entry.

1.4.6.1 Example: Nested header-defined section

# <section definition> 

 ... 

## <nested section definition>

 ...

1.4.6.2 Example: Nested list-defined section

+ <section definition>

     ... 

    + <nested section definition>

     ...

NOTE: While not necessary it is a good habit to increase the level of a nested section markdown-header.

NOTE: A markdown-list section is always considered to be nested under the preceding markdown-header section.


2 II. Sections Reference

NOTE: Sections marked as “Abstract” serve as a base for other sections and as such they cannot be used directly.

2.1 1. Named section

2.1.0.1 Definition

Defined by a keyword followed by an optional section name - identifier in a Markdown header or list entity.

# <keyword> <identifier>
+ <keyword> <identifier>

2.1.0.2 Description

Named section is the base section for most of the API Blueprint sections. It conforms to the general section and as such it composes of a section name (identifier), description and nested sections or specific formatted content (see descendants descriptions).

2.1.0.3 Example

# <keyword> Section Name
This the `Section Name` description.

- one
- **two**
- three

<nested sections> |  <formatted content>

2.2 2. Asset section

2.2.0.1 Definition

Defined by a keyword in Markdown list entity.

+ <keyword>

2.2.0.2 Description

Asset section is the base section for atomic data in API Blueprint. The content this section is expected to be a pre-formatted code block.

2.2.0.3 Example

+ <keyword>

        {
            "message": "Hello"
        }

2.2.0.4 Example: Fenced code blocks

+ <keyword>

    ```
    {
        "message": "Hello"
    }
    ```

2.3 3. Payload section

2.3.0.1 Definition

Defined by a keyword in Markdown list entity. The keyword may be followed by identifier. The definition may include payload’s media-type enclosed in braces.

+ <keyword> <identifier> (<media type>)

NOTE: Refer to descendant for the particular section type definition.

2.3.0.2 Description

Payload sections represent the information transferred as a payload of an HTTP request or response messages. A Payload consists of optional meta information in the form of HTTP headers and optional content in the form HTTP body.

Furthermore, in API Blueprint context, a payload include a description and a validation schema.

A payload section may have its media type associated. A payload’s media type represents the metadata received or sent in the form of a HTTP Content-Type header. When specified a payload should include nested Body section.

This section should include at least one following nested sections:

If there is no nested section the content of the payload section is considered as content of the Body section.

2.3.0.3 Referencing

Instead of providing a payload section content a model payload section can be referenced using the Markdown implicit reference syntax:

[<identifier>][]

2.3.0.4 Example

+ <keyword> Payload Name (application/json)

    This the `Payload Name` description.

    + Headers
    
     ...

    + Body
        
     ...

    + Schema
    
    ...

2.3.0.5 Example: Referencing model payload

+ <keyword> Payload Name

    [Resource model identifier][]

2.4 4. Headers section

2.4.0.1 Definition

Defined by the Headers keyword in Markdown list entity.

+ Headers

2.4.0.2 Description

Specifies the HTTP message-headers of the parent payload section. The content this section is expected to be a pre-formatted code block with the following syntax:

<HTTP header name>: <value>

One HTTP header per line.

2.4.0.3 Example

+ Headers

        Accept-Charset: utf-8
        Connection: keep-alive
        Content-Type: multipart/form-data, boundary=AaB03x

2.5 5. Body section

2.5.0.1 Definition

Defined by the Body keyword in Markdown list entity.

+ Body

2.5.0.2 Description

Specifies the HTTP message-body of a payload section.

2.5.0.3 Example

+ Body

        {
            "message": "Hello"
        }

2.6 6. Schema section

2.6.0.1 Definition

Defined by the Schema keyword in Markdown list entity.

+ Schema

2.6.0.2 Description

Specifies a validation schema for the HTTP message-body of parent payload section.


2.7 7. Metadata section

2.7.0.1 Definition

Key value pairs. Key separated from its value by colon (:). One pair per line. Starts at the beginning of the document and ends with the first Markdown element that is not recognized as a key value pair.

2.7.0.2 Description

Metadata keys and its values are tool-specific. Refer to relevant tool documentation for the list of supported keys.

2.7.0.3 Example

FORMAT: 1A
HOST: http://blog.acme.com

2.8 8. API name & overview section

2.8.0.1 Definition

Defined by the first Markdown header in the blueprint document unless it represents another section definition.

2.8.0.2 Description

Name and description of the API

2.8.0.3 Example

# Basic ACME Blog API
Welcome to the **ACME Blog** API. This API provides access to the **ACME Blog** service.

2.9 9. Resource group section

2.9.0.1 Definition

Defined by the Group keyword followed by group name (identifier):

# Group <identifier>

2.9.0.2 Description

This sections represents a group of resources (Resource Sections). May include one or more nested Resource Sections.

2.9.0.3 Example

# Group Blog Posts

## Resource 1 [/resource1]

 ...

# Group Authors
Resources in this groups are related to **ACME Blog** authors.    

## Resource 2 [/resource2]

 ...

2.10 10. Resource section

2.10.0.1 Definition

Defined by an URI template:

# <URI template>

– or –

Defined by a resource name (identifier) followed by an URI template enclosed in square brackets [].

# <identifier> [<URI template>]

– or –

Defined by an HTTP request method followed by URI template:

# <HTTP request method> <URI template>

NOTE: In this case the rest of this section represents the Action section including its description and nested sections and follows the rules of Action section instead.

2.10.0.2 Description

An API resource as specified by its URI or a set of resources (a resource template) matching its URI template.

This section should include at least one nested Action section and may include following nested sections:

NOTE: A blueprint document may contain multiple sections for the same resource (or resource set), as long as their HTTP methods differ. However it is considered good practice to group multiple HTTP methods under one resource (resource set).

2.10.0.3 Example

# Blog Posts [/posts/{id}]
Resource representing **ACME Blog** posts.
# /posts/{id}
# GET /posts/{id}

2.11 11. Resource model section

2.11.0.1 Definition

Defined by the Model keyword followed by an optional media type:

+ Model (<media type>)

2.11.0.2 Description

A resource manifestation - one exemplar representation of the resource in the form of a payload.

2.11.0.3 Referencing

The payload defined in this section may be referenced in any response or request section in the document using parent section’s identifier. You can refer to this payload in any of the following Request or Response payload sections using the Markdown implicit reference syntax.

2.11.0.4 Example

# My Resource [/resource]
+ Model (text/plain)
    
        Hello World
        
## Retrieve My Resource [GET]
+ Response 200
    
    [My Resource][]

2.12 12. URI parameters section

2.12.0.1 Definition

Defined by the Parameters keyword written in a Markdown list item:

+ Parameters

2.12.0.2 Description

Discussion of URI parameters in the scope of the parent section.

This section must be composed of nested list items only. This section must not contain any other elements. One list item per URI parameter. The nested list items subsections inherit from the Named section and are subject to additional formatting as follows:

+ <parameter name> = `<default value>` (required | optional , <type>, `<example value>`) ... <description>

    <additional description>
    
    + Values
        + `<enumeration element 1>` 
        + `<enumeration element 2>`
        ...
        + `<enumeration element N>`

Where:

NOTE: This section should only contain parameters that are specified in the parents’ URI template, and does not have to list every URI parameter.

2.12.0.3 Example

# GET /posts/{id}
+ Parameters
    + id ... Id of a post.
+ Parameters
    + id (number) ... Id of a post.
+ Parameters
    + id (required, number, `1001`) ... Id of a post.
+ Parameters
    + id = `20` (optional, number, `1001`) ... Id of a post.
+ Parameters
    + id (string)

        Id of a Post

        + Values
            + `A`
            + `B`
            + `C`

2.13 13. Action section

2.13.0.1 Definition

Defined by an HTTP request method:

## <HTTP request method>

– or –

Defined by an action name (identifier) followed by an HTTP request method enclosed in square brackets [].

# <identifier> [<HTTP request method>]

2.13.0.2 Description

Definition of at least one complete HTTP transaction as performed with the parent resource section. An action section may consists of multiple HTTP transaction examples for the given HTTP request method.

This section may include one nested URI parameters section describing any URI parameters specific to the action – URI parameters discussed in the scope of an Action section apply to the respective Action section ONLY.

It should include at least one nested Response section and may include additional nested Request and Response sections.

Nested Request and Response sections may be ordered into groups where each groups represent one transaction example. First transaction example group starts with the first nested Request or Response section. Subsequent groups start with the first nested Request section following a Response section.

Multiple Request and Response nested sections within one transaction example should have different identifiers.

2.13.0.3 Example

# Blog Posts [/posts{?limit}]
 ...    

## Retrieve Blog Posts [GET]
Retrieves the list of **ACME Blog** posts.

+ Parameters
    + limit (optional, number) ... Maximum number of posts to retrieve

+ Response 200

        ...

### Create a Post [POST]
+ Request

        ...

+ Response 201

        ...

2.13.0.4 Example Multiple Transaction Examples

# Resource [/resource]
## Create Resource [POST]

+ request A
    
        ...

+ response 200
    
        ...

+ request B
    
        ...

+ response 200    
        
        ...

+ response 500
    
        ...

+ request C
    
        ...

+ request D
    
        ...

+ response 200
    
        ...

NOTE: The “Multiple Transaction Examples” example demonstrates three transaction examples for one given action:

  1. 1st example: request A, response 200
  2. 2nd example: request B, responses 200 and 500
  3. 3rd example: requests C and D, response 200

2.14 14. Request section

2.14.0.1 Definition

Defined by the Request keyword followed by an optional identifier:

+ Request <identifier> (<Media Type>)

2.14.0.2 Description

One HTTP request-message example – payload.

2.14.0.3 Example

+ Request Create Blog Post (application/json)
    
        { "message" : "Hello World." }

2.15 15. Response section

2.15.0.1 Definition

Defined by the Response keyword. The response section definition should include an HTTP status code as its identifier.

+ Response <HTTP status code> (<Media Type>)

2.15.0.2 Description

One HTTP response-message example – payload.

2.15.0.3 Example

+ Response 201 (application/json)
    
            { "message" : "created" }


3 III. Appendix

3.1 1. URI Templates

The API Blueprint uses a subset of RFC6570 to define a resource URI Template.

3.1.1 URI Path Segment

At its simplest form – without any variables – a path segment of an URI Template is identical to an URI path segment:

/path/to/resources/42

3.1.2 URI Template Variable

Variable names are case-sensitive. The variable name may consists of following characters only:

Multiple variables are separated by the comma without any leading or trailing spaces. A variable(s) must be enclosed in braces – {} without any additional leading or trailing whitespace.

3.1.2.1 Operators

The first variable in the braces might be preceded by an operator. API Blueprint currently supports following operators:

3.1.2.2 Examples

{var}
{var1,var2,var3}
{#var}
{+var}
{?var}
{?var1,var2}
{?%24var}
{&var}

NOTE: The explode variable modifier is also supported. Refer to RFC6570 for its description.

3.1.2.3 Variable Reserved Values

Following characters are reserved in variable values:

: / / / ? / # / [ / ] / @ / ! / $ / & / ' / ( / ) / * / + / , / ; / =

3.1.3 Path Segment Variable

Simple path segment component variable is defined without any operator:

/path/to/resources/{var}

With var := 42 the expansion is /path/to/resources/42.

NOTE: RFC6570 – Level 1

3.1.4 Fragment Identifier Variable

URI Template variables for fragment identifiers are defined using the crosshatch (#) operator:

/path/to/resources/42{#var}

With var := my_id the expansion is /path/to/resources/42#my_id.

NOTE: RFC6570 – Level 2

3.1.5 Variable with Reserved Characters Values

To define URI Template variables with reserved URI characters use the plus (+) operator:

/path/{+var}/42

With var := to/resources the expansion is /path/to/resources/42.

NOTE: RFC6570 – Level 2

3.1.6 Form-style Query Variable

To define variables for a form-style query use the question mark (?) operator

/path/to/resources/{varone}{?vartwo}

With varone := 42 and vartwo = hello the expansion is /path/to/resources/42?vartwo=hello.

To continue a form-style query use the ampersand (&) operator:

/path/to/resources/{varone}?path=test{&vartwo,varthree}

With varone := 42, vartwo = hello, varthree = 1024 the expansion is /path/to/resources/42?path=test&vartwo=hello&varthree=1024.

NOTE: RFC6570 – Part of Level 3