Latest 0.2.0
Homepage https://github.com/levibostian/MoyaResponseHandlerPlugin
License MIT
Platforms ios 8.0
Dependencies Moya
Authors

Moya Response Handler

Quick to configure Moya plugin to handle API responses for mobile app.

MoyaResponseHandlerPlugin

Version
License
Platform

Why?

When I build mobile apps, this is how I want to handle API network requests:

Was the API request successful (HTTP response status code >=200, <300)?

  • Yes
    • Parse the response to JSON, string, image, etc. Use the response on my app.
  • No
    • Was it a network connectivity issue?
    • Yes
    • Show the user a human readable message saying they have no Internet, request failed but they can try again, etc.
    • No
    • Was the network request successful, but the server responded back with a status code >=300?
    • Yes
      • Let me see the status code, possibly parse the response body, then return a human readable message to the user telling them about the error and how they can fix it.
    • No
      • Was the error a Moya error such as an error parsing the respone body?
      • Yes
      • Handle however you wish. I want to log this error as it’s probably an error with the app. Then, return human reable message to the user.
      • No
      • The error is unknown. Handle however you wish. I want to log this error as it’s probably an error with the app. Then, return human reable message to the user.

With all of the mobile apps I build and maintain, I copy/pasted this boilerplate code into each app and edited the code minimally to conform to the app I was building. This boilerplate code was hard to maintain across multiple apps, was a hard API to remember, error prone if I ever messed up a use case, and ugly. Because of this, I built this Moya plugin to allow me to have a quick, flexible, no boilerplate code solution for each of my apps.

How?

  • Create a class that inherits the MoyaResponseHandler protocol.
class MyMoyaResponseHandler: MoyaResponseHandler {
    ...
}
  • Install the plugin by providing a plugin instance to your MoyaProvider:
MoyaProvider<Target>(plugins: [MoyaResponseHandlerPlugin(handler: MyMoyaResponseHandler)])
  • Use your MoyaProvider as usual to call your endpoints.

Installation

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

pod "MoyaResponseHandlerPlugin"

Author

Levi Bostian image

License

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

Docs

Check out the docs here.

Development

Documentation

Docs for the plugin are located in docs directory.

The docs are generated via jazzy using command: jazzy --podspec MoyaResponseHandlerPlugin.podspec (assuming jazzy is intalled. If not: gem install jazzy)

Credits

Photo by Steve Richey on Unsplash

Latest podspec

{
    "name": "MoyaResponseHandlerPlugin",
    "version": "0.2.0",
    "summary": "Quick to configure Moya plugin to handle API responses for mobile app.",
    "description": "When building iOS apps, you need to handle the response from an API call. Maybe the response was successful. Maybe it failed because the user's Internet connection is bad. Maybe the status code was a 403 error. Maybe Moya failed parsing the response body to JSON. No matter what the case is, writing the code to handle these responses require a lot of boilerplate. With the help of Moya making it easy to work with networking calls, this plugin makes it very quick and easy to configure how your app will handle Moya network call responses.",
    "homepage": "https://github.com/levibostian/MoyaResponseHandlerPlugin",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Levi Bostian": "[email protected]"
    },
    "source": {
        "git": "https://github.com/levibostian/MoyaResponseHandlerPlugin.git",
        "tag": "0.2.0"
    },
    "social_media_url": "https://twitter.com/levibostian",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "MoyaResponseHandlerPlugin/Classes/**/*",
    "dependencies": {
        "Moya": [
            "~> 8.0.5"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This