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. ๐ ๐ ๐
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)
}
)
Codable
- decode, encode Json.
- CodableTarget<Body, Response>
- EncodableTarget
- DecodableTarget
ObjectMapper
- json, xml, dictionary mapping.
- MappableTarget<Body, Response>
- MappableBodyTarget
- MappableResponseTarget
- XMLTargetType
Useful plugins
- AcceptHeaderPlugin
- FlexibleAccessTokenPlugin
- InternationalizationPlugin
Common RESTful trait
- FilterableTarget
- FormPostableTarget
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)
}
}
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
}
}
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)
}
pod try Moya-Pretty
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'
Moya-Pretty is available under the MIT license. See the LICENSE file for more info.