All Projects β†’ devxoul β†’ Moyasugar

devxoul / Moyasugar

Licence: mit
🍯 Syntactic sugar for Moya

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Moyasugar

Rxswiftexamples
Examples and resources for RxSwift.
Stars: ✭ 930 (+463.64%)
Mutual labels:  rxswift, alamofire, moya
Moya-Gloss
Gloss bindings for Moya
Stars: ✭ 37 (-77.58%)
Mutual labels:  rxswift, moya, alamofire
Evreflection
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Stars: ✭ 954 (+478.18%)
Mutual labels:  rxswift, alamofire, moya
Moyamapper
εΏ«ι€Ÿθ§£ζžζ¨‘εž‹ε·₯ε…·οΌŒζ”―ζŒRxSwiftγ€‚εŒζ—Άζ”―ζŒηΌ“ε­˜εŠŸθƒ½ γ€η›Έε…³ζ‰‹ε†Œ https://MoyaMapper.github.io 】
Stars: ✭ 115 (-30.3%)
Mutual labels:  rxswift, moya
Netclient Ios
Versatile HTTP Networking in Swift
Stars: ✭ 117 (-29.09%)
Mutual labels:  alamofire, moya
Restofire
Restofire is a protocol oriented networking client for Alamofire
Stars: ✭ 377 (+128.48%)
Mutual labels:  alamofire, moya
SwiftMoyaCodeGenerator
This is a Paw Extension that generates Moya code.
Stars: ✭ 14 (-91.52%)
Mutual labels:  moya, alamofire
U17
η²Ύδ»Ώζœ‰ε¦–ζ°”ζΌ«η”»(Swift5)
Stars: ✭ 853 (+416.97%)
Mutual labels:  alamofire, moya
Rxnetwork
A swift network library based on Moya/RxSwift.
Stars: ✭ 43 (-73.94%)
Mutual labels:  rxswift, moya
Networking
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support
Stars: ✭ 1,269 (+669.09%)
Mutual labels:  alamofire, moya
Simpleapiclient Ios
A configurable api client based on Alamofire4 and RxSwift4 for iOS
Stars: ✭ 68 (-58.79%)
Mutual labels:  rxswift, alamofire
Rxmarvel
Playing around marvel public API with RxSwift, Argo, Alamofire
Stars: ✭ 86 (-47.88%)
Mutual labels:  rxswift, alamofire
Tswechat
A WeChat alternative. Written in Swift 5.
Stars: ✭ 3,674 (+2126.67%)
Mutual labels:  rxswift, alamofire
Swift
πŸ’» Swift - Boilerplate Front : RxSwift, ReactorKit, JWT, Moya (Beta)
Stars: ✭ 17 (-89.7%)
Mutual labels:  rxswift, moya
Moya Modelmapper
ModelMapper bindings for Moya.
Stars: ✭ 143 (-13.33%)
Mutual labels:  rxswift, moya
Restaurant-Viewing-App
Build A Restaurant Viewing App in Swift 4.2
Stars: ✭ 43 (-73.94%)
Mutual labels:  moya, alamofire
Rxalamofire
RxSwift wrapper around the elegant HTTP networking in Swift Alamofire
Stars: ✭ 1,503 (+810.91%)
Mutual labels:  rxswift, alamofire
WhatFilm
Simple iOS app using TMDb API and RxSwift
Stars: ✭ 35 (-78.79%)
Mutual labels:  rxswift, alamofire
GitTime
GitTime is GitHub Tracking App. Using ReactorKit, RxSwift, Moya.
Stars: ✭ 55 (-66.67%)
Mutual labels:  rxswift, moya
Ios
A sample project demonstrating MVVM, RxSwift, Coordinator Pattern, Dependency Injection
Stars: ✭ 49 (-70.3%)
Mutual labels:  rxswift, moya

MoyaSugar

Swift CI CocoaPods

Syntactic sugar for Moya.

Why?

Moya is an elegant network abstraction layer which abstracts API endpoints gracefully with enum. However, it would become massive when the application is getting larger. Whenever you add an endpoint, you should write code at least 5 places: enum case, method, path, parameters and parameterEncoding property. It makes you scroll down again and again. Besides, if you would like to set different HTTP header fields, you should customize endpointClosure of MoyaProvider.

If you're as lazy as I am, MoyaSugar is for you.

At a Glance

Forget about method, path, parameters, parameterEncoding and endpointClosure. Use route, params, httpHeaderFields instead.

extension MyService: SugarTargetType {
  var route: Route {
    return .get("/me")
  }

  var params: Parameters? {
    return JSONEncoding() => [
      "username": "devxoul",
      "password": "****",
    ]
  }

  var headers: [String: String]? {
    return ["Accept": "application/json"]
  }
}

Use MoyaSugarProvider instead of MoyaProvider.

let provider = MoyaSugarProvider<MyService>()
let provider = RxMoyaSugarProvider<MyService>() // If you're using Moya/RxSwift

Complete Example

import Moya
import Moyasugar

enum GitHubAPI {
  case url(String)
  case userRepos(owner: String)
  case createIssue(owner: String, repo: String, title: String, body: String?)
  case editIssue(owner: String, repo: String, number: Int, title: String?, body: String?)
}

extension GitHubAPI : SugarTargetType {

  /// method + path
  var route: Route {
    switch self {
    case .url(let urlString):
      return .get(urlString)

    case .userRepos(let owner):
      return .get("/users/\(owner)/repos")

    case .createIssue(let owner, let repo, _, _):
      return .post("/repos/\(owner)/\(repo)/issues")

    case .editIssue(let owner, let repo, let number, _, _):
      return .patch("/repos/\(owner)/\(repo)/issues/\(number)")
    }
  }

  // override default url building behavior
  var url: URL {
    switch self {
    case .url(let urlString):
      return URL(string: urlString)!
    default:
      return self.defaultURL
    }
  }

  /// encoding + parameters
  var params: Parameters? {
    switch self {
    case .url:
      return nil

    case .userRepos:
      return nil

    case .createIssue(_, _, let title, let body):
      return JSONEncoding() => [
        "title": title,
        "body": body,
      ]

    case .editIssue(_, _, _, let title, let body):
      // Use `URLEncoding()` as default when not specified
      return [
        "title": title,
        "body": body,
      ]
    }
  }

  var headers: [String: String]? {
    return [
      "Accept": "application/json"
    ]
  }

}

APIs

  • πŸ”— protocol SugarTargetType

    - extension GitHubAPI: TargetType
    + extension GitHubAPI: SugarTargetType
    
  • πŸ”— var route: Route

    Returns Route which contains HTTP method and URL path information.

    - var method: Method { get }
    - var path: String { get }
    + var route: Route { get }
    

    Example:

    var route: Route {
      return .get("/me")
    }
    
  • πŸ”— var url: URL

    Returns the request URL. It retuens defaultURL as default, which is the combination of baseURL and path. Implement this property to return custom url. See #6 for detail.

  • πŸ”— var params: Parameters?

    Returns Parameters which contains parameter encoding and values.

    - var parameters: [String: Any]? { get }
    + var params: Parameters? { get }
    

    Example:

    var params: Parameters? {
      return JSONEncoding() => [
        "username": "devxoul",
        "password": "****",
      ]
    }
    

    Note: If the MoyaSugarProvider is initialized with an endpointClosure parameter, HTTP header fields of endpointClosure will be used. This is how to extend the target's HTTP header fields in endpointClosure:

    let endpointClosure: (GitHubAPI) -> Endpoint<GitHubAPI> = { target in
      let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
      return defaultEndpoint
        .adding(newHTTPHeaderFields: ["Authorization": "MySecretToken"])
    }
    

Requirements

Same with Moya

Installation

  • Using CocoaPods:

    pod 'MoyaSugar'
    pod 'MoyaSugar/RxSwift' # Use with RxSwift
    

MoyaSugar currently doesn't cupport Carthage.

Contributing

$ TEST=1 swift package generate-xcodeproj
$ open MoyaSugar.xcodeproj

License

MoyaSugar is under MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].