RKAPIService uses Combine publishers or Swift’s native concurrency “async/await” and performs simple Restful API operations. Apple offers URLSession async/await API’s only above iOS 15.0 and macOS 12.0 but swift concurrency is supported from iOS 13.0 and macOS 10.15. RKAPIService let’s developer utilize those URLSessionasync/await operations down to iOS 13.0 or macOS 10.15
N.B: Currently we support URLSession.dataTask only. Rest is coming soon.
Create and instance of RKAPIService. Developer can also use the shared instance by typing RKAPIService.shared
Use func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) async throws -> NetworkResult<Data> for calling any URLSession.dataTask operations. This is a Throwing method.
Use func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error> for calling any URLSession.dataTask operations via Combine Publishers. This is non Throwing method.
If the developer want’s to do simple HTTP GET request then there is another dedicated API for that,
func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) async throws -> NetworkResult<Data>. This is a Throwing method.
If the developer want’s to do simple HTTP GET request then there is another dedicated API for that, func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error>. This is non Throwing method.
Example with 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
}
}
}
Example with Combine Publisher
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)
}
}
}
For iOS 9.0+ and macOS 10.10+
Import RKAPIService
Create and instance of RKAPIService. Developer can also use the shared instance by typing RKAPIService.shared
Use func fetchItems(urlLink: URL?, _ completion: @escaping (Result<NetworkResult<Data>, Error>)-> Void ) for calling any URLSession.dataTask operations. This is a Throwing method
If the developer want’s to do simple HTTP GET request then there is another dedicated API for that,
func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, _ completion: @escaping (Result<NetworkResult<Data>, Error>)-> Void )
Example
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)
}
}
}
}
Helper
When working with network communication, URL is the primary component. A simple URLBuilder is included with this package to build URL properly and effortlessly.
RKAPIService
RKAPIService
uses Combine publishers or Swift’s native concurrency “async/await” and performs simple Restful API operations. Apple offersURLSession
async/await API’s only above iOS 15.0 and macOS 12.0 but swift concurrency is supported from iOS 13.0 and macOS 10.15.RKAPIService
let’s developer utilize thoseURLSession
async/await operations down to iOS 13.0 or macOS 10.15N.B: Currently we support
URLSession.dataTask
only. Rest is coming soon.Table of Contents
System Requirments
RKAPIService requires
Installations
RKAPIService is available through Swift Package Manager. To install it, simply follow the steps:
RKAPIService
- prefixed libraries you want to useOR
Add as a package dependency
Usage
For iOS 13.0+ and macOS 10.15+
Import
RKAPIService
Create and instance of
RKAPIService
. Developer can also use the shared instance by typingRKAPIService.shared
Use
func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) async throws -> NetworkResult<Data>
for calling anyURLSession.dataTask
operations. This is a Throwing method.Use
func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error>
for calling anyURLSession.dataTask
operations viaCombine
Publishers. This is non Throwing method.If the developer want’s to do simple HTTP GET request then there is another dedicated API for that,
func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) async throws -> NetworkResult<Data>
. This is a Throwing method.If the developer want’s to do simple HTTP GET request then there is another dedicated API for that,
func fetchItems(urlLink: URL?, additionalHeader: [HTTPHeader]?) -> AnyPublisher<NetworkResult<Data>, Error>
. This is non Throwing method.Example with async/await
Example with Combine Publisher
For iOS 9.0+ and macOS 10.10+
Import
RKAPIService
Create and instance of
RKAPIService
. Developer can also use the shared instance by typingRKAPIService.shared
Use
func fetchItems(urlLink: URL?, _ completion: @escaping (Result<NetworkResult<Data>, Error>)-> Void )
for calling anyURLSession.dataTask
operations. This is a Throwing methodIf the developer want’s to do simple HTTP GET request then there is another dedicated API for that,
func fetchItemsByHTTPMethod(urlLink: URL?, httpMethod: HTTPMethod, body: Data?, _ completion: @escaping (Result<NetworkResult<Data>, Error>)-> Void )
Example
Helper
When working with network communication,
URL
is the primary component. A simple URLBuilder is included with this package to buildURL
properly and effortlessly.buildURL(scheme: String, baseURL: String, portNo: Int?, path: String?, queries: [URLQueryItem]?)
returns anURL?
buildURL(string: String, filter: CharacterSet)
returns anURL?
Example
Here is an exapmple of building simple url of Swift Pacakge Manager page link.
Author
Rakibur Khan, contact me via email or visit my website
License
This package is licensed under MIT License. See the LICENSE file.
Changelog
All changes are loggedd in CHANGELOG file.