Giter Club home page Giter Club logo

okhttputils's Introduction

okhttputils

Contains various different utilities for the OkHttp library usable across multiple projects.

Installation

Remove the excludes if the libraries are not already present in your app (they most likely are :D)

implementation ('dk.nodes.utils:okhttp:0.12.2')
{
    exclude module: 'retrofit:2.1.0'
    exclude module: 'converter-gson:2.1.0'
    exclude module: 'okhttp:3.4.1'
    exclude module: 'gson:2.8.0'
}

HttpErrorManager

This is a global error handler for http requests performed by the OkHttp library. The idea is that instead of having to handle common error codes like 442 (invalid token) each place you make the request, you implement a handler for the error code once and for all.

There is no initialisation since the library use the firebase content provider trick to obtain a Context.

Usage

To show a message in a dialog when a certain error code is returned:

HttpErrorManager.setHandler(500, new HttpErrorHandler("Ach nein! eine internal server fehler has passiert!!!", true);

To show a message in a dialog and run some code after the user dismiss it:

HttpErrorHandler invalidAuthHandler = new HttpErrorHandler("Ach nein! deine Token sind KAPUT!!") {
        @Override
        public boolean onError(int code, String url) {
            // Only show dialog if the endpoint url does not contain the word login
            if(url.contains("login")) {
                return false;
            }
            return true;
        }

        @Override
        public void onDialogClosed() {
            // This code gets run after the user dismissed the dialog         
            Intent intent = new Intent(App.this, SplashActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
};

// register the handler above for error code 442 and 443
HttpErrorManager.setHandler(442, invalidAuthHandler);
HttpErrorManager.setHandler(443, invalidAuthHandler);

If the user returns true from the onError method the dialog is shown. If no dialog is shown the onDialogClosed function will not get called.

okhttputils's People

Contributors

biscon avatar johsoe avatar levinzonr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

morristech

okhttputils's Issues

Token refresh handler

Implement an Authenticator for handling 401 responses when a token needs to be refreshed.

ErrorHandler

  • Scope is up for discussion, but a nice pluggable handler into okhttp could be nice.
  • Check out the competition here, is there anything we can use or get inspired from?

Readme.md

  • Gradle import
  • Various helpful description of functionality etc

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.