https://github.com/fmo91/conn
Minimal yet modular networking layer for Swift.
https://github.com/fmo91/conn
ios lightweight networking protocol-oriented-programming swift
Last synced: 3 months ago
JSON representation
Minimal yet modular networking layer for Swift.
- Host: GitHub
- URL: https://github.com/fmo91/conn
- Owner: fmo91
- License: mit
- Created: 2018-05-24T20:29:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-16T19:19:51.000Z (almost 7 years ago)
- Last Synced: 2024-10-11T21:45:22.909Z (8 months ago)
- Topics: ios, lightweight, networking, protocol-oriented-programming, swift
- Language: Swift
- Size: 36.1 KB
- Stars: 23
- Watchers: 5
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Conn
[](https://travis-ci.org/fmo91/Conn)
[](https://cocoapods.org/pods/Conn)
[](https://cocoapods.org/pods/Conn)
[](https://cocoapods.org/pods/Conn)## Introduction
When writing networking layers, it´s common to end with a lot of boilerplate or repeated code.
There are currently some libraries that one can use in order to improve code readability and to avoid writing boilerplate, but they are usually so bloated that one doesn´t use the half of what that library offers.
On the other hand, there are some other libraries that are lightweight, but they often fall short when you need more advanced functionality.
Conn is the library that resolves this issue. It is very lightweight (118 lines counting empty lines until now), but it is still highly modular while it doesn't require the developer to write any boilerplate.
You can read more about the reasoning behind this library in my [article in Medium](https://medium.com/@ortizfernandomartin/minimal-networking-layer-from-scratch-in-swift-4-a151af786dc5).
## Features
- Declarative style
- Very lightweight
- Parses JSON to models
- Replaceable network dispatcher
- Minimal boilerplate required## Usage
The first thing you have to do is describing your requests as structs, classes or enums conforming to the `RequestType` protocol. For instance:
```swift
struct GetAllUsers: RequestType {
typealias ResponseType = [User]
var data: RequestData {
return RequestData(path: "https://jsonplaceholder.typicode.com/users")
}
}
````ResponseType` is an `associatedtype` that declares the format of the response. In this case, we expect to have an array of `User` as a response. The type associated to the request must implement `Codable` in order to be parsed. Take `User` as an example:
```swift
struct User: Codable {
let id: Int
let username: String
}
````RequestData` is a plain struct that defines the format of the request. In this case we are only defining a path (url), but `RequestData` also supports defining a http method, headers and params (body).
This is all you have to do. Then, to execute the request, `RequestType` has a method called `execute()` that actually dispatches the request:
```swift
GetAllUsers().execute(
onSuccess: { (users: [User]) in
// Do something with users
},
onError: { (error: Error) in
// Do something with error
}
)
```Please note that users are already parsed as an array of `User`.
## Advanced Usage
If you need to do something more complex, use other networking library like Alamofire, or anything else, the `execute` method in `RequestType` accepts an optional argument named `dispatcher` of type `NetworkDispatcher`.
`NetworkDispatcher` is a protocol that only requires implementing a single method called `dispatch`:
```swift
public protocol NetworkDispatcher {
func dispatch(request: RequestData, onSuccess: @escaping (Data) -> Void, onError: @escaping (Error) -> Void)
}
```By default, `dispatch` uses `URLSessionNetworkDispatcher` that dispatches the request using `URLSession`. But you can define your own `NetworkDispatcher` as you want.
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
## Installation
Conn is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'Conn'
```## Author
fmo91, [email protected]
## License
Conn is available under the MIT license. See the LICENSE file for more info.