Git Product home page Git Product logo

moya's Introduction

Build Status codecov.io Carthage compatible

Moya Logo

You're a smart developer. You probably use Alamofire to abstract away access to NSURLSession and all those nasty details you don't really care about. But then, like lots of smart developers, you write ad hoc network abstraction layers. They are probably called "APIManager" or "NetworkModel", and they always end in tears.

Moya Overview

Ad hoc network layers are common in iOS apps. They're bad for a few reasons:

  • Makes it hard to write new apps ("where do I begin?")
  • Makes it hard to maintain existing apps ("oh my god, this mess...")
  • Makes it hard to write unit tests ("how do I do this again?")

So the basic idea of Moya is that we want some network abstraction layer that sufficiently encapsulates actually calling Alamofire directly. It should be simple enough that common things are easy, but comprehensive enough that complicated things are also easy.

If you use Alamofire to abstract away NSURLSession, why not use something to abstract away the nitty gritty of URLs, parameters, etc?

Some awesome features of Moya:

  • Compile-time checking for correct API endpoint accesses.
  • Lets you define a clear usage of different endpoints with associated enum values.
  • Treats test stubs as first-class citizens so unit testing is super-easy.

Sample Project

There's a sample project in the Demo directory. Have fun!

Project Status

This project is actively under development, and is being used in Artsy's new auction app. We consider it ready for production use.

Currently, we support Xcode 7 and Swift 2.

Installation

CocoaPods

Just add pod 'Moya' to your Podfile and go!

In any file you'd like to use Moya in, don't forget to import the framework with import Moya.

For RxSwift or ReactiveCocoa extensions, this project will include them as dependencies. You can do this via CocoaPods subspecs.

pod 'Moya/RxSwift'
pod 'Moya/ReactiveCocoa'

Then run pod install.

Carthage

Carthage users can point to this repository and use whichever generated framework they'd like, Moya, RxMoya, or ReactiveMoya. The full Moya framework is bundled in each of those frameworks; importing more than one framework in a single file will result in ambiguous lookups at compile time.

github "Moya/Moya"

Usage

After some setup, using Moya is really simple. You can access an API like this:

provider.request(.Zen) { result in
    switch result {
    case let .Success(moyaResponse):
        let data = moyaResponse.data
        let statusCode = moyaResponse.statusCode
        // do something with the response data or statusCode
    case .Failure(error):
        // this means there was a network failure - either the request
        // wasn't sent (connectivity), or no response was received (server
        // timed out).  If the server responds with a 4xx or 5xx error, that
        // will be sent as a ".Success"-ful response.
    }
}

That's a basic example. Many API requests need parameters. Moya encodes these into the enum you use to access the endpoint, like this:

provider.request(.UserProfile("ashfurrow")) { result in
    // do something with the result
}

No more typos in URLs. No more missing parameter values. No more messing with parameter encoding.

For examples, see the documentation.

Reactive Extensions

Even cooler are the reactive extensions. Moya provides reactive extensions for ReactiveCocoa and RxSwift.

ReactiveCocoa

For ReactiveCocoa, it immediately returns a SignalProducer (RACSignal is also available if needed) that you can start or bind or map or whatever you want to do. To handle errors, for instance, we could do the following:

provider.request(.UserProfile("ashfurrow")).start { (event) -> Void in
    switch event {
    case .Next(let response):
        image = UIImage(data: response.data)
    case .Failed(let error):
        print(error)
    default:
      break
    }
}

##RxSwift

For RxSwift, it immediately returns an Observable that you can subscribe to or bind or map or whatever you want to do. To handle errors, for instance, we could do the following:

provider.request(.UserProfile("ashfurrow")).subscribe { (event) -> Void in
    switch event {
    case .Next(let response):
        image = UIImage(data: response.data)
    case .Error(let error):
        print(error)
    default:
        break
    }
}

In addition to the option of using signals instead of callback blocks, there are also a series of signal operators for RxSwift and ReactiveCocoa that will attempt to map the data received from the network response into either an image, some JSON, or a string, with mapImage(), mapJSON(), and mapString(), respectively. If the mapping is unsuccessful, you'll get an error on the signal. You also get handy methods for filtering out certain status codes. This means that you can place your code for handling API errors like 400's in the same places as code for handling invalid responses.

Community Extensions

Moya has a great community around it and some people have created some very helpful extensions.

We appreciate all the work being done by the community around Moya. If you would like to have your extension featured in the list above, simply create a pull request adding your extensions to the list.

Contributing

Hey! Like Moya? Awesome! We could actually really use your help!

Open source isn't just writing code. Moya could use your help with any of the following:

  • Finding (and reporting!) bugs.
  • New feature suggestions.
  • Answering questions on issues.
  • Documentation improvements.
  • Reviewing pull requests.
  • Helping to manage issue priorities.
  • Fixing bugs/new features.

If any of that sounds cool to you, send a pull request! After a few contributions, we'll add you as an admin to the repo so you can merge pull requests and help steer the ship ๐Ÿšข

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by [its terms](https://github.com/Moya/contributors/blob/master/Code of Conduct.md).

License

Moya is released under an MIT license. See LICENSE for more information.

moya's People

Contributors

alcarvalho avatar alexanderkaraberov avatar aloco avatar aschuch avatar ashfurrow avatar bojan avatar colinta avatar delebedev avatar esttorhe avatar interstateone avatar jarrodrobins avatar jeehut avatar jrheaton avatar legoless avatar michaelmcguire avatar michaelselsky avatar mpurland avatar neonichu avatar orta avatar pedrofjfmartins avatar pmairoldi avatar rickerbh avatar skywinder avatar steam avatar sunshinejr avatar swizzlr avatar tbaranes avatar tomburns avatar wangshengjia avatar wattson12 avatar

Watchers

 avatar  avatar  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.