Swiftpack.co - jdisho/TinyNetworking as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by jdisho.
jdisho/TinyNetworking 5.0.0
🌩 Simple HTTP network abstraction layer written in Swift
⭐️ 145
🕓 3 years ago
.package(url: "https://github.com/jdisho/TinyNetworking.git", from: "5.0.0")

🌩 TinyNetworking

Mac Twitter: @_disho

  • 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.
  • No external dependencies.

🛠 Installation

CocoaPods

To integrate TinyNetworking into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'TinyNetworking'
    
#or
    
pod 'TinyNetworking/RxSwift' # for the RxSwift extentions

Then, run the following command:

$ 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:

enum Unsplash {
  case me
  case photo(id: String)
  case collection(id: String)
  case likePhoto(id: String)
  ...
}

Extend enum and confom to Resource protocol.

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

import TinyNetworking

let tinyNetworking = TinyNetworking<Unsplash>()

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<Response, TinyNetworkingError>, 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

return tinyNetworking
     .requestPublisher(resource: .photo(id: id))
     .map(to: Photo.self)

RxSwift

return tinyNetworking.rx
     .request(resource: .photo(id: id))
     .map(to: Photo.self)

✨ Example

See Papr

👤 Author

This tiny library is created with ❤️ by Joan Disho at QuickBird Studios

📃 License

TinyNetworking is released under an MIT license. See License.md for more information.

GitHub

link
Stars: 145
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

5.0.0
3 years ago

Support RxSwift 6.0.0

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics