Moya-Pretty 2.0.0

Moya-Pretty 2.0.0

Maintained by ObiCat.



Moya-Pretty

Build Status Version License Platform Swift 4.1

Moya-Pretty provides many convenient extensions like generic class-based target, plugins, even RESTful traits. This allows you to declare Moya Target more pretty and without writing those extensions again by yourself.

Inspired by Moya and Retrofit. 👍 👍 👍

Moya-Pretty Overview

Usage

Simply, declare your target class:

struct PetService {  
  class PostPetThenResponsePet: CodableTarget<Pet, Pet>, BaseTargetType {
    var method = Method.post
    var path = "pet/"    
  }
  class PostPetOnly: EncodableTarget<Pet>, BaseTargetType {
    var method = Method.post
    var path = "pet/"    
  }
  class GetPets: DecodableTarget<[Pet]>, BaseTargetType {
    var method = Method.get
    var path = "pet/"    
  }
}

Then, execute it:

let provider = MoyaProvider<PetService.AddPet>() 
let pet = Pet(id: 1, name: "Obi")
let target = PetService.AddPet(body: pet)
provider.requestModel(target, completion: { (result) in
switch result {
  case .success(let pet):
    // Pet model here 🎉 🎉 🎉
    print(pet.name)
  case .failure(let error):
    fail(error.localizedDescription)
  }
)

Documention

Generic Target

Codable - decode, encode Json.

  • CodableTarget<Body, Response>
  • EncodableTarget
  • DecodableTarget

ObjectMapper - json, xml, dictionary mapping.

  • MappableTarget<Body, Response>
  • MappableBodyTarget
  • MappableResponseTarget
  • XMLTargetType

Plugins

Useful plugins

  • AcceptHeaderPlugin
  • FlexibleAccessTokenPlugin
  • InternationalizationPlugin

RESTful

Common RESTful trait

  • FilterableTarget
  • FormPostableTarget

RxSwift

Installation:

pod 'Moya-Pretty/RxSwift'

Execute:

let provider = MoyaProvider<PetService.AddPet>()
let pet = Pet(id: 1, name: "Obi")
let target = PetService.AddPet(body: pet)
provider.rx.requestModel(target).subscribe { event in
  switch event {
    case let .success(pet):
      // Pet model here 🎉 🎉 🎉
      print(pet.name)
    case let .error(error):
      print(error)
  }
}

ReactiveSwift

Installation:

pod 'Moya-Pretty/ReactiveSwift'

Execute:

let provider = MoyaProvider<PetService.AddPet>()
let pet = Pet(id: 1, name: "Obi")
let target = PetService.AddPet(body: pet)
provider.reactive.requestModel(target).start { event in
  switch event {
    case .value(let pet):
      // Pet model here 🎉 🎉 🎉
      print(pet.name)
    case .failed(let error):
      print(error)
    default:
      break
  }
}

PromiseKit

Installation:

pod 'Moya-Pretty/PromiseKit'

Execute:

let provider = MoyaProvider<PetService.AddPet>() 
let pet = Pet(id: 1, name: "Obi")
let target = PetService.AddPet(body: pet)
firstly {
  provider.requestModel(target)
}.done { (pet) in
  // Pet model here 🎉 🎉 🎉
  print(pet.name)
}.catch{ (error) in
  print(error)
}

Example

pod try Moya-Pretty

Installation

Moya-Pretty is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Moya-Pretty'

pod 'Moya-Pretty/ObjectMapper'

pod 'Moya-Pretty/RxSwift'

pod 'Moya-Pretty/ReactiveSwift'

pod 'Moya-Pretty/PromiseKit'

pod 'Moya-Pretty/RESTful'

pod 'Moya-Pretty/Plugins'

Author

[email protected]

License

Moya-Pretty is available under the MIT license. See the LICENSE file for more info.