https://github.com/jdisho/tinynetworking
🌩 Simple HTTP network abstraction layer written in Swift
https://github.com/jdisho/tinynetworking
cocoapods networking swift urlrequest
Last synced: 3 months ago
JSON representation
🌩 Simple HTTP network abstraction layer written in Swift
- Host: GitHub
- URL: https://github.com/jdisho/tinynetworking
- Owner: jdisho
- License: mit
- Created: 2018-03-04T10:16:45.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-01-07T14:10:46.000Z (almost 5 years ago)
- Last Synced: 2024-12-01T16:47:44.067Z (12 months ago)
- Topics: cocoapods, networking, swift, urlrequest
- Language: Swift
- Homepage:
- Size: 701 KB
- Stars: 146
- Watchers: 5
- Forks: 14
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🌩 TinyNetworking
- A simple network abstraction layer written in Swift.
- A thin wrapper around NSURLSession.
- Supports CRUD methods.
- Compile-time checking for correct API endpoint accesses.
- Combine extensions to the API.
- (Optional) RxSwift extensions to the API.
- Inspired by [Moya](https://github.com/Moya/Moya).
- No external dependencies.
## 🛠 Installation
### CocoaPods
To integrate TinyNetworking into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
pod 'TinyNetworking'
#or
pod 'TinyNetworking/RxSwift' # for the RxSwift extentions
```
Then, run the following command:
```bash
$ pod install
```
### Carthage
*Coming Soon*
### Swift Package Manager
Add the following as a dependency to your `Package.swift`:
`.package(url: "https://github.com/jdisho/TinyNetworking.git", .upToNextMajor(from: "4.0.0"))`
### Manually
If you prefer not to use any of the dependency managers, you can integrate TinyNetworking into your project manually, by downloading the source code and placing the files on your project directory.
## 🏃♀️ Getting started
Set up an `enum` with all of your API resources like the following:
```swift
enum Unsplash {
case me
case photo(id: String)
case collection(id: String)
case likePhoto(id: String)
...
}
```
Extend `enum` and confom to `Resource` protocol.
```swift
extension Unsplash: Resource {
var baseURL: URL {
return URL(string: "https://api.unsplash.com")!
}
var endpoint: Endpoint {
switch self {
case .me:
return .get(path: "/me")
case let .photo(id: id):
return .get(path: "/photos/\(id)")
case let .collection(id: id):
return .get(path: "/collections/\(id)")
case let .likePhoto(id: id):
return .post(path: "/photos/\(id)/like")
}
}
var task: Task {
var params: [String: Any] = [:]
return .requestWithParameters(params, encoding: URLEncoding())
}
var headers: [String: String] {
return ["Authorization": "Bearer xxx"]
}
var cachePolicy: URLRequest.CachePolicy {
return .useProtocolCachePolicy
}
}
```
### ⚙️ Making and handling a request
```swift
import TinyNetworking
let tinyNetworking = TinyNetworking()
tinyNetworking.request(.photo(id: "1234")) { result in
switch result {
case let .success(response):
let photo = try? response.map(to: Photo.self)
print(photo)
case let .failure(error):
print(error)
}
}
```
### 🔖 Response
After making a request, `TinyNetworking` gives a result back in the form of `Result`, which has two cases to `switch` over. In case of success, a `Response` object is given, otherwise an error.
The `Response` object gives the possibility to use:
- Raw data from the request.
- The `URLRequest` object.
- The HTTP response.
- Debug description.
- The JSON repdesentation of the data.
- The pretty printed version of the data in a JSON format.
- Method to map/decode the data to a decodable object. A decodable object, is an object that conforms to `Codable` (`Decodable+Encodable`) protocol.
## 🐍 Reactive Extensions
Reactive extensions are cool. TinyNetworking provides reactive extensions for Combine, RxSwift and **soon** for ReactiveSwift.
### Combine
```swift
return tinyNetworking
.requestPublisher(resource: .photo(id: id))
.map(to: Photo.self)
```
### RxSwift
```swift
return tinyNetworking.rx
.request(resource: .photo(id: id))
.map(to: Photo.self)
```
## ✨ Example
See [Papr](https://github.com/jdisho/Papr/tree/papr-tinyNetworking-version)
## 👤 Author
This tiny library is created with ❤️ by [Joan Disho](https://twitter.com/_disho) at [QuickBird Studios](www.quickbirdstudios.com)
### 📃 License
TinyNetworking is released under an MIT license. See [License.md](https://github.com/jdisho/TinyNetworking/blob/master/LICENSE) for more information.