Coder Social home page Coder Social logo

gopherlabsltd / mayo Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 1.0 6.72 MB

Network abstraction layer written in Kotlin. ๐Ÿš€

License: MIT License

JavaScript 8.50% CSS 2.55% Kotlin 88.95%
http network libraries android kotlin okhttpclient networking android-library

mayo's Introduction

Mayo

pipeline status

You're a smart developer!

You probably use okHttp to abstract away access to HttpUrlConnection 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.


Ad hoc network layers are common in Android 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 Mayo is that we want some network abstraction layer that sufficiently encapsulates actually calling okHttp directly. It should be simple enough that common things are easy, but comprehensive enough that complicated things are also easy.

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

Some awesome features of Mayo:

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

Sample Project

There's a sample project in the Mayo-Demo directory. To use it, open the project and press run.

Project Status

This project is actively under development. A release will come shortly!

Installation

In your project level build.gradle add the following:

allprojects {
 repositories {
    jcenter()
    maven { url "https://jitpack.io" }
 }
}

In your module's build.gradle add the following:

dependencies {
    compile 'com.github.gopherlabsltd:mayo:1.0.1'
}

Usage

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

provider = MayoProvider<GitHubApi>()
provider.request(GitHubApi.zen(),
        object : HttpCallback<String> {
            override fun onFailure(call: Call?, exception: Exception?) {
                // 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 onSuccess response.
            }

            override fun onSuccess(call: Call?, response: String?) {
                // do something with the response data or statusCode
            }
        })

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

provider = MayoProvider<GitHubApi>()
provider.request(GitHubApi.userProfile("GopherLabsLtd"),
          object : HttpCallback<GitHubUser> {
              override fun onFailure(call: Call?, exception: Exception?) {
                  // Handle Error
              }

              override fun onSuccess(call: Call?, response: String?) {
                  // do something with the response data or statusCode
              }
          })

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

Reactive Extensions

On the roadmap...

ReactiveSwift

On the roadmap...

RxSwift

On the roadmap...

License

Mayo is released under an MIT license. See License.md for more information.

mayo's People

Contributors

itsalexnguyen avatar theiliad avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

morristech

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.