Make simple API request using functional programming
The Restler framework has been built to use features of the newest versions of Swift. Inspiration for it is the 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 a nice interface for making API requests the easiest as possible and the fastest as possible.
We think that README isn’t a good place for complete documentation so that’s why we decided to generate it to a folder inside the framework repository. Full documentation you can find in the Documentation folder. If you’re looking for a description of a specific protocol or class, we put here a list of the most important symbols.
RestlerType - it’s the main protocol which should be used when it comes to mocking or using Restler’s class’ instance.
All these protocols are defined in one file: RestlerRequestBuilderType
Restler.Request - generic class for all request types provided by Restler.
unknownError
.Nothing is easier there - you just add the framework to the Swift Package Manager dependencies if you use one.
Otherwise you can use CocoaPods. If you use one simply add to your Podfile
:
...
pod 'Restler/Core'
...
It’s important to specify it with /Core
! (Changed in v1.0)
and call in your console:
pod install
Import the framework to the project:
import RestlerCore
and call it!
If you don’t want to add the same error to be parsed on a 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)
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 in the “Authorization” key, simply provide username and password to the header:
restler.header.setBasicAuthentication(username: "me", password: "password")
Restler(baseURL: myBaseURL)
.get(Endpoint.myProfile) // 1
.query(anEncodableQueryObject) // 2
.failureDecode(ErrorToDecodeOnFailure.self) // 3
.setInHeader("myNewTemporaryToken", forKey: "token") // 4
.receive(on: .main) // 5
.decode(Profile.self) // 6
// 7
.subscribe(
onSuccess: { profile in // 8
updateProfile(with: profile)
},
onCompletion: { _ in // 9
hideLoadingIndicator()
})
Restler(baseURL: myBaseURL)
.post(Endpoint.myProfile) // 1
.body(anEncodableQueryObject) // 2
.failureDecode(ErrorToDecodeOnFailure.self)
.decode(Profile.self)
.subscribe(
onFailure: { error in // 3
print("\(error)")
},
onCompletion: { _ in
hideLoadingIndicator()
})
Any other method call is very similar to these two, but if you have questions simply create an issue.
Restler(baseURL: myBaseURL)
.get("/profile") // 1
.decode(Profile.self) // 2
.publisher // 3
.catch { _ in Empty() } // 4
.assign(to: \.profile, on: self) // 5
.store(in: &subscriptions) // 6
Profile
object.
Restler(baseURL: myBaseURL)
.get(Endpoint.myProfile) // 1
.query(anEncodableQueryObject) // 2
.publisher()? // 3
.receive(on: DispatchQueue.main) // 4
.map(\.data) // 5
.decode(type: Profile.self, decoder: JSONDecoder()) // 6
.catch { _ in Empty() } // 7
.assign(to: \.profile, on: self) // 8
.store(in: &subscriptions) // 9
DataTaskPublisher
.Profile
object.First of all, you need to add RxRestler
to your target you can do it simply in SPM. In CocoaPods you should add to your Podfile:
pod `Restler/Rx`
Then import RxRestler
to every file it’s needed.
Restler(baseURL: myBaseURL)
.get(Endpoint.myProfile)
.query(anEncodableQueryObject)
.receive(on: .main) // 1
.decode(Profile.self) // 2
.rx // 3
.subscribe( // 4
onSuccess: { print("This is my profile:", $0) },
onError: { print("This is an error:", $0) })
.disposed(by: bag) // 5
Single<Profile>
in this case.Disposable
to the DisposeBag
. The networking task will be canceled automatically if the bag
will deinitialize.If you want to contribute in this framework, simply put your pull request here.
If you have found any bug, file it in the issues.
If you would like Restler to do something else, create an issue with a feature request.
./Scripts/configure.sh
Restler-Example/Restler-Example/Configuration
named Debug.xcconfig
with needed information.Restler-Example
. You can do it from the terminal: open Restler-Example/Restler-Example.xcodeproj
Run command ./Scripts/pod_lib_lint.rb Restler.podspec
to lint the podspec before pushing changes to the repo.
cd Scripts/releaseTool
swift run ReleaseTool release ../..