Skip to content
Snippets Groups Projects

RestUtils

Basics

To start using RestUtils, subclass RestManager. This will allow you to configure the behaviour of the library by overriding the appropiate methods and variables. For instance: baseURLString: this is the base of the url for your requests. defaultHeader: the headers that will be used in all requests. successBehaviour and failureBehaviour: these methods will be called when the api responds depending on whether the request had any errors. shouldMakeRequest: this method is used to block requests to certain endpoints.

Requests

To perform a request, use the method call of RestManager. This method has several overloads with different combinations of parameters that allow to make the request in the most suitable way. The method is generic, and will take an Output type and optionally an Input type if required. Input must conform to the protocol Codable, while Output must conform to the custom protocol ResponseObject (which in turn inherits from Codable). You can make an object conform to ResponseObject by simply typing: struct YourObject: ResponseObject or, if the model of your response is an array, you can use make it conform to the protocol by typing: extension Array: ResponseObject where Element == YourObject or

typealias YourObjectList = [YourObject]
extension YourObjectList: ResponseObject {}

Normally, the Output type will be specified through the signature of the completion closure that is passed to the library.

The completion closure taken by the call method has a parameter of type Result<T, RestError>, which is typealiased to RestResult<T>. The type Result allows to wrap the success and failure case in a single variable, which then must be switched to extract the result status and data or error. RestError conforms to the protocol Error and contains many common error cases that the library (or the custom implementations) will return. Since it is an enum, cases cannot be added to it, but if needed, more cases can be added by using:

extension RestError {
    enum CustomError {
        case myError
    }
}

An example of usage:

api.call(.yourEndpoint, body: yourObject) { (result: RestResult<OutputType>) in
    switch result {
    case .success(let outputObject):
        // Code on success
    case .failure(let error):
        // Code on failure
    }
}

By default, parallel requests are not supported: instead, the library handles the requests such that they are automatically made sequentally.

Parallel calls are now supported!! For activation you have to implement the var "singleThread" to false:

 override var singleThread: Bool {
        return false
    }

Endpoints

Create instances of the struct Endpoint (it is recommeded to make them static) specifying the path, HTTP method and headers.

Headers

They are modelled by the class HeaderField. There are already created several common headers, but you can create your own. If you need futher customization, you can also subclass HeaderField.