RequestKit

3.3.0

The foundation of octokit.swift, TrashCanKit, TanukiKit and VloggerKit
nerdishbynature/RequestKit

What's New

3.3.0

2024-06-16T09:38:23Z

What's Changed

  • Add overloading with dateDecodingStrategy for post by @417-72KI in #96
  • Make type for load/post just Decodable by @417-72KI in #97

Full Changelog: 3.2.1...3.3.0

RequestKit

codecov.io

The base of octokit.swift, TanukiKit, TrashCanKit and VloggerKit.

Installation

Carthage

# Cartfile
github "nerdishbynature/RequestKit"

CocoaPods

# Podfile
pod "NBNRequestKit"

Usage

To make a request using RequestKit you will need three parts: a Router, a Configuration and usually an object that know both and connects them. See OctoKit.

Defining a Router

Router are defined by the Router protocol. It is recommended to define them as Enumerations having a case for every route.

This is what a basic router looks like:

enum MyRouter: Router {
    case getMyself(Configuration)

    var configuration: Configuration {
        switch self {
        case .getMyself(let config): return config
        }
    }

    var method: HTTPMethod {
        switch self {
        case .getMyself:
            return .GET
        }
    }

    var encoding: HTTPEncoding {
        switch self {
        case .getMyself:
            return .url
        }
    }

    var path: String {
        switch self {
        case .getMyself:
            return "myself"
        }
    }

    var params: [String: Any] {
        switch self {
        case .getMyself(_):
            return ["key1": "value1", "key2": "value2"]
        }
    }
}

Defining a Configuration

As RequestKit was designed to handle OAuth requests we needed something to store user credentials. This is where Configurations come into play. Configurations are defined in the Configuration protocol.

public struct TokenConfiguration: Configuration {
    public let accessToken: String?
    public let apiEndpoint = "https://my.webservice.example/api/2.0/"
    public let accessTokenFieldName = "access_token"
    public let errorDomain = "com.my.customErrorDomain"
    
    public init(_ accessToken: String? = nil) {
        self.accessToken = accessToken
    }
}

In the above Configuration the accessToken will be passed as a URL parameter named access_token with each request. Alternatively you can have the accessToken passed in an HTTP Authorization header by setting the authorizationHeader property to the desired token type. As an example the following Configuration passes it as a Bearer token.

public struct TokenConfiguration: Configuration {
    public let accessToken: String?
    public let apiEndpoint = "https://my.webservice.example/api/2.0/"
    public let authorizationHeader: String? = "Bearer"
    public let errorDomain = "com.my.customErrorDomain"
        
    public init(_ accessToken: String? = nil) {
        self.accessToken = accessToken
    }
}

Defining the binding object

We will need something that connects the router and the configuration to make provide a convenient interface. The common way of doing this is to use a struct or a class that does it for you.

struct User : Codable {
}

struct MyWebservice {
    var configuration: Configuration

    init(configuration: Configuration) {
        self.configuration = configuration
    }

    func getMyself(session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<User>) -> Void) -> URLSessionDataTaskProtocol? {
        let router = MyRouter.getMyself(self.configuration)
        return router.load(session, expectedResultType: User.self) { user, error in
            if let error = error {
                completion(Response.failure(error))
            } else if let user = user {
                completion(Response.success(user))
            }
        }
    }
}

Making a request

All your user has to do is call your MyWebservice:

let config = TokenConfiguration("123456")
MyWebservice(configuration:config).getMyself { response in
    switch response {
        case .success(let user):
            print(user)
        case .failure(let error):
            print(error)
        }
    }
}

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 09:24:25 GMT-0900 (Hawaii-Aleutian Daylight Time)