Restofire is a protocol oriented networking client for Alamofire.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate Restofire into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
pod 'Restofire', '~> 5.0'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Restofire into your Xcode project using Carthage, specify it in your Cartfile
:
github "Restofire/Restofire" ~> 5.0
To use Restofire as a Swift Package Manager package just add the following in your Package.swift file.
import PackageDescription
let package = Package(
name: "HelloRestofire",
dependencies: [
.Package(url: "https://github.com/Restofire/Restofire.git", from: "5.0.0")
]
)
If you prefer not to use either of the aforementioned dependency managers, you can integrate Restofire into your project manually.
cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:$ git init
$ git submodule add https://github.com/Restofire/Restofire.git
$ git submodule update --init --recursive
Open the new Restofire
folder, and drag the Restofire.xcodeproj
into the Project Navigator of your application's Xcode project.
It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
Select the Restofire.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target.
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
In the tab bar at the top of that window, open the "General" panel.
Click on the +
button under the "Embedded Binaries" section.
You will see two different Restofire.xcodeproj
folders each with two different versions of the Restofire.framework
nested inside a Products
folder.
It does not matter which
Products
folder you choose from.
Select the Restofire.framework
& Alamofire.framework
.
And that's it!
The
Restofire.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
+
button under the "Embedded Binaries" section.Restofire.framework
& Alamofire.framework
.func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Restofire.Configuration.default.host = "httpbin.org"
Restofire.Retry.default.retryErrorCodes = [.requestTimedOut,.networkConnectionLost]
return true
}
import Restofire
protocol ApiaryConfigurable: Configurable {}
extension ApiaryConfigurable {
public var configuration: Configuration {
var configuration = Configuration.default
configuration.host = "private-07c21-rahulkatariya.apiary-mock.com"
configuration.headers = ["Content-Type": "application/json"]
return configuration
}
}
protocol ApiaryRequestable: Requestable, ApiaryConfigurable {}
import Restofire
struct NoteResponseModel: Decodable {
var id: Int16
var title: String
}
struct NotesGETService: ApiaryRequestable {
typealias Response = [NoteResponseModel]
var path: String? = "notes"
}
import Restofire
struct NoteRequestModel: Encodable {
var title: String
}
struct NotePOSTService: Requestable {
typealias Response = NoteResponseModel
let host: String = "private-07c21-rahulkatariya.apiary-mock.com"
let headers = ["Content-Type": "application/json"]
let path: String? = "notes"
let method: HTTPMethod = .post
var parameters: Any?
init(parameters: NoteRequestModel) {
let data = try! JSONEncoder().encode(parameters)
self.parameters = try! JSONSerialization.jsonObject(with: data, options: .allowFragments)
}
}
Requestable
gives completion handler to enable making requests and receive response.
import Restofire
class ViewController: UITableViewController {
var notes: [NoteResponseModel]!
var requestOp: RequestOperation<NotesGetAllService>!
override func viewDidLoad() {
super.viewDidLoad()
// We want to cancel the request to save resources when the user pops the view controller.
requestOp = NotesGetAllService().execute() {
if let value = $0.result.value {
self.notes = value
}
}
}
func postNote(title: String) {
let noteRequestModel = NoteRequestModel(title: title)
// We don't want to cancel the request even if user pops the view controller.
NotePOSTService(parameters: noteRequestModel).execute()
}
deinit {
requestOp.cancel()
}
}
Requestable
gives delegate methods to enable making requests from anywhere which you can use to store data in your cache.
import Restofire
struct NotesGetAllService: ApiaryRequestable {
...
func request(_ request: RequestOperation<NotesGetAllService>, didCompleteWithValue value: [NoteResponseModel]) {
// Here you can store the results into your cache and then listen for changes inside your view controller.
}
}
By adding the following snippet in your project, All Requestable
associatedType Response as Decodable
will be decoded with JSONDecoder.
import Restofire
extension Restofire.DataResponseSerializable where Response: Decodable {
public var responseSerializer: DataResponseSerializer<Response> {
return DataRequest.JSONDecodableResponseSerializer()
}
}
By adding the following snippet in your project, All Requestable
associatedType Response as Any
will be decoded with NSJSONSerialization.
import Restofire
extension Restofire.DataResponseSerializable where Response == Any {
public var responseSerializer: DataResponseSerializer<Response> {
return DataRequest.jsonResponseSerializer()
}
}
Requestable
gives you a property waitsForConnectivity which can be set to true. This will make the first request regardless of the internet connectivity. If the request fails due to .notConnectedToInternet, it will retry the request when internet connection is established.
struct PushTokenPutService: Requestable {
typealias Response = Data
...
var waitsForConnectivity: Bool = true
}
Issues and pull requests are welcome!
Rahul Katariya @rahulkatariya91
Restofire is released under the MIT license. See LICENSE for details.
link |
Stars: 380 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics