一个 Swift 包,为 HTTP REST 请求提供一个简化的接口,同时支持 Combine 和 async/await 模式。
GET
, POST
, PUT
, DELETE
)RestingError
案例进行无缝的错误处理async/await
特性进行异步请求处理Dictionary
或 Data
形式发送参数Data
和 Decodable
处理响应您可以将此包添加到您的 Package.swift 文件的 dependencies 值中。
dependencies: [
.package(url: "https://github.com/rocxteady/Resting.git", .upToNextMajor(from: "0.0.6"))
]
import Resting
let clientConfiguration = RestClientConfiguration(sessionConfiguration: .default, jsonDecoder: .init())
let restClient = RestClient(configuration: clientConfiguration)
let requestConfig = RequestConfiguration(
urlString: "https://api.example.com/data",
method: .get
)
// Decodable as response
do {
let data: YourDecodableModel = try await restClient.fetch(with: requestConfig)
// Handle the data
} catch {
// Handle the error
}
// Data as response
do {
let data = try await restClient.fetch(with: requestConfig)
// Handle the data
} catch {
// Handle the error
}
// Decodable as response
let cancellable :AnyPublisher<YourDecodableModel, Error> = restClient.publisher(with: requestConfig)
cancellable.sink { completion in
// Handle completion or error
} receiveValue: { (data: YourDecodableModel) in
// Handle the data
}
// Data as response
restClient.publisher(with: requestConfig)
.sink { completion in
// Handle completion or error
} receiveValue: { data in
// Handle the data
}
该包提供了一个专门的 RestingError
枚举来优雅地处理错误。它包括 .urlMalformed
、.statusCode
、.wrongParameterType
和 .unknown
等案例。请务必将这些包含在您的错误处理逻辑中。
该包支持以下语言
如果您想贡献其他语言的翻译,请在我们的 GitHub 存储库上创建一个新的 issue 或提交一个 pull request。
我们感谢您的贡献!如果您有任何建议、功能请求或错误报告,请在我们的 GitHub 存储库上创建一个新的 issue。
该包在 MIT 许可下可用。有关更多信息,请参见 LICENSE 文件。