TRON is a lightweight network abstraction layer, built on top of Alamofire. It can be used to dramatically simplify interacting with RESTful JSON web-services.
Features
- [x] Generic, protocol-based implementation
- [x] Built-in response and error parsing
- [x] Support for any custom mapper (SwiftyJSON implementation provided). Defaults to
Codable
protocol. - [x] Support for upload tasks
- [x] Support for download tasks and resuming downloads
- [x] Robust plugin system
- [x] Stubbing of network requests
- [x] Modular architecture
- [x] Support for iOS/Mac OS X/tvOS/watchOS/Linux
- [x] Support for CocoaPods/Swift Package Manager
- [x] RxSwift extension
- [x] Complete documentation
Overview
We designed TRON to be simple to use and also very easy to customize. After initial setup, using TRON is very straightforward:
Requirements
- Xcode 10 and higher
- Swift 4 and higher
- iOS 10 / macOS 10.12 / tvOS 10.0 / watchOS 3.0
Installation
Swift Package Manager(requires Xcode 11)
- Add package into Project settings -> Swift Packages
TRON framework includes Codable implementation. To use SwiftyJSON, import TRONSwiftyJSON
framework. To use RxSwift wrapper, import RxTRON
.
CocoaPods
Only Core subspec, without SwiftyJSON dependency:
RxSwift extension for TRON:
Migration Guides
Project status
TRON
is under active development by MLSDev Inc. Pull requests are welcome!
Request building
TRON
object serves as initial configurator for APIRequest
, setting all base values and configuring to use with baseURL.
You need to keep strong reference to TRON
object, because it holds Alamofire.Manager, that is running all requests.
URLBuildable
URLBuildable
protocol is used to convert relative path to URL, that will be used by request.
By default, TRON
uses URLBuilder
class, that simply appends relative path to base URL, which is sufficient in most cases. You can customize url building process globally by changing urlBuilder
property on TRON
or locally, for a single request by modifying urlBuilder
property on APIRequest
.
Sending requests
To send APIRequest
, call perform(withSuccess:failure:)
method on APIRequest
:
Alternatively, you can use performCollectingTimeline(withCompletion:)
method that contains Alamofire.Response
inside completion closure:
In both cases, you can additionally chain Alamofire.Request
methods, if you need:
Response parsing
Generic APIRequest
implementation allows us to define expected response type before request is even sent. On top of Alamofire
DataResponseSerializerProtocol
, we are adding one additional protocol for error-handling.
Codable
Parsing models using Swift4 Codable
protocol is simple, implement Codable
protocol:
And send a request:
It's possible to customize decoders for both model and error parsing:
JSONDecodable
TRON
provides JSONDecodable
protocol, that allows us to parse models using SwiftyJSON:
To parse your response from the server using SwiftyJSON
, all you need to do is to create JSONDecodable
conforming type, for example:
And send a request:
There are also default implementations of JSONDecodable
protocol for Swift built-in types like String, Int, Float, Double and Bool, so you can easily do something like this:
You can also use Alamofire.Empty
struct in cases where you don't care about actual response.
Some concepts for response serialization, including array response serializer, are described in Container Types Parsing document
It's possible to customize JSONSerialization.ReadingOptions
, that are used by SwiftyJSON.JSON
object while parsing data of the response:
RxSwift
Error handling
TRON
includes built-in parsing for errors. APIError
is an implementation of ErrorSerializable
protocol, that includes several useful properties, that can be fetched from unsuccessful request:
CRUD
Using these requests is really simple:
It can be also nice to introduce namespacing to your API:
This way you can call your API methods like so:
Stubbing
Stubbing is built right into APIRequest
itself. All you need to stub a successful request is to set apiStub property and turn stubbingEnabled on:
Stubbing can be enabled globally on TRON
object or locally for a single APIRequest
. Stubbing unsuccessful requests is easy as well:
You can also optionally delay stubbing time:
Upload
- From file:
- Data:
- Stream:
- Multipart-form data:
Download
Plugins
TRON
includes plugin system, that allows reacting to most of request events.
Plugins can be used globally, on TRON
instance itself, or locally, on concrete APIRequest
. Keep in mind, that plugins that are added to TRON
instance, will be called for each request. There are some really cool use-cases for global and local plugins.
By default, no plugins are used, however two plugins are implemented as a part of TRON
framework.
NetworkActivityPlugin
NetworkActivityPlugin
serves to monitor requests and control network activity indicator in iPhone status bar. This plugin assumes you have only one TRON
instance in your application.
NetworkLoggerPlugin
NetworkLoggerPlugin
is used to log responses to console in readable format. By default, it prints only failed requests, skipping requests that were successful.
Local plugins
There are some very cool concepts for local plugins, some of them are described in dedicated PluginConcepts page.
Alternatives
We are dedicated to building best possible tool for interacting with RESTful web-services. However, we understand, that every tool has it's purpose, and therefore it's always useful to know, what other tools can be used to achieve the same goal.
TRON
was heavily inspired by Moya framework and LevelUPSDK, which is no longer available in open-source.
License
TRON
is released under the MIT license. See LICENSE for details.
About MLSDev
TRON
is maintained by MLSDev, Inc. We specialize in providing all-in-one solution in mobile and web development. Our team follows Lean principles and works according to agile methodologies to deliver the best results reducing the budget for development and its timeline.
Find out more here and don't hesitate to contact us!