package com.pixelized.biblib.network.client

import okhttp3.Interceptor
import okhttp3.Response

class BearerInterceptor : Interceptor {
    private val bearer get() = "$BEARER $token"
    var token: String? = null

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        if (request.header(NO_AUTHORIZATION) == null && token.isNullOrEmpty().not()) {
            request = request.newBuilder().addHeader(AUTHORIZATION, bearer).build()
        }
        return chain.proceed(request)
    }

    companion object {
        private const val NO_AUTHORIZATION = "No-Authentication"
        private const val AUTHORIZATION = "Authorization"
        private const val BEARER = "Bearer"
    }
}