Skip to content

πŸ“‘ π™·πš˜πš  𝚝𝚘 𝚞𝚜𝚎 π™Ύπš”πš‘πšπšπš™ πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›πšœ πšπš˜πš› πš—πšŽπšπš πš˜πš›πš” πš›πšŽπššπšžπšŽπšœπšπšœ, πš•πš˜πš πšπš‘πšŽπš–, πšŠπš—πš πš–πš˜πšπš’πšπš’ πšπš‘πšŽπš– 𝚊𝚜 πš›πšŽπššπšžπš’πš›πšŽπš πš’πš— π™°πš—πšπš›πš˜πš’πš

devrath/fluffy-okhttp-interceptors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

35 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πšπš•πšžπšπšπš’-πš˜πš”πš‘πšπšπš™-πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›πšœ πŸ“‘

πŸ“‘ π™·πš˜πš  𝚝𝚘 𝚞𝚜𝚎 π™Ύπš”πš‘πšπšπš™ πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›πšœ πšπš˜πš› πš—πšŽπšπš πš˜πš›πš” πš›πšŽπššπšžπšŽπšœπšπšœ, πš•πš˜πš πšπš‘πšŽπš–, πšŠπš—πš πš–πš˜πšπš’πšπš’ πšπš‘πšŽπš– 𝚊𝚜 πš›πšŽπššπšžπš’πš›πšŽπš πš’πš— π™°πš—πšπš›πš˜πš’πš


Topics
πš†πš‘πšŠπš πš’πšœ π™Ύπš”π™·πšπšπš™ π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›
π™²πš‘πšŠπš’πš—πš’πš—πš π™Ύπš”π™·πšπšπš™ πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›
πš†πš‘πšŠπš πšŠπš›πšŽ πšπš‘πšŽ πšπš’πšπšπšŽπš›πšŽπš—πš πšπš’πš™πšŽπšœ 𝚘𝚏 π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›πšœ
πš†πš‘πšŠπš πš’πšœ πšŠπš— π™°πš—πšŠπš•πš’πšπš’πšŒπšœ π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›
πš†πš‘πšŠπš πš’πšœ πšŠπš— 𝙰𝙿𝙸 πš”πšŽπš’ π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›
πš†πš‘πšŠπš πš’πšœ πšŠπš— π™·πšƒπšƒπ™Ώ π™»πš˜πšπšπš’πš—πš π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›
Representation π™Ύπšžπšπš™πšžπš

πš†πš‘πšŠπš πš’πšœ π™Ύπš”π™·πšπšπš™ π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›

  • OkHttp is a mechanism that helps you monitor and re-write network calls.
  • In the image below we can see that a call is sent from the application -> Then it is received by the interceptor and modified -> Then further modified call is received at the server.

π™²πš‘πšŠπš’πš—πš’πš—πš π™Ύπš”π™·πšπšπš™ πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›

  • We can chain multiple interceptors and modify the request
  • Order of the chaining is important
  • Okhttp keeps a list of interceptors and processes them in the same order in which they are added

πš†πš‘πšŠπš πšŠπš›πšŽ πšπš‘πšŽ πšπš’πšπšπšŽπš›πšŽπš—πš πšπš’πš™πšŽπšœ 𝚘𝚏 π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›πšœ

There are two types of interceptors

  • Application Interceptors
  • Network Interceptors

Application Interceptors Network Interceptors
The application interceptors are the type of interceptors that are found between the application and the okhttp module. The network interceptors are the type of interceptors that are found between the okhttp module and remote server.
They are not concerned with the intermediate responses and focus on the final response sent to the application. They are concerned with the intermediary responses from the time of application making the call and receiving the final response.

πš†πš‘πšŠπš πš’πšœ πšŠπš— π™°πš—πšŠπš•πš’πšπš’πšŒπšœ π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›

  • Even here we customize the regular interceptor to send certain specific user data to the server on each API request

πš„πšœπšŽ 𝚌𝚊𝚜𝚎

  • Suppose again if we are sending information like Device-ID, OS-version etc to the server to understand the customer who is using the API service.
  • Now again instead of sending these details which can be of any length, we can send it from one place having a common interceptor

π™΄πš‘πšŠπš–πš™πš•πšŽ

class AnalyticsInterceptor(private val context: Context): Interceptor {

    private val APP_VERSION = "X-App-Version"
    private val DEVICE_MODEL = "X-Device-Model"
    private val DEVICE_PLATFORM = "X-Device-Platform"
    private val OS_VERSION = "X-Device-OS-Version"

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()

        val requestBuilder: Request.Builder = request.newBuilder()

        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        val version = packageInfo.versionName

        requestBuilder.addHeader(APP_VERSION, version)
        requestBuilder.addHeader(OS_VERSION, Build.VERSION.SDK_INT.toString())
        requestBuilder.addHeader(DEVICE_MODEL, Build.MODEL)
        requestBuilder.addHeader(DEVICE_PLATFORM, "android")

        return chain.proceed(requestBuilder.build())
    }
}

πš†πš‘πšŠπš πš’πšœ πšŠπš— 𝙰𝙿𝙸 πš”πšŽπš’ π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›

  • There is no such special thing as ApiKey or tolken Interceptor.
  • We customize the interceptor in such a way

πš„πšœπšŽ 𝚌𝚊𝚜𝚎

  • Suppose in every API request we want to pass an auth token to the server in the header.
  • We need not have to pass while creating every API request.
  • Instead of it we can pass it in a custom interceptor

π™΄πš‘πšŠπš–πš™πš•πšŽ

class ApiKeyInterceptor: Interceptor {

    private val apiKeyQueryParameterKey = "api_key"

    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        val originalUrl = originalRequest.url

        val url = originalUrl.newBuilder()
            .addQueryParameter(apiKeyQueryParameterKey, BuildConfig.THE_MOVIE_DB_API_TOKEN)
            .build()

        val newRequest = originalRequest.newBuilder()
            .url(url)
            .build()

        return chain.proceed(newRequest)
    }
}
  • Add the object while creating the okhttp request
val okHttpClient = OkHttpClient.Builder()
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(ApiKeyInterceptor()) 

πš†πš‘πšŠπš πš’πšœ πšŠπš— π™·πšƒπšƒπ™Ώ π™»πš˜πšπšπš’πš—πš π™Έπš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›

  • The HTTP Logging Interceptor is an interceptor that helps to log all the HTTP requests that are being sent to the server.
  • It also can log all the responses that are sent from the server to the application.

π™·πš˜πš  πšžπšœπšŽπšπšžπš• πš’πšœ π™·πšƒπšƒπ™Ώ πš•πš˜πšπšπš’πš—πš πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›

  • It is helpful in debugging the application on network-related issues when building the application.

πš†πš‘πšŠπš πš’πšœ πšπšŽπšπšŠπšŒπšπ™·πšŽπšŠπšπšŽπš› πš’πš— π™·πšƒπšƒπ™Ώ πš•πš˜πšπšπš’πš—πš πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›.

  • This is the ability to remove certain information from logging in to the terminal.
  • For example, we can remove the API key getting logged if it is sent in a request to the server.

π™΄πš‘πšŠπš–πš™πš•πšŽ

  • Add the code
 val loggingInterceptor = HttpLoggingInterceptor { message -> Timber.tag("OkHttp").d(message) }
      loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
      loggingInterceptor.redactHeader("x-amz-cf-id")
  • And add it while building the okhttp client
object OkHttpProvider {

  // Timeout for the network requests
  private const val REQUEST_TIMEOUT = 3L

  private var okHttpClient: OkHttpClient? = null

  fun getOkHttpClient(context: Context): OkHttpClient {
    return if (okHttpClient == null) {
      val loggingInterceptor = HttpLoggingInterceptor { message -> Timber.tag("OkHttp").d(message) }
      loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
      loggingInterceptor.redactHeader("x-amz-cf-id")

      val okHttpClient = OkHttpClient.Builder()
          .readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
          .connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
          .addInterceptor(loggingInterceptor)
          .build()
      OkHttpProvider.okHttpClient = okHttpClient
      okHttpClient
    } else {
      okHttpClient!!
    }
  }
}

πš‚πšžπš™πš™πš˜πš›πš β˜•

π™Έπš 𝚒𝚘𝚞 πšπšŽπšŽπš• πš•πš’πš”πšŽ πšœπšžπš™πš™πš˜πš›πš πš–πšŽ 𝚊 𝚌𝚘𝚏𝚏𝚎𝚎 πšπš˜πš› πš–πš’ πšŽπšπšπš˜πš›πšπšœ, 𝙸 πš πš˜πšžπš•πš πšπš›πšŽπšŠπšπš•πš’ πšŠπš™πš™πš›πšŽπšŒπš’πšŠπšπšŽ πš’πš.
π™±πšžπš’ π™ΌπšŽ 𝙰 π™²πš˜πšπšπšŽπšŽ

π™²πš˜πš—πšπš›πš’πš‹πšžπšπšŽ πŸ™‹β€β™‚οΈ

𝚁𝚎𝚊𝚍 πšŒπš˜πš—πšπš›πš’πš‹πšžπšπš’πš˜πš— πšπšžπš’πšπšŽπš•πš’πš—πšŽπšœ πšπš˜πš› πš–πš˜πš›πšŽ πš’πš—πšπš˜πš›πš–πšŠπšπš’πš˜πš— πš›πšŽπšπšŠπš›πšπš’πš—πš πšŒπš˜πš—πšπš›πš’πš‹πšžπšπš’πš˜πš—.

π™΅πšŽπšŽπšπš‹πšŠπšŒπš” ✍️

π™΅πšŽπšŠπšπšžπš›πšŽ πš›πšŽπššπšžπšŽπšœπšπšœ πšŠπš›πšŽ πšŠπš•πš πšŠπš’πšœ πš πšŽπš•πšŒπš˜πš–πšŽ, π™΅πš’πš•πšŽ πšŠπš— πš’πšœπšœπšžπšŽ πš‘πšŽπš›πšŽ.

π™΅πš’πš—πš πšπš‘πš’πšœ πš™πš›πš˜πš“πšŽπšŒπš πšžπšœπšŽπšπšžπš• ? ❀️

πš‚πšžπš™πš™πš˜πš›πš πš’πš πš‹πš’ πšŒπš•πš’πšŒπš”πš’πš—πš πšπš‘πšŽ ⭐ πš‹πšžπšπšπš˜πš— πš˜πš— πšπš‘πšŽ πšžπš™πš™πšŽπš› πš›πš’πšπš‘πš 𝚘𝚏 πšπš‘πš’πšœ πš™πšŠπšπšŽ. ✌️

π™»πš’πšŒπšŽπš—πšœπšŽ Licence πŸ’³

πšƒπš‘πš’πšœ πš™πš›πš˜πš“πšŽπšŒπš πš’πšœ πš•πš’πšŒπšŽπš—πšœπšŽπš πšžπš—πšπšŽπš› πšπš‘πšŽ π™°πš™πšŠπšŒπš‘πšŽ π™»πš’πšŒπšŽπš—πšœπšŽ 𝟸.𝟢 - 𝚜𝚎𝚎 πšπš‘πšŽ π™»π™Έπ™²π™΄π™½πš‚π™΄ πšπš’πš•πšŽ πšπš˜πš› πšπšŽπšπšŠπš’πš•πšœ.

About

πŸ“‘ π™·πš˜πš  𝚝𝚘 𝚞𝚜𝚎 π™Ύπš”πš‘πšπšπš™ πš’πš—πšπšŽπš›πšŒπšŽπš™πšπš˜πš›πšœ πšπš˜πš› πš—πšŽπšπš πš˜πš›πš” πš›πšŽπššπšžπšŽπšœπšπšœ, πš•πš˜πš πšπš‘πšŽπš–, πšŠπš—πš πš–πš˜πšπš’πšπš’ πšπš‘πšŽπš– 𝚊𝚜 πš›πšŽπššπšžπš’πš›πšŽπš πš’πš— π™°πš—πšπš›πš˜πš’πš

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Languages