阿里·斯威夫特编码
请注意: 生成的代码尚不支持 swift-tools-version 6 或超过。 请跟踪这个问题的状况 这里同时请设置您的 swift-tools-version 改为 5.10 或低一点.
设置
1) 将 Swift 客户端生成器添加到您的 arri 配置
// arri.config.ts
import { defineConfig, generators } from 'arri';
export default defineConfig({
generators: [
generators.swiftClient({
clientName: 'MyClient',
outputFile: './client/Sources/MyClient.g.swift',
}),
],
});
选项 :
| 名称 | 说明 |
|---|---|
| 输出文件( 需要) | 由生成器创建的文件路径 |
| 客户端Name | 生成客户端的名称 |
| 类型 前缀 | 在所有生成类型中添加前缀 |
| 根服务 | 生成客户端的根服务 |
(2) 安装 Swift 客户端库
生成的代码依赖于 阿里·斯威夫特客户端 图书馆,请务必把它添加到你的快速项目中。 版本号应该与您的 Arri CLI 版本相匹配 .
Swift 软件包管理器
添加数据 arri-client-swift git repo 到您的软件包依赖度
.package(
url: "https://github.com/modiimedia/arri-client-swift.git",
from: "<your-arri-cli-version>"
)
然后添加 ArriClient 作为你的目标的依赖
.target(
name: "MyApp",
dependencies: [
.product(name: "ArriClient", package: "arri-client-swift")
]
)
使用生成代码
初始化客户端
let client = MyClient(
baseURL: "https://example.com",
delegate: DefaultRequestDelegate(),
headers: {
var headers: Dictionary<String, String> = Dictionary()
return headers
},
// optional
onError: { err in
// do something
}
)
await client.myProcedure()
根客户端将是一个包含所有子服务和程序的结构. 如果你只需要特殊服务,你可以初始化服务.
例如,如果我们将一些程序归类于以下: "users" 我们可以像这样初始化用户服务.
let usersService = MyClientUsersService(
baseURL: "https://example.com",
delegate: DefaultRequestDelegate(),
headers: {
var headers: Dictionary<String, String> = Dictionary()
return headers
}
)
usersService.someProcedure()
使用生成类型
所有产生的结构、班级和有标签的工会都执行 ArriClientModel 协议,它看起来像:
public protocol ArriClientModel: Equatable {
init()
init(json: JSON)
init(JSONString: String)
func toJSONString() -> String
func toURLQueryParts() -> [URLQueryItem]
func clone() -> Self
}
所有生成的标准enums执行 ArriClientEnum 协议,它看起来像:
public protocol ArriClientEnum: Equatable {
init()
init(serialValue: String)
func serialValue() -> String
}
调用事件流程序
程序生成a 任务
var msgCount = 0
var openCount = 0
let params = WatchUserParams()
// event stream procedures return a task that you can cancel whenever
let task: Task<(), Never> = client.users.watchUser(
params,
options: EventSourceOptions(
onMessage: { msg, eventSource in
msgCount += 1
print("New message: \(msg)")
},
onRequest: nil,
onRequestError: nil,
onResponse: { _, eventSource in
openCount += 1
print("Established connection!")
},
onResponseError: { err, eventSource in
print("The server returned an error: \(err)")
// you can also cancel the task from inside one of these hooks
// by calling `cancel()` on the EventSource.
// this will cause the parent Task to be completed
eventSource.cancel()
},
onClose: nil,
maxRetryCount: nil,
maxRetryInterval: nil,
)
)
// if you want to wait for the task to finished
await task.result
// this will continue indefinitely unless the server sends a "done" event
// or you call `cancel()` on the EventSource
可用事件源选项
onMessage- 一旦从服务器收到"消息"事件,即关闭发射装置。 这是唯一必要的选择.onRequest- 当一项请求已经提出但尚未执行时,关闭该请求.onRequestError- 关闭在创建请求时发生错误(即错误的URL),或者无法连接到服务器时点火。 (即:connectionRefused错误)onResponse- 当我们收到服务器的回复时关闭起火器onResponseError- 关闭服务器未使用状态代码进行响应时起火200- 说吧299或Content-Type页眉不包含text/event-streamonClose- 当事件源被关闭时关闭火灾。 (这只有在事件源代码已经能够成功收到服务器的响应时才会开火. )maxRetryCount- 限制事件源试图重新连接到服务器的次数。 设定为nil它将无限期地重试。 (过失是)nil(中文(简体) )maxRetryInterval- 设定每毫秒重试之间的最大延迟时间。 默认是30000。 。 。 .
补充说明
目前 DefaultRequestDelegate() 依赖 AsyncHTTPC 键. 我想最终去除这种依赖,所以如果有人知道如何让服务器发送事件(Server Sent Events)与斯威夫特基金会图书馆合作,请打开一个问题. 请注意,这些客户端需要运行Linux,所以拟议解决方案需要工作而不使用 URLSession.asyncBytes 或其他任何只在苹果平台工作的API.