翻译自 API Design Guide - Documentationnode
这一章是为 API 添加内部文档的指南。大部分 API 有概述、教程和更高级别的参考文档(此指南不讨论)。API 名、资源名和方法名的信息请查看命名约定。git
在 .proto
文件中使用 //
来添加注释。json
// Creates a shelf in the library, and returns the new Shelf. rpc CreateShelf(CreateShelfRequest) returns (Shelf) { option (google.api.http) = { post: "/v1/shelves" body: "shelf" }; }
做为在 .proto
文件中添加注释的替代方法,你能够在 API 的 YAML 服务配置文件中添加注释。若是两个文件中都记录了相同的元素,则该文件中的文档将优先于 .proto
中的文档api
documentation: summary: Gets and lists social activities overview: A simple example service that lets you get and list possible social activities rules: - selector: google.social.Social.GetActivity description: Gets a social activity. If the activity does not exist, returns Code.NOT_FOUND. ...
当在一个 .proto
文件中有多个服务而且要提供特定于服务的文档时,你就须要上面的方法。能够在 YAML 中添加 overview
详细记录 API 的描述信息。可是,通常推荐将注释添加到 .proto
。app
与 .proto
注释同样,能够在 YAML 文件的注释中使用 Markdown 来提供其余格式。ide
API 描述是一个描述此 API 能作什么,以动词开始的句子。在 .proto
文件中,API 描述以注释的方法添加到对应的 service
上,例如:post
// Manages books and shelves in a simple digital library. service LibraryService { ... }
API 描述的其余示例:ui
Shares updates, photos, videos, and more with your friends around the world.this
Accesses a cloud-hosted machine learning service that makes it easy to build smart apps that respond to streams of data.google
顾名思义资源描述用来描述资源表明的什么东西。在 .proto
文件中,资源描述做为注释添加到对应消息上,例如:
// A book resource in the Library API. message Book { ... }
资源描述的其余示例:
A task on the user's to-do list. Each task has a unique priority.
An event on the user's calendar.
用于描述字段和参数的定义,一些示例:
The number of topics in this series.
The accuracy of the latitude and longitude coordinates, in meters. Must be non-negative.
Flag governing whether attachment URL values are returned for submission resources in this series. The default value for series.insert
is true
.
The container for voting information. Present only when voting information is recorded.
Not currently used or deprecated.
字段和参数描述:
必须清楚地描述边界条件(描述哪些值有效哪些值无效。API 用户会尽最大的可能来误用服务,并且不能阅读底层代码来弄清楚)
必须指定默认值和默认行为
当是字符串时,要描述语法、容许的字符和须要的编码格式,例如
1-255 characters in the set [A-a0-9]
A valid URL path string starting with / that follows the RFC 2332 conventions. Max length is 500 characters.
若是能够的话,提供示例
当字段 必须、仅输入、仅输出 时,必须在字段描述的开始进行说明。默认状况下全部字段和参数都是可选的。例如:
message Table { // Required. The resource name of the table. string name = 1; // Input only. Whether to dry run the table creation. bool dryrun = 2; // Output only. The timestamp when the table was created. Assigned by // the server. Timestamp create_time = 3; // The display name of the table. string display_name = 4; }
方法描述用来讲明方法的做用和操做的资源。一般以第三人称的如今时动词开始。示例:
Lists calendar events for the authenticated user.
Updates a calendar event with the data included in the request.
Deletes a location record from the authenticated user's location history.
Creates or updates a location record in the authenticated user's location history using the data included in the request. If a location resource already exists with the same timestamp value, the data provided overwrites the existing data.
确保描述简洁完整而且容易被理解。不能仅作重述,例如 series.insert
方法的描述不能简单地写成 "Inserts a series"。命名应该包含丰富的信息,大多数读者会阅读描述,由于他们须要比名字自己更多的信息。若是不知道在描述中要写什么,试着回答下面的问题:
它是什么
成功或失败后会致使什么。什么会致使失败。
是幂等的吗
单位是什么(例如:米、度、像素)
接收参数的范围
反作用是什么
如何使用
常见错误是什么
是否老是存在(例如:"Container for voting information. Present only when voting information is recorded.")
是否有默认值
本节列出了文本描述和文档的一些使用惯例。使用 ID
表示标识符(而不使用 Id
或 id
)。使用 JSON
表示数据格式(而不使用 Json
或 json
)。全部字段/参数使用 code font
的格式,字符串要用引号括起来。
ID
JSON
RPC
REST
property_name
或 "string_literal"
true
/ false
和命名约定同样,在写注释时推荐使用简洁一致的词语和风格,让母语非英语的读者容易理解。所以要避免行话、俚语、复杂的隐喻、流行文化或其余不容易理解的内容。使用友好专业的风格直接与读者“交谈”,并尽量保持注释的简洁。请记住,大部分读者只想知道如何使用 API,而不是阅读你的文档!