Swiftpack.co - TheRakiburKhan/RKAPIService as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by TheRakiburKhan.
TheRakiburKhan/RKAPIService 2.0.0
RKAPIService is a simple and handy API Service utilizes swift concurrency aka async/await
⭐️ 0
🕓 2 days ago
iOS macOS
.package(url: "https://github.com/TheRakiburKhan/RKAPIService.git", from: "2.0.0")

RKAPIService

Platforms Support Swift Package Manager Swift Version iOS Version macOS Version XCode Version

RKAPIService uses 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 URLSession async/await operations down to iOS 13.0 or macOS 10.15

N.B: Currently we support URLSession.dataTask only. Rest is coming soon.

Table of Contents

System Requirments

RKAPIService requires

  • iOS 9.0 or above
  • macOS 10.10 or above
  • XCode 9.0 or above

Installations

RKAPIService is available through Swift Package Manager. To install it, simply follow the steps:

  1. In Xcode, select File > Swift Packages > Add Package Dependency.
  2. Follow the prompts using the URL for this repository
  3. Select the RKAPIService-prefixed libraries you want to use

Usage

For iOS 13.0+ and macOS 10.15+

  • Import RKAPIService

  • 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?) async throws -> NetworkResult<Data> 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 fetchItems(urlLink: URL?) async throws -> NetworkResult<Data>. This is a Throwing method.

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?) async {
        do {
            let reply = try await apiService.fetchItemsByHTTPMethod(urlLink: url, httpMethod: method, body: body)
            
            //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)async {
        do {
            let reply = try await apiService.fetchItems(urlLink: url)
            
            //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
        }
    }
}

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?) {
        apiService.fetchItemsByHTTPMethod(urlLink: url, httpMethod: method, body: body) { 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) {
         apiService.fetchItems(urlLink: url) { 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)
                    
            }
        }
    }
}

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.

GitHub

link
Stars: 0
Last commit: 2 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Added Compatibility
Yesterday

Version 2.0.0 (Latest)

  • Upgraded support from iOS 8 to iOS 9.
  • Added dedicated get request method for iOS 9 and macOS 10.10
  • Updated code documentation. Now developers can utilize the new XCode 13 Build Documentation feature.
  • HTTPStatusCode.ResponseType is now @frozen

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics