1- [ ![ Release] ( https://jitpack.io/v/stanwood/Network_android.svg?style=flat-square )] ( https://jitpack.io/#stanwood/Network_android )
1+ [ ![ Release] ( https://jitpack.io/v/stanwood/framework-network-android.svg?style=flat-square )] ( https://jitpack.io/#stanwood/framework-network-android )
2+ [ ![ Build Status] ( https://www.bitrise.io/app/983e6342cc5e0e24/status.svg?token=QtXUf2lbVhJrANROaTkluQ )] ( https://www.bitrise.io/app/983e6342cc5e0e24 )
23
34# stanwood Network Utilities (Android)
45
@@ -23,12 +24,92 @@ Then add this to you app's `build.gradle`:
2324
2425``` groovy
2526dependencies {
26- implementation 'com.github.stanwood:Network_android :<insert latest version here>' // aar version available as well
27+ implementation 'com.github.stanwood:framework-network-android :<insert latest version here>' // aar version available as well
2728}
2829```
2930
3031## Usage
3132
3233Refer to the extensive javadoc of the provided classes for details on how to use them. Right now there are solutions for the following use cases:
3334
34- - handle offline situations and caching when using OkHttp (` cache ` package)
35+ - handle offline situations and caching when using OkHttp (` cache ` package)
36+ - generic token based authentication handling with OkHttp (` auth ` package)
37+
38+ ### cache
39+
40+ TODO
41+
42+ ### auth
43+
44+ The ` auth ` package contains classes for handling token based authentication with OkHttp. Generally this
45+ is done via Authenticators and Interceptors.
46+
47+ Integration into both existing means of token retrieval as well as from scratch is simple.
48+
49+ First you need to implement both ` TokenReaderWriter ` and ` AuthenticationProvider ` .
50+ The first is used for reading and writing tokens from/to requests.
51+ The second provides means to get authentication data (such as tokens and sign-in status).
52+ Refer to the javadoc for more details on how to implement these interfaces.
53+ In the future optional modules will provide implementations for common use cases.
54+
55+ Then create an instance of ` io.stanwood.framework.network.auth.Authenticator ` :
56+ ``` java
57+ Authenticator authenticator = new Authenticator (
58+ authenticationProvider,
59+ tokenReaderWriter,
60+ response - > Log . e(" Authentication failed permanently!" )
61+ );
62+ ```
63+
64+ And an instance of ` AuthInterceptor ` :
65+ ``` java
66+ AuthInterceptor authInterceptor = new AuthInterceptor (
67+ appContext,
68+ authenticationProvider,
69+ tokenReaderWriter
70+ );
71+ ```
72+
73+ Construct an ` OkHttpClient ` and pass the interceptor and the authenticator:
74+ ``` java
75+ new OkHttpClient .Builder ()
76+ .authenticator(authenticator)
77+ .addInterceptor(authInterceptor)
78+ .build();
79+ ```
80+
81+ That's it. When using this ` OkHttpClient ` instance you'll benefit from fully transparent token handling.
82+
83+ * If your app uses multiple authentication methods make sure to implement an own subclass of ` AuthenticationProvider `
84+ for each method and use an own ` OkHttpClient ` for each!*
85+
86+ #### A note on authentication exception handling
87+
88+ The library provides an own exception class called ` AuthenticationException ` . You can listen for this
89+ class in your Retrofit ` Callback.onFailure(Call, Throwable) ` to check for authentication related
90+ errors.
91+
92+ However up until now we are not able to fire this exception everywhere - especially the
93+ ` Authenticator ` suffers from a likely [ okhttp bug] ( https://github.com/square/okhttp/issues/3872 )
94+ which prevents us from firing exceptions there. In case of errors you will receive a ` NullPointerException `
95+ instead which probably won't help you much for automated handling as this exception is basically
96+ caused by every interceptor/authenticator returning ` null ` for the expected Request/Response.
97+
98+ For the time being we recommend to take a best effort approach here and additionally check for 401 response code
99+ in Retrofit's ` Callback.onSuccess() ` for when an issue in the Authenticator occured (if there was no
100+ issue you won't get a 401 propagated to ` Callback.onSuccess() ` as in case of successful authentication
101+ after receiving a 401 for the initial request your callback will get the response code of the following
102+ originally intended request).
103+
104+ If you're having problems retrieving a token in the ` AuthenticationProvider ` (e.g. due to an invalid
105+ refresh token) always try to resolve those issues there as well if possible. Throwing an
106+ ` AuthenticationException ` should just be a last resort.
107+
108+ Alternatively you can also subclass the ` Authenticator ` class and override
109+ ` onAuthenticationFailed() ` if you want to trigger special handling for failed authentication from
110+ which we couldn't recover with our default handling. This usually tends to be a bit harder to get
111+ right as it expects you to modify the failed request directly without any means of intercepting the
112+ response. Thus handling token retrieval issues should preferably handled in the ` AuthenticationProvider `
113+ as explained above.
114+
115+ We're looking forward to streamlining this as soon as the okhttp bug has been resolved.
0 commit comments