RKAPIService

Coding time tracker Platforms Support License Swift Package Manager Cocoapod Carthage Swift Version iOS Version macOS Version watchOS Version tvOS Version

RKAPIService 使用 Combine 发布器或 Swift 的原生并发 "async/await" 并执行简单的 Restful API 操作。 Apple 仅在 iOS 15.0, macOS 12.0, watchOS 8.0tvOS 15.0 以上版本提供 URLSession 的 async/await API,但 Swift 并发支持从 iOS 13.0, macOS 10.15, watchOS 6.0tvOS 13.0 开始。 RKAPIService 允许开发者在 iOS 13.0, macOS 10.15, watchOS 6.0tvOS 13.0 以下版本使用这些 URLSession async/await 操作。

注意:目前我们仅支持 URLSession.dataTask。其他功能即将推出。

目录

系统要求

RKAPIService 需要

安装

RKAPIService 通过 Swift Package Manager 提供。 要安装它,只需按照以下步骤操作

https://github.com/TheRakiburKhan/RKAPIService.git

添加为包依赖项

.package(url: "https://github.com/TheRakiburKhan/RKAPIService.git", from: "3.0.0")

使用方法

适用于 iOS 13.0+, macOS 10.15+, watchOS 6.0+ 或 tvOS 13.0+

如果开发人员想要执行简单的 HTTP GET 请求,那么还有另一个专门为此目的的 API,func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error>。 这不是一个抛出方法。

使用 async/await 的示例

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
        }
    }
}

使用 Combine 发布者的示例

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)
            }
    }
}

适用于 iOS 11.0+ 和 macOS 10.10+

示例

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

示例

这是一个构建 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 文件中。