定义API(Application Programming Interface)调用接口,是通过约定格式来配置传统WebAPI的Controller以及一些其他配置参数。使得开发人员可以节约成本并更方便的管理其开发接口。API(本文中后续的API统一指的是Dbrestful中的API)还可以模拟返回结果以及生成文档,这样可以使得接口在未开发完成时前端也可以根据接口文档并行开发。不必浪费时间等待后端接口开发完成。
[TOC]
配置API调用接口可以通过文件直接配置或通过PMT平台页面配置管理,配置文件以json
格式存储,通常,该配置代码文件存放在服务项目的API目录,也可以通过该项目的根目录下的config.json
文件中指定。
{
……
"APIPath": "API/"
……
}
API的详细定义,请参考Wicture.DbRESTFul
项目中Schema
目录下的api-schema.json
文件。它的主要结构如下:
{
"version": "",
"owner": "",
"updatedTime": "",
"name": "",
"module": "",
"url": "",
"useAbsoluteUrl": true,
"method": "",
"title": "",
"summary": "",
"note": "",
"allowAnonymous": false,
"cache": {
"enabled": false,
"type": "redis",
"expiration": 300
},
"implemented": true,
"implementation": {
"type": "repository",
"name": ""
},
"parameter": {
"query": [
{
"type": "",
"name": "",
"description": ""
}
],
"body": []
},
"result": {
"type": "json",
"schema": [
{
"name": "",
"type": "int",
"nullable": false,
"description": ""
},
{
"name": "errorMessage",
"type": "string",
"nullable": true,
"description": ""
},
{
"name": "data",
"type": "object",
"nullable": true,
"description": "",
"schema": [
{
"name": "items",
"type": "array",
"nullable": false,
"description": "",
"schema": [
{
"name": ",
"type": "int",
"nullable": false,
"description": ""
},...]
}]
]},
"mock": [
{
"input": {
"waitForStatus": "*",
"pageIndex": "*",
"pageSize": "*"
},
"output": {
"statusCode": 200,
"errorMessage": "",
"data": {
"items": "",
"pagination": ""
}
}
}
]
},
假设我们需要配置API基本信息我可以这样配置:
{
"version": "1.0",
"owner": "田成果",
"updatedTime": "2016-09-26T10:03:16",
"name": "ListOrders",
"module": "Order.Info",
"url": "/client/v1/order/list",
"useAbsoluteUrl": true,
"method": "GET",
"title": "获取订单列表",
"summary": "获取订单列表",
"note": "",
"allowAnonymous": false,
"cache": {
"enabled": false,
"type": "redis",
"expiration": 300
},
"implemented": true,
"implementation": {
"type": "repository",
"name": "OrderRepository.ListOrders"
},
//"implementation": {
//"type": "csi",
//"name": "ListOrders"
//},
}
说明:
version
:API
的版本号。owner
:API
的负责人。name
:名称,必须全局唯一。module
:API
所属模块。url
:API
的访问地址。useAbsoluteUrl
:是否绝对地址(否的时候,访问地址为API\模块名\url)method
:HTTP请求类型(GET POST DELETE PUT ect)useAbsoluteUrl
:是否绝对地址(否的时候,访问地址为API\模块名\url)title
:接口标题summary
:描述note
:备注allowAnonymous
:是否匿名访问,一般都要使用身份验证和权限控制,但总有部分接口是可以匿名访问的
useAbsoluteUrl
:是否绝对地址(否的时候,访问地址为API\模块名\url)
cache
: 缓存,暂时只支持redis
implemented
: 是否已实现,否的时候会返回mock中定义的返回结果。
implementation
: 实现方式,实现方式分为两种,一种为csi,一种为repository。(请参考Wicture.DbRESTFul
项目中docs
目录下的CSI
和repository
文件)
假设我们需要配置API请求信息我可以这样配置:
{
"parameter": {
"query": [
{
"type": "int",
"name": "id",
"nullable": true
"description": "{id:1}"
}
],
"body": [
{
"type": "object",
"name": "test"
}
]
},
}
说明:
parameter
:请求信息格式,分类两类:query以及body。query
:API
的URL参数。即在URL后跟上请求的定义参数。如:api/test?id=1body
:API
的表单参数。按raw方式请求,定义内容可以JSONnullable
:表示是否必须填写,如是则必须有内容。
假设我们需要配置API返回以及模拟信息我可以这样配置:
{
"result": {
"type": "json",
"schema": [
{
"name": "",
"type": "int",
"nullable": false,
"description": ""
},
{
"name": "errorMessage",
"type": "string",
"nullable": true,
"description": ""
},
{
"name": "data",
"type": "object",
"nullable": true,
"description": "",
"schema": [
{
"name": "items",
"type": "array",
"nullable": false,
"description": "",
"schema": [
{
"name": ",
"type": "int",
"nullable": false,
"description": ""
},...]
}]
]},
"mock": [
{
"input": {
"waitForStatus": "*",
"pageIndex": "*",
"pageSize": "*"
},
"output": {
"statusCode": 200,
"errorMessage": "",
"data": {
"items": "",
"pagination": ""
}
}
}
}
说明:
result
:返回类型默认是json。schema
:result
中的schema
指的是返回集合定义。可以是多个返回参数的集合。nullable
:表示是否必须填写,如是则必须有内容。type
:返回字段类型。string,int,object,array etc...mock
:模拟返回信息,当implemented
是false时才会生效。返回内容可以是多条。条件根据input
设置匹配input
:模拟的请求参数,为json格式。*
表示所有参数,不然则按照定义的返回output
:模拟返回字段,为json格式。