Open up DefaultClient more to be able to extend it, and ability to provide custom okHttpClient
Describe the problem you'd like to have solved
I would like to customize the networking client (DefaultClient) to be able to add an interceptor to the OkHttp chain. This will allow me to log all the Auth0 network calls to my cloud logging provider.
Describe the ideal solution
Open up the parameter "DefaultClient.okHttpClient" to be able to provide my own implementation of OkHttpClient.
Alternatives and current work-arounds
With the current SDK, the only way I can think of is to provide my own implementation of the NetworkingClient. But my implementation would almost copy all the code from DefaultClient (which is a final class by the way and not extendable), and force me to write my own gson parser. Writing my own gson parser is almost impossible as that would require understanding and maintaining all the network responses and formats.
Additional information, if any
It would be a simple change and I can provide a PR if needed. Add a new parameter to the constructor to be able to provide custom implementation of OkHttpClient.
@nishanth043 I have been looking into this issue and I understand your concern. However, exposing the default client wouldn't be the ideal solution since that would be exposing an instance of an external library through ours. This can create hard dependency and even breaking changes because of the other library.
I am exploring a generic way to support this. Please provide us with any feedback if you have any suggestions on this as well
Got it, if you do not wish to expose OkHttp Interceptor, we could create our own Auth0Interceptor interface and have the ability to add any Auth0Intercetor objects to the Default client. Internally we can just write adapters to convert these Auth0Interceptors to OkHttpInterceptors and them to the chain.
Yes @nishanth043 that was something I was thinking about as well and that would be a great idea. If you are still up for providing a PR for this we would be happy to help
Hi @nishanth043, actually after discussing with @jimmyjames we figured that this can be achieved without any deserialization done on your end. For example the below custom network client is a sample which will work
private val account: Auth0 by lazy {
val account = Auth0(
getString(R.string.com_auth0_client_id),
getString(R.string.com_auth0_domain)
)
val builder = OkHttpClient.Builder()
builder.addInterceptor { chain ->
val request: Request = chain.request()
Log.d(DatabaseLoginFragment::class.java.name, "Testing requests")
chain.proceed(request)
}
val customOkHttpClient = builder.build()
// Only enable network traffic logging on production environments!
account.networkingClient = object : NetworkingClient {
override fun load(url: String, options: RequestOptions): ServerResponse {
val request = Request.Builder()
.url(url)
.headers(options.headers.toHeaders())
.build()
val response = customOkHttpClient.newCall(request).execute()
return ServerResponse(
response.code,
response.body!!.byteStream(),
response.headers.toMultimap()
)
}
}
account
}
Hope this helps you. I will close this issue for now but feel free to reopen it if you have more doubts on this