-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggestion for adding Retrofit support to API with minimal impact on …
…existing solution: * Only change to Client.kt is to make it override a ClientInterface * Added RetrofitClientFacade to wrap normal client, that add Retrofit service support * Retrofit service will be built when calling resumeLastLoggedInUser() RetrofitClientFacade * All other client calls to RetrofitClientFacade will by proxied to the normal Client instance * For Retrofit clients, makeAuthenticatedRequest() should be caused on RetrofitClientFacade instance and not on User object
- Loading branch information
Anders Carlsen
committed
Sep 27, 2021
1 parent
605bb1e
commit c034350
Showing
3 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
webflows/src/main/kotlin/com/schibsted/account/webflows/client/ClientInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.schibsted.account.webflows.client | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import com.schibsted.account.webflows.activities.AuthorizationManagementActivity | ||
import com.schibsted.account.webflows.user.User | ||
|
||
interface ClientInterface { | ||
/** | ||
* Start login flow. | ||
* | ||
* Requires [AuthorizationManagementActivity.setup] to have been called before this. | ||
* | ||
* @param authRequest Authentication request parameters. | ||
*/ | ||
fun getAuthenticationIntent(context: Context, authRequest: AuthRequest = AuthRequest()): Intent | ||
|
||
/** | ||
* Start auth activity manually. | ||
* | ||
* @param authRequest Authentication request parameters. | ||
*/ | ||
fun launchAuth(context: Context, authRequest: AuthRequest = AuthRequest()) | ||
|
||
/** | ||
* Call this with the intent received via deep link to complete the login flow. | ||
* | ||
* This only needs to be used if manually starting the login flow using [launchAuth]. | ||
* If using [getAuthenticationIntent] this step will be handled for you. | ||
*/ | ||
fun handleAuthenticationResponse(intent: Intent, callback: LoginResultHandler) | ||
|
||
/** Resume any previously logged-in user session */ | ||
fun resumeLastLoggedInUser(): User? | ||
} |
55 changes: 55 additions & 0 deletions
55
webflows/src/main/kotlin/com/schibsted/account/webflows/client/RetrofitClientFacade.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.schibsted.account.webflows.client | ||
|
||
import com.schibsted.account.webflows.user.User | ||
import retrofit2.Retrofit | ||
|
||
/** Represents a client registered with Schibsted account - with Retrofit support. */ | ||
class RetrofitClientFacade<S>( | ||
private val client: Client, | ||
private val serviceClass: Class<S>, | ||
private val retrofitBuilder: Retrofit.Builder, | ||
): ClientInterface by client { | ||
|
||
private var retrofitApi: S? = null | ||
private var user: User? = null | ||
|
||
/** Resume any previously logged-in user session */ | ||
override fun resumeLastLoggedInUser(): User? { | ||
val loggedInUser = client.resumeLastLoggedInUser() | ||
|
||
if (loggedInUser != null) { | ||
user = loggedInUser | ||
retrofitApi = retrofitBuilder | ||
.client(loggedInUser.httpClient) | ||
.build() | ||
.create(serviceClass) | ||
|
||
return loggedInUser | ||
} | ||
|
||
user = null | ||
retrofitApi = null | ||
return null | ||
} | ||
|
||
/** | ||
* Perform the given [request] with user access token as Bearer token in Authorization header. | ||
* | ||
* If the initial request fails with a 401, a refresh token request is made to get a new access | ||
* token and the request will be retried with the new token if successful. | ||
* | ||
* If the refresh token request fails with an OAuth 'invalid_grant' error response, meaning the | ||
* refresh token has expired or been invalidated, the user will be logged-out (and all existing | ||
* tokens will be destroyed). | ||
* | ||
* @param request retrofit suspend request to perform with authentication using user tokens | ||
*/ | ||
suspend fun <V> makeAuthenticatedRequest(request: suspend (S) -> V?): V? { | ||
if (user?.isLoggedIn() == true) { | ||
retrofitApi?.let { | ||
return request(it) | ||
} | ||
} | ||
return null | ||
} | ||
} |