Restler

restler-1.2-branch

Make simple API request using functional programming
railwaymen/restler

Restler

pipeline status

coverage report

The Restler framework has been built to use features of the newest versions of Swift. Inspiration for it is Vapor library for building Server-side with Swift. What we love is functional programming, so you can build your desired request just calling some chained functions. The main goal of the framework is to provide nice interface for making API requests the easiest as possible and the fastest as possible.

List of Content

Instalation

Nothing is easier there - you just add the framework to the Swift Package Manager dependecies if you use one.

Otherwise you can use CocoaPods. If you use one simply add to your Podfile:

...
pod 'Restler'
...

and call in your console:

pod install

Usage

Import the framework to the project:

import Restler

and call it!

Examples

GET

Restler(baseURL: myBaseURL)
  .get(Endpoint.myProfile)
  // Makes GET request to the given endpoint.

  .query(anEncodableQueryObject)
  // Encodes object and puts it in query for GET request.

  .failureDecode(ErrorToDecodeOnFailure.self)
  // If error will occure, error parser would try to decode the given type.

  .setInHeader("myNewTemporaryToken", forKey: "token")
  // Sets the specified value for given key in the header only for this request.

  .decode(Profile.self)
  // Decodes Profile object on successful response. If it is not optional, still failure handler can be called.

  .onSuccess({ profile in
    updateProfile(with: profile)
  })
  // Handler called if Restler would successfully end request.

  .onCompletion({ _ in
    hideLoadingIndicator()
  })
  // Handler called on completion of the request whatever the result would be.

  .start()
  // Create and start data task.

POST

Restler(baseURL: myBaseURL)
  .post(Endpoint.myProfile)
  // Makes POST request to the given endpoint

  .body(anEncodableQueryObject)
  // Encodes object and puts it into body of the request. Ignored if request method doesn't support it.

  .failureDecode(ErrorToDecodeOnFailure.self)   
  .decode(Profile.self)
  .onFailure({ profile in
    updateProfile(with: profile)
  })
  // Handler called if request has failed.

  .onCompletion({ _ in
    hideLoadingIndicator()
  })
  .start()

Error parser

If you don't want to add the same error to be parsed on failure of every request, simply add the error directly to the error parser of the Restler object.

restler.errorParser.decode(ErrorToDecodeOnFailure.self)

If you don't want to decode it anymore, simply stop decoding it:

restler.errorParser.stopDecoding(ErrorToDecodeOnFailure.self)

Header

Setting header values is very easy. Simply set it as a dictionary:

restler.header = [
  .contentType: "application/json",
  .cacheControl: "none",
  "customKey": "value"
]
restler.header[.cacheControl] = nil

If you're using basic authentication at "Authorization" key, simply provide username and password to header:

restler.header.setBasicAuthentication(username: "me", password: "password")

Contribution

Dependencies

Gems

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Jan 11 2024 06:33:26 GMT-1000 (Hawaii-Aleutian Standard Time)