RKAPIService
使用 Combine 发布器或 Swift 的原生并发 "async/await" 并执行简单的 Restful API 操作。 Apple 仅在 iOS 15.0, macOS 12.0, watchOS 8.0 和 tvOS 15.0 以上版本提供 URLSession
的 async/await API,但 Swift 并发支持从 iOS 13.0, macOS 10.15, watchOS 6.0 和 tvOS 13.0 开始。 RKAPIService
允许开发者在 iOS 13.0, macOS 10.15, watchOS 6.0 或 tvOS 13.0 以下版本使用这些 URLSession
async/await 操作。
注意:目前我们仅支持 URLSession.dataTask
。其他功能即将推出。
RKAPIService 需要
RKAPIService 通过 Swift Package Manager 提供。 要安装它,只需按照以下步骤操作
https://github.com/TheRakiburKhan/RKAPIService.git
RKAPIService
前缀的库添加为包依赖项
.package(url: "https://github.com/TheRakiburKhan/RKAPIService.git", from: "3.0.0")
导入 RKAPIService
创建 RKAPIService
的实例。 开发人员也可以通过键入 RKAPIService.shared
来使用共享实例。
使用 func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) async throws -> NetworkResult<Data>
调用任何 URLSession.dataTask
操作。 这是一个抛出方法。
使用 func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error>
通过 Combine
发布器调用任何 URLSession.dataTask
操作。 这不是一个抛出方法。
如果开发人员想要执行简单的 HTTP GET 请求,那么还有另一个专门为此目的的 API,func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) async throws -> NetworkResult<Data>
。 这是一个抛出方法。
如果开发人员想要执行简单的 HTTP GET 请求,那么还有另一个专门为此目的的 API,func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error>
。 这不是一个抛出方法。
import Foundation
import RKAPIService
final class DataFetchService {
let apiService = RKAPIService.shared
//If you want to use any type of HTTP Request
func fetchDataWithBody(url: URL?, method: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) async {
do {
let reply = try await apiService.fetchItemsByHTTPMethod(urlLink: url, httpMethod: method, body: body, additionalHeader: additionalHeader)
//Handle your data and response code however you like
//Printing Optional<Data>
debugPrint(reply.data)
//Printing HTTPStatusCode
debugPrint(reply.response)
} catch(let error) {
// Handle any exception or Error
}
}
// If you want to use HTTP Get Request only
func fetchData(url: URL?, additionalHeader: [HTTPHeader]?) async {
do {
let reply = try await apiService.fetchItems(urlLink: url, additionalHeader: additionalHeader)
//Handle your data and response code however you like
//Printing Optional<Data>
debugPrint(reply.data)
//Printing HTTPStatusCode
debugPrint(reply.response)
} catch(let error) {
// Handle any exception or Error
}
}
}
import Foundation
import Combine
import RKAPIService
final class DataFetchService {
let apiService = RKAPIService.shared
let cancellable = Set<AnyCancellable>()
//If you want to use any type of HTTP Request
func fetchDataWithBody(url: URL?, method: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) {
apiService.fetchItemsByHTTPMethod(urlLink: url, httpMethod: method, body: body, additionalHeader: additionalHeader)
//Receiving on Main Thread
.receive(on: DispatchQueue.main)
.sink { reply in
switch reply {
case .finished:
// After finishing a successful call
break
case .failure(let error):
// Handle any exception or Error
break
}
} receiveValue: { result in
//Handle your data and response code however you like
//Printing Optional<Data>
debugPrint(result.data)
//Printing HTTPStatusCode
debugPrint(result.response)
}
}
// If you want to use HTTP Get Request only
func fetchData(url: URL?, additionalHeader: [HTTPHeader]?) {
apiService.fetchItems(urlLink: url, additionalHeader: additionalHeader)
//Receiving on Main Thread
.receive(on: DispatchQueue.main)
.sink { reply in
switch reply {
case .finished:
// After finishing a successful call
break
case .failure(let error):
// Handle any exception or Error
break
}
} receiveValue: { result in
//Handle your data and response code however you like
//Printing Optional<Data>
debugPrint(result.data)
//Printing HTTPStatusCode
debugPrint(result.response)
}
}
}
导入 RKAPIService
创建 RKAPIService
的实例。 开发人员也可以通过键入 RKAPIService.shared
来使用共享实例。
使用 func fetchItems(urlLink: URL?, _ completion: @escaping (Result<NetworkResult<Data>, Error>)-> Void )
调用任何 URLSession.dataTask
操作。 这是一个抛出方法
如果开发人员想要执行简单的 HTTP GET 请求,那么还有另一个专门为此目的的 API,func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, _ completion: @escaping (Result<NetworkResult<Data>, Error>)-> Void )
import Foundation
import RKAPIService
final class DataFetchService {
let apiService = RKAPIService.shared
//If you want to use any type of HTTP Request
func fetchDataWithBody(url: URL?, method: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) {
apiService.fetchItemsByHTTPMethod(urlLink: url, httpMethod: method, body: body, additionalHeader: additionalHeader) { result in
switch result {
case .success(let reply):
//Handle your data and response code however you like
//Printing Optional<Data>
debugPrint(reply.data)
//Printing HTTPStatusCode
debugPrint(reply.response)
case .failure(let error):
// Handle any exception or Error
debugPrint(error)
}
}
}
// If you want to use HTTP Get Request only
func fetchData(url: URL?, additionalHeader: [HTTPHeader]?) {
apiService.fetchItems(urlLink: url, additionalHeader: additionalHeader) { result in
switch result {
case .success(let reply):
//Handle your data and response code however you like
//Printing Optional<Data>
debugPrint(reply.data)
//Printing HTTPStatusCode
debugPrint(reply.response)
case .failure(let error):
// Handle any exception or Error
debugPrint(error)
}
}
}
}
在处理网络通信时,URL
是主要组件。 此软件包中包含一个简单的 URLBuilder,可以正确且轻松地构建 URL
。
buildURL(scheme: String, baseURL: String, portNo: Int?, path: String?, queries: [URLQueryItem]?)
返回一个 URL?
buildURL(string: String, filter: CharacterSet)
返回一个 URL?
这是一个构建 Swift Pacakge Manager 页面链接的简单 URL 的示例。
RKAPIHelper.buildURL(scheme: "https", baseURL: "swift.org", portNo: nil, path: "/package-manager", queries: nil)
RKAPIHelper.buildURL(string: "https://swiftlang.cn/package-manager", filter: .urlQueryAllowed)
Rakibur Khan,通过 电子邮件 与我联系或访问我的 网站
此软件包已获得 MIT 许可证的许可。 请参阅 LICENSE 文件。
所有更改都记录在 CHANGELOG 文件中。