Flipper: Question: How do I get the networkFlipperPlugin for OkHttp interceptor

Created on 9 Aug 2019  路  6Comments  路  Source: facebook/flipper

According to the docs I have to use

.addNetworkInterceptor(FlipperOkhttpInterceptor(networkFlipperPlugin))

but that requires a networkFlipperPlugin. I create the networkFlipperPlugin in my App class though. What's the best way to do this?

Also, maybe this could be outlined in the docs better?

Most helpful comment

For new implementation you have to initialise network interceptor like this.

new FlipperOkhttpInterceptor(AndroidFlipperClient.getInstance(context).getPlugin(NetworkFlipperPlugin.ID)

All 6 comments

In Application and your initialize Flipper function

 SoLoader.init(this, false)
val client = AndroidFlipperClient.getInstance(this)
//Network
client.addPlugin(NetworkFlipperPlugin())

Then in your initialize OkHttpClient function

//add flipperOkhttpInterceptor
if(BuildConfig.DEBUG)  {
    okhttpBuilder
        .addNetworkInterceptor(
            AndroidFlipperClient.getInstance(context).getPlugin(NetworkFlipperPlugin.ID))
        )
}

@franticn how will I get context in an interface which hosts OkHttp client. Say something like in the example below.

interface GithubApiService {

    @GET("search/users")
    fun search(@Query("q") query: String,
               @Query("page") page: Int,
               @Query("per_page") perPage: Int): Observable<Result>

    /**
     * Companion object to create the GithubApiService
     */
    companion object Factory {
        fun create(): GithubApiService {

            val okHttpClient = OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addNetworkInterceptor(
                 AndroidFlipperClient.getInstance(this).getPlugin(NetworkFlipperPlugin.ID)))
                .build()

            val retrofit = Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .baseUrl("https://api.github.com/")
                    .build()

            return retrofit.create(GithubApiService::class.java);
        }
    }
}

Thanks for the answers, both!

@sudhirkhanger Maybe Your can try like this

interface GithubApiService {

    @GET("search/users")
    fun search(@Query("q") query: String,
               @Query("page") page: Int,
               @Query("per_page") perPage: Int): Observable<Result>

    /**
     * Companion object to create the GithubApiService
     */
    companion object Factory {
        fun create(context:Context): GithubApiService {

            val okHttpClient = OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addNetworkInterceptor(
                 AndroidFlipperClient.getInstance(context).getPlugin(NetworkFlipperPlugin.ID)))
                .build()

            val retrofit = Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .baseUrl("https://api.github.com/")
                    .build()

            return retrofit.create(GithubApiService::class.java);
        }
    }
}

For new implementation you have to initialise network interceptor like this.

new FlipperOkhttpInterceptor(AndroidFlipperClient.getInstance(context).getPlugin(NetworkFlipperPlugin.ID)

For anyone who doesn't want to inject Context everywhere:

AndroidFlipperClient.getInstanceIfInitialized()?.let { flipperClient ->
    addNetworkInterceptor(FlipperOkhttpInterceptor(flipperClient.getPlugin(NetworkFlipperPlugin.ID)))
}
Was this page helpful?
0 / 5 - 0 ratings