Swiftpack.co - Package - jdisho/TinyNetworking

TinyNetworking is a simple network abstraction layer written in Swift.

  • Just a tiny wrapper around NSURLSession. 🌯
  • Supports CRUD methods (GET, POST, PUT, DELETE). ✌️
  • No external dependencies. 🎉
  • Works if you can determine how your data is being represented in JSON. 😇
  • Highly inspired by Moya ❤️

🛠 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

Comming Soon

Swift Package Manager

Comming Soon

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.

👨🏻‍💻 Usage

Set up an enum with all of your API resources like this example:

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

Extend enum and confom to ResourceType protocol.

extention Unsplash: ResourceType {
  var baseURL: URL {
    guard let url = URL(string: "https://api.unsplash.com") else {
      fatalError("FAILED: https://api.unsplash.com")
    }
    return url
  }
  
  var endpoint: String {
    switch self {
    case .me:
      return "/me"
    case let .photo(id):
      return "/photos/\(id)"
    case let .collection(id):
      return "/collections/\(id)"
    case let .likePhoto(id):
      return "/photos/\(id)/like"
    }
  }
  
  var method: HTTPMethod {
    switch self {
      case .me, .photo, .collection:
        return .get
      case .likePhoto:
        return .post
    }
  }
  
  // Sends paramters in URL or in HTTP Body
  var task: Task {
    var params = [:]
    return .requestWithParameters(params)
  }
  
  var headers: [String: String] {
    return ["Authorization": "Bearer xxx"]
  }
}

⚙️ Making and handling a request

import TinyNetworking

let tinyNetworking = TinyNetworking<Unsplash>()

tinyNetworking.request(.photo("1234")) { response in
  switch response {
    case let .success(result):
      let photo = try? result.map(to: Photo.self)
      print(photo)
    case let .error(error):
      print(error)
  }
}

🔥 Reactive Extensions

Reactive extensions are cool. TinyNetworking provides reactive extensions for RxSwift and soon for ReactiveSwift.

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: 31
Help us keep the lights on

Dependencies

Used By

Total: 0