Coder Social home page Coder Social logo

tinynetworking's Introduction

๐ŸŒฉ 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.

tinynetworking's People

Contributors

jdisho avatar asteiman avatar grafele avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.