APIs
apis[name]
ApiMethodProxy app.apis[String apiName]
调用 API 网关接口。支持通过下标运算符获取 API 代理对象,进行链式调用。支持 GET、POST、PUT、DELETE、HEAD、OPTIONS、PATCH 方法。
参数
apiName
String
API 名称
返回
ApiMethodProxy
ApiMethodProxy
API 方法代理对象,支持链式调用
ApiResponse
ApiResponse
API 响应
示例
- 链式调用
- 直接调用 callApi
// POST 请求
final result = await app.apis['myApi'].post(
path: '/users',
body: {'name': '张三', 'age': 25},
);
if (result.isSuccess) {
print('响应数据: ${result.data}');
}
// GET 请求
final getResult = await app.apis['myApi'].get(path: '/users/123');
// PUT 请求
final putResult = await app.apis['myApi'].put(
path: '/users/123',
body: {'name': '李四'},
);
// DELETE 请求
final deleteResult = await app.apis['myApi'].delete(path: '/users/123');
final result = await app.apis.callApi(CallApiOptions(
name: 'myApi',
method: 'POST',
path: '/users',
body: {'name': 'test'},
headers: {'X-Custom': 'value'},
));
if (result.isSuccess) {
print('响应: ${result.data}');
}
api
ApiMethodProxy app.apis.api(String apiName)
按名称取得 API 代理。之后的调用方式与 app.apis[apiName] 相同。
参数
apiName
String
API 名称
返回
ApiMethodProxy
ApiMethodProxy
API 方法代理对象
示例
- 示例
final myApi = app.apis.api('myApi');
final result = await myApi.get(path: '/users');
callApi
Future<ApiResponse> app.apis.callApi(CallApiOptions options)
直接调用 API。path 不以 / 开头时会自动补上。method 无法识别时按 POST 发送。
参数
options
CallApiOptions
调用参数
返回
Future
ApiResponse
API 响应
示例
- 示例
final result = await app.apis.callApi(CallApiOptions(
name: 'myApi',
method: 'POST',
path: '/users',
body: {'name': 'test'},
));
gatewayOrigin
String? app.apis.gatewayOrigin
公开字段。当前请求不读取它,赋值不会改变实际请求地址。